Skip to content

Commit 3a70974

Browse files
committed
feat: switch api install to leaf mvc
1 parent 8809aca commit 3a70974

File tree

6 files changed

+257
-7
lines changed

6 files changed

+257
-7
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "leafs/cli",
33
"description": "A simple command line tool for installing and interacting with your leaf apps",
44
"homepage": "https://cli.leafphp.dev",
5-
"version": "v2.12.0",
5+
"version": "v2.13.0",
66
"keywords": [
77
"leaf",
88
"php",

src/CreateCommand.php

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ protected function configure()
6565
*/
6666
protected function execute(InputInterface $input, OutputInterface $output): int
6767
{
68-
$composer = Utils\Core::findComposer();
6968
$leaf = Utils\Core::findLeaf();
69+
$composer = Utils\Core::findComposer();
7070
$needsUpdate = Package::updateAvailable();
7171

7272
if ($needsUpdate) {
@@ -111,7 +111,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
111111
}
112112

113113
$commands = [
114-
"$composer create-project leafs/$preset " . basename($directory),
114+
"$composer create-project leafs/mvc " . basename($directory),
115115
'cd ' . basename($directory),
116116
];
117117

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

182182
if ($process->isSuccessful()) {
183+
if ($preset === 'api') {
184+
$this->buildAPIApp($input, $output, $directory);
185+
}
186+
183187
if ($this->getAppDockPreset($input, $output)) {
184188
$dockerThemeFolder = __DIR__ . '/themes/docker';
185189

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

265-
echo "\n";
266-
267269
$process->run(function ($type, $line) use ($output) {
268270
$output->write($line);
269271
});
@@ -285,6 +287,58 @@ protected function buildLeafApp($input, $output, $directory): int
285287
return 0;
286288
}
287289

290+
protected function buildAPIApp($input, $output, $directory): bool
291+
{
292+
/// Will refactor when we switch to PHP 8.2
293+
294+
function deleteDir($dir)
295+
{
296+
if (!file_exists($dir)) {
297+
return true;
298+
}
299+
300+
if (!is_dir($dir)) {
301+
return unlink($dir);
302+
}
303+
304+
foreach (scandir($dir) as $item) {
305+
if ($item == '.' || $item == '..') {
306+
continue;
307+
}
308+
309+
if (!deleteDir($dir . DIRECTORY_SEPARATOR . $item)) {
310+
return false;
311+
}
312+
}
313+
314+
return rmdir($dir);
315+
}
316+
317+
if (file_exists($directory . '/vite.config.js')) {
318+
FS::deleteFile($directory . '/vite.config.js');
319+
}
320+
321+
if (file_exists($directory . '/package.json')) {
322+
FS::deleteFile($directory . '/package.json');
323+
}
324+
325+
if (is_dir($directory . '/app/views')) {
326+
deleteDir($directory . '/app/views');
327+
328+
FS::deleteFolder($directory . '/app/views');
329+
FS::createFolder($directory . '/app/views');
330+
FS::createFile($directory . '/app/views/.gitkeep');
331+
332+
deleteDir($directory . '/app/routes');
333+
FS::superCopy(__DIR__ . '/themes/api/routes', $directory . '/app/routes');
334+
335+
FS::deleteFile($directory . '/public/index.php');
336+
FS::superCopy(__DIR__ . '/themes/api/index.php', $directory . '/public/index.php');
337+
}
338+
339+
return true;
340+
}
341+
288342
protected function getAppName($input, $output): string
289343
{
290344
$name = $input->getArgument('project-name');
@@ -321,14 +375,22 @@ protected function getAppPreset(InputInterface $input, $output): string
321375
}
322376

323377
$helper = $this->getHelper('question');
324-
$question = new ChoiceQuestion('<info>? What kind of app do you want to create?</info> <comment>[leaf]</comment>', ['leaf', 'leaf mvc', 'leaf api'], 'leaf');
378+
$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');
325379

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

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

331-
return str_replace('leaf ', '', $preset);
385+
if ($preset === 'leaf mvc for apis') {
386+
return 'api';
387+
}
388+
389+
if ($preset === 'leaf mvc') {
390+
return 'mvc';
391+
}
392+
393+
return 'leaf';
332394
}
333395

334396
protected function getAppTestPreset($input, $output)

src/Utils/Package.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ public static function updateAvailable()
6565
$currentVersion = static::version();
6666
$latestVersion = static::ltsVersion();
6767

68+
if ($currentVersion > $latestVersion) {
69+
return false;
70+
}
71+
6872
return ($currentVersion !== $latestVersion);
6973
}
7074
}

