Skip to content

Commit

Permalink
feat: switch api install to leaf mvc
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Sep 13, 2024
1 parent 8809aca commit 3a70974
Show file tree
Hide file tree
Showing 6 changed files with 257 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "leafs/cli",
"description": "A simple command line tool for installing and interacting with your leaf apps",
"homepage": "https://cli.leafphp.dev",
"version": "v2.12.0",
"version": "v2.13.0",
"keywords": [
"leaf",
"php",
Expand Down
74 changes: 68 additions & 6 deletions src/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$composer = Utils\Core::findComposer();
$leaf = Utils\Core::findLeaf();
$composer = Utils\Core::findComposer();
$needsUpdate = Package::updateAvailable();

if ($needsUpdate) {
Expand Down Expand Up @@ -111,7 +111,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$commands = [
"$composer create-project leafs/$preset " . basename($directory),
"$composer create-project leafs/mvc " . basename($directory),
'cd ' . basename($directory),
];

Expand Down Expand Up @@ -180,6 +180,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
});

if ($process->isSuccessful()) {
if ($preset === 'api') {
$this->buildAPIApp($input, $output, $directory);
}

if ($this->getAppDockPreset($input, $output)) {
$dockerThemeFolder = __DIR__ . '/themes/docker';

Expand Down Expand Up @@ -262,8 +266,6 @@ protected function buildLeafApp($input, $output, $directory): int
$process->setTty(true);
}

echo "\n";

$process->run(function ($type, $line) use ($output) {
$output->write($line);
});
Expand All @@ -285,6 +287,58 @@ protected function buildLeafApp($input, $output, $directory): int
return 0;
}

protected function buildAPIApp($input, $output, $directory): bool
{
/// Will refactor when we switch to PHP 8.2

function deleteDir($dir)
{
if (!file_exists($dir)) {
return true;
}

if (!is_dir($dir)) {
return unlink($dir);
}

foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') {
continue;
}

if (!deleteDir($dir . DIRECTORY_SEPARATOR . $item)) {
return false;
}
}

return rmdir($dir);
}

if (file_exists($directory . '/vite.config.js')) {
FS::deleteFile($directory . '/vite.config.js');
}

if (file_exists($directory . '/package.json')) {
FS::deleteFile($directory . '/package.json');
}

if (is_dir($directory . '/app/views')) {
deleteDir($directory . '/app/views');

FS::deleteFolder($directory . '/app/views');
FS::createFolder($directory . '/app/views');
FS::createFile($directory . '/app/views/.gitkeep');

deleteDir($directory . '/app/routes');
FS::superCopy(__DIR__ . '/themes/api/routes', $directory . '/app/routes');

FS::deleteFile($directory . '/public/index.php');
FS::superCopy(__DIR__ . '/themes/api/index.php', $directory . '/public/index.php');
}

return true;
}

protected function getAppName($input, $output): string
{
$name = $input->getArgument('project-name');
Expand Down Expand Up @@ -321,14 +375,22 @@ protected function getAppPreset(InputInterface $input, $output): string
}

$helper = $this->getHelper('question');
$question = new ChoiceQuestion('<info>? What kind of app do you want to create?</info> <comment>[leaf]</comment>', ['leaf', 'leaf mvc', 'leaf api'], 'leaf');
$question = new ChoiceQuestion('<info>? What kind of app do you want to create?</info> <comment>[leaf]</comment>', ['leaf', 'leaf mvc', 'leaf mvc for apis'], 'leaf');

$question->setMultiselect(false);
$question->setErrorMessage('❌ Invalid option selected!');

$preset = $helper->ask($input, $output, $question);

return str_replace('leaf ', '', $preset);
if ($preset === 'leaf mvc for apis') {
return 'api';
}

if ($preset === 'leaf mvc') {
return 'mvc';
}

return 'leaf';
}

protected function getAppTestPreset($input, $output)
Expand Down
4 changes: 4 additions & 0 deletions src/Utils/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public static function updateAvailable()
$currentVersion = static::version();
$latestVersion = static::ltsVersion();

if ($currentVersion > $latestVersion) {
return false;
}

return ($currentVersion !== $latestVersion);
}
}
123 changes: 123 additions & 0 deletions src/themes/api/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

