1 <?php
2
3 namespace Scaffolder\Commands;
4
5 use Illuminate\Support\Facades\File;
6 use Illuminate\Support\Facades\Log;
7 use Scaffolder\Support\Composer;
8
9 class InitializeApiCommand extends BaseCommand
10 {
11 12 13 14
15 protected $signature = 'scaffolder:api-initialize {name} {domain} {--webExecution}';
16
17 18 19 20
21 protected $description = 'Initialize API project';
22
23 24 25
26 public function handle()
27 {
28 $name = $this->argument('name');
29 $domain = $this->argument('domain');
30 $webExecution = $this->option('webExecution');
31
32 try
33 {
34 $workingPath = base_path('../');
35 $outputFolder = strtolower(str_replace(' ', '-', $name . '-api'));
36
37 $this->writeStatus('Installing lumen and dependencies ...', $webExecution);
38
39
40 (new Composer($workingPath))->createProject('laravel/lumen', '5.1.*', $outputFolder)
41 ->setWorkingPath($workingPath . $outputFolder)
42 ->requirePackage('dingo/api', '1.0.x@dev');
43
44 $this->writeStatus('Setting up lumen ...', $webExecution);
45
46
47 File::move($workingPath . $outputFolder . '/.env.example', $workingPath . $outputFolder . '/.env');
48
49
50 File::append($workingPath . $outputFolder . '/.env',
51 'API_DOMAIN=' . $domain . PHP_EOL .
52 'API_STANDARDS_TREE=vnd' . PHP_EOL .
53 'API_SUBTYPE=' . strtolower(str_replace(' ', '', $name)) . PHP_EOL .
54 'API_VERSION=v1' . PHP_EOL .
55 'API_NAME="' . $name . '"' . PHP_EOL .
56 'API_CONDITIONAL_REQUEST=false' . PHP_EOL .
57 'API_STRICT=false' . PHP_EOL .
58 'API_DEFAULT_FORMAT=json' . PHP_EOL .
59 'API_DEBUG=true');
60
61
62 $lumenAppFile = File::get($workingPath . $outputFolder . '/bootstrap/app.php');
63
64
65 $lumenEnvFile = File::get($workingPath . $outputFolder . '/.env');
66
67
68 $lumenAppFile = str_replace('// Dotenv::load(__DIR__.\'/../\');', 'Dotenv::load(__DIR__.\'/../\');', $lumenAppFile);
69
70
71 $lumenAppFile = str_replace('// $app->withEloquent();', '$app->withEloquent();', $lumenAppFile);
72
73
74 $registerServiceProviderPosition = strpos($lumenAppFile, 'Register Service Providers');
75 $lumenAppFile = substr($lumenAppFile, 0, $registerServiceProviderPosition + 322) . '$app->register(Dingo\Api\Provider\LumenServiceProvider::class);' . substr($lumenAppFile, $registerServiceProviderPosition + 320);
76
77
78 $lumenEnvFile = str_replace('DB_DATABASE=homestead', 'DB_DATABASE=' . env('DB_DATABASE', 'homestead'), $lumenEnvFile);
79 $lumenEnvFile = str_replace('DB_USERNAME=homestead', 'DB_USERNAME=' . env('DB_USERNAME', 'homestead'), $lumenEnvFile);
80 $lumenEnvFile = str_replace('DB_PASSWORD=secret', 'DB_PASSWORD=' . env('DB_PASSWORD', 'secret'), $lumenEnvFile);
81
82 File::put($workingPath . $outputFolder . '/.env', $lumenEnvFile);
83 File::put($workingPath . $outputFolder . '/bootstrap/app.php', $lumenAppFile);
84 }
85 catch (\Exception $exception)
86 {
87 $this->writeStatus('Error', $webExecution);
88 Log::error($exception->getMessage());
89 }
90 }
91 }
92