src/themes/api/index.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Switch to root path
6+
|--------------------------------------------------------------------------
7+
|
8+
| Point to the application root directory so leaf can accurately
9+
| resolve app paths.
10+
|
11+
*/
12+
chdir(dirname(__DIR__));
13+
14+
/*
15+
|--------------------------------------------------------------------------
16+
| Register The Auto Loader
17+
|--------------------------------------------------------------------------
18+
|
19+
| Composer provides a convenient, automatically generated class loader
20+
| for our application. We just need to utilize it! We'll require it
21+
| into the script here so that we do not have to worry about the
22+
| loading of any our classes "manually". Feels great to relax.
23+
|
24+
*/
25+
require dirname(__DIR__) . '/vendor/autoload.php';
26+
27+
/*
28+
|--------------------------------------------------------------------------
29+
| Bring in (env)
30+
|--------------------------------------------------------------------------
31+
|
32+
| Quickly use our environment variables
33+
|
34+
*/
35+
try {
36+
\Dotenv\Dotenv::createUnsafeImmutable(dirname(__DIR__))->load();
37+
} catch (\Throwable $th) {
38+
trigger_error($th);
39+
}
40+
41+
/*
42+
|--------------------------------------------------------------------------
43+
| Load application paths
44+
|--------------------------------------------------------------------------
45+
|
46+
| Decline static file requests back to the PHP built-in webserver
47+
|
48+
*/
49+
if (php_sapi_name() === 'cli-server') {
50+
$path = realpath(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
51+
52+
if (is_string($path) && __FILE__ !== $path && is_file($path)) {
53+
return false;
54+
}
55+
56+
unset($path);
57+
}
58+
59+
/*
60+
|--------------------------------------------------------------------------
61+
| Attach blade view
62+
|--------------------------------------------------------------------------
63+
|
64+
| Templating has been disabled because you chose the MVC for APIs starter.
65+
| If you want to use blade in your application,
66+
| you can uncomment the line below.
67+
|
68+
*/
69+
// Leaf\Config::attachView(\Leaf\Blade::class);
70+
71+
/*
72+
|--------------------------------------------------------------------------
73+
| Load Leaf configuration
74+
|--------------------------------------------------------------------------
75+
|
76+
| Leaf MVC allows you to customize Leaf and it's modules using
77+
| configuration files defined in the config folder. This line
78+
| loads the configuration files and makes them available to
79+
| your application.
80+
|
81+
*/
82+
Leaf\Core::loadApplicationConfig();
83+
84+
/*
85+
|--------------------------------------------------------------------------
86+
| Sync Leaf Db with ORM and connect
87+
|--------------------------------------------------------------------------
88+
|
89+
| Sync Leaf Db with ORM and connect to the database
90+
| This allows you to use Leaf Db without having
91+
| to initialize it in your controllers.
92+
|
93+
| If you want to use a different connection from those
94+
| used in your models, you can remove the line below and
95+
| add your own connection with:
96+
| db()->connect(...)
97+
|
98+
| **Uncomment the line below to use Leaf Db**
99+
| **You don't need this line to use Leaf Auth**
100+
*/
101+
// \Leaf\Database::initDb();
102+
103+
/*
104+
|--------------------------------------------------------------------------
105+
| Load custom libraries
106+
|--------------------------------------------------------------------------
107+
|
108+
| You can load your custom libraries here. If you have
109+
| anything defined in your lib folder, you can load
110+
| them here. Simply uncomment the line below.
111+
|
112+
*/
113+
// \Leaf\Core::loadLibs();
114+
115+
/*
116+
|--------------------------------------------------------------------------
117+
| Run your Leaf MVC application
118+
|--------------------------------------------------------------------------
119+
|
120+
| This line brings in all your routes and starts your application
121+
|
122+
*/
123+
\Leaf\Core::runApplication();

src/themes/api/routes/_app.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
app()->get('/', function () {
4+
response()->json(['message' => 'Congrats!! You\'re on Leaf MVC']);
5+
});

src/themes/api/routes/index.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Set up 404 handler
6+
|--------------------------------------------------------------------------
7+
|
8+
| Leaf provides a default 404 page, but you can also create your
9+
| own 404 handler by calling app()->set404(). Whatever function
10+
| you set will be called when a 404 error is encountered
11+
|
12+
*/
13+
app()->set404(function () {
14+
response()->json('Resource not found', 404, true);
15+
});
16+
17+
/*
18+
|--------------------------------------------------------------------------
19+
| Set up 500 handler
20+
|--------------------------------------------------------------------------
21+
|
22+
| Leaf provides a default 500 page, but you can create your own error
23+
| 500 handler by calling the setErrorHandler() method. The function
24+
| you set will be called when a 500 error is encountered
25+
|
26+
*/
27+
app()->setErrorHandler(function () {
28+
response()->json('An error occured, our team has been notified', 500, true);
29+
});
30+
31+
/*
32+
|--------------------------------------------------------------------------
33+
| Set up Controller namespace
34+
|--------------------------------------------------------------------------
35+
|
36+
| This allows you to directly use controller names instead of typing
37+
| the controller namespace first.
38+
|
39+
*/
40+
app()->setNamespace('\App\Controllers');
41+
42+
/*
43+
|--------------------------------------------------------------------------
44+
| Your application routes
45+
|--------------------------------------------------------------------------
46+
|
47+
| Leaf MVC automatically loads all files in the routes folder that
48+
| start with "_". We call these files route partials. An example
49+
| partial has been created for you.
50+
|
51+
| If you want to manually load routes, you can
52+
| create a file that doesn't start with "_" and manually require
53+
| it here like so:
54+
|
55+
*/
56+
// require __DIR__ . '/custom-route.php';

0 commit comments

Comments
 (0)