/*
|--------------------------------------------------------------------------
| Switch to root path
|--------------------------------------------------------------------------
|
| Point to the application root directory so leaf can accurately
| resolve app paths.
|
*/
chdir(dirname(__DIR__));

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
require dirname(__DIR__) . '/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Bring in (env)
|--------------------------------------------------------------------------
|
| Quickly use our environment variables
|
*/
try {
\Dotenv\Dotenv::createUnsafeImmutable(dirname(__DIR__))->load();
} catch (\Throwable $th) {
trigger_error($th);
}

/*
|--------------------------------------------------------------------------
| Load application paths
|--------------------------------------------------------------------------
|
| Decline static file requests back to the PHP built-in webserver
|
*/
if (php_sapi_name() === 'cli-server') {
$path = realpath(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));

if (is_string($path) && __FILE__ !== $path && is_file($path)) {
return false;
}

unset($path);
}

/*
|--------------------------------------------------------------------------
| Attach blade view
|--------------------------------------------------------------------------
|
| Templating has been disabled because you chose the MVC for APIs starter.
| If you want to use blade in your application,
| you can uncomment the line below.
|
*/
// Leaf\Config::attachView(\Leaf\Blade::class);

/*
|--------------------------------------------------------------------------
| Load Leaf configuration
|--------------------------------------------------------------------------
|
| Leaf MVC allows you to customize Leaf and it's modules using
| configuration files defined in the config folder. This line
| loads the configuration files and makes them available to
| your application.
|
*/
Leaf\Core::loadApplicationConfig();

/*
|--------------------------------------------------------------------------
| Sync Leaf Db with ORM and connect
|--------------------------------------------------------------------------
|
| Sync Leaf Db with ORM and connect to the database
| This allows you to use Leaf Db without having
| to initialize it in your controllers.
|
| If you want to use a different connection from those
| used in your models, you can remove the line below and
| add your own connection with:
| db()->connect(...)
|
| **Uncomment the line below to use Leaf Db**
| **You don't need this line to use Leaf Auth**
*/
// \Leaf\Database::initDb();

/*
|--------------------------------------------------------------------------
| Load custom libraries
|--------------------------------------------------------------------------
|
| You can load your custom libraries here. If you have
| anything defined in your lib folder, you can load
| them here. Simply uncomment the line below.
|
*/
// \Leaf\Core::loadLibs();

/*
|--------------------------------------------------------------------------
| Run your Leaf MVC application
|--------------------------------------------------------------------------
|
| This line brings in all your routes and starts your application
|
*/
\Leaf\Core::runApplication();
5 changes: 5 additions & 0 deletions src/themes/api/routes/_app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

app()->get('/', function () {
response()->json(['message' => 'Congrats!! You\'re on Leaf MVC']);
});
56 changes: 56 additions & 0 deletions src/themes/api/routes/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
|--------------------------------------------------------------------------
| Set up 404 handler
|--------------------------------------------------------------------------
|
| Leaf provides a default 404 page, but you can also create your
| own 404 handler by calling app()->set404(). Whatever function
| you set will be called when a 404 error is encountered
|
*/
app()->set404(function () {
response()->json('Resource not found', 404, true);
});

/*
|--------------------------------------------------------------------------
| Set up 500 handler
|--------------------------------------------------------------------------
|
| Leaf provides a default 500 page, but you can create your own error
| 500 handler by calling the setErrorHandler() method. The function
| you set will be called when a 500 error is encountered
|
*/
app()->setErrorHandler(function () {
response()->json('An error occured, our team has been notified', 500, true);
});

/*
|--------------------------------------------------------------------------
| Set up Controller namespace
|--------------------------------------------------------------------------
|
| This allows you to directly use controller names instead of typing
| the controller namespace first.
|
*/
app()->setNamespace('\App\Controllers');

/*
|--------------------------------------------------------------------------
| Your application routes
|--------------------------------------------------------------------------
|
| Leaf MVC automatically loads all files in the routes folder that
| start with "_". We call these files route partials. An example
| partial has been created for you.
|
| If you want to manually load routes, you can
| create a file that doesn't start with "_" and manually require
| it here like so:
|
*/
// require __DIR__ . '/custom-route.php';

0 comments on commit 3a70974

Please sign in to comment.