Scaffolder v2.0.0
  • Namespace
  • Class

Namespaces

  • Scaffolder
    • Commands
    • Compilers
      • Core
        • Api
      • Layout
      • View

Classes

  • Scaffolder\Commands\BaseCommand
  • Scaffolder\Commands\ClearCacheCommand
  • Scaffolder\Commands\GeneratorCommand
  • Scaffolder\Commands\InitializeApiCommand
  • Scaffolder\Compilers\AbstractCompiler
  • Scaffolder\Compilers\AbstractCoreCompiler
  • Scaffolder\Compilers\AbstractViewCompiler
  • Scaffolder\Compilers\Core\Api\ApiControllerCompiler
  • Scaffolder\Compilers\Core\Api\ApiModelCompiler
  • Scaffolder\Compilers\Core\Api\ApiRouteCompiler
  • Scaffolder\Compilers\Core\ControllerCompiler
  • Scaffolder\Compilers\Core\MigrationCompiler
  • Scaffolder\Compilers\Core\ModelCompiler
  • Scaffolder\Compilers\Core\RouteCompiler
  • Scaffolder\Compilers\Layout\CreateLayoutCompiler
  • Scaffolder\Compilers\Layout\EditLayoutCompiler
  • Scaffolder\Compilers\Layout\PageLayoutCompiler
  • Scaffolder\Compilers\View\CreateViewCompiler
  • Scaffolder\Compilers\View\DashboardViewCompiler
  • Scaffolder\Compilers\View\EditViewCompiler
  • Scaffolder\Compilers\View\IndexViewCompiler
  • Scaffolder\Compilers\View\LoginViewCompiler
  • Scaffolder\Compilers\View\WelcomeViewCompiler
  • Scaffolder\ScaffolderServiceProvider
  1 <?php
  2 
  3 namespace Scaffolder\Commands;
  4 
  5 use Illuminate\Support\Facades\File;
  6 use Illuminate\Support\Facades\Log;
  7 use Scaffolder\Compilers\Core\Api\ApiControllerCompiler;
  8 use Scaffolder\Compilers\Core\Api\ApiModelCompiler;
  9 use Scaffolder\Compilers\Core\Api\ApiRouteCompiler;
 10 use Scaffolder\Compilers\Core\ControllerCompiler;
 11 use Scaffolder\Compilers\Core\MigrationCompiler;
 12 use Scaffolder\Compilers\Core\ModelCompiler;
 13 use Scaffolder\Compilers\Core\RouteCompiler;
 14 use Scaffolder\Compilers\Layout\CreateLayoutCompiler;
 15 use Scaffolder\Compilers\Layout\EditLayoutCompiler;
 16 use Scaffolder\Compilers\Layout\PageLayoutCompiler;
 17 use Scaffolder\Compilers\View\CreateViewCompiler;
 18 use Scaffolder\Compilers\View\DashboardViewCompiler;
 19 use Scaffolder\Compilers\View\EditViewCompiler;
 20 use Scaffolder\Compilers\View\IndexViewCompiler;
 21 use Scaffolder\Compilers\View\LoginViewCompiler;
 22 use Scaffolder\Compilers\View\WelcomeViewCompiler;
 23 use Scaffolder\Support\Contracts\ScaffolderExtensionInterface;
 24 use Scaffolder\Support\Contracts\ScaffolderThemeExtensionInterface;
 25 use Scaffolder\Support\Contracts\ScaffolderThemeLayoutsInterface;
 26 use Scaffolder\Support\Contracts\ScaffolderThemeViewsInterface;
 27 use Scaffolder\Support\Directory;
 28 use Scaffolder\Support\Json;
 29 
 30 class GeneratorCommand extends BaseCommand
 31 {
 32     /**
 33      * Command signature.
 34      * @var string
 35      */
 36     protected $signature = 'scaffolder:generate {--api} {--webExecution}';
 37 
 38     /**
 39      * Command description.
 40      * @var string
 41      */
 42     protected $description = 'Scaffold an application';
 43 
 44     /**
 45      * Stubs directory.
 46      * @var string
 47      */
 48     protected $stubsDirectory;
 49 
 50     /**
 51      * Theme views implementation.
 52      * @var \Scaffolder\Support\Contracts\ScaffolderThemeViewsInterface
 53      */
 54     protected $themeViews;
 55 
 56     /**
 57      * Theme layouts implementation.
 58      * @var \Scaffolder\Support\Contracts\ScaffolderThemeLayoutsInterface
 59      */
 60     protected $themeLayouts;
 61 
 62     /**
 63      * Theme extension implementation.
 64      * @var \Scaffolder\Support\Contracts\ScaffolderThemeExtensionInterface
 65      */
 66     protected $themeExtension;
 67 
 68     /**
 69      * Extension implementations.
 70      * @var \Scaffolder\Support\Contracts\ScaffolderExtensionInterface[]
 71      */
 72     protected $extensions;
 73 
 74     /**
 75      * Create a new generator command instance.
 76      *
 77      * @param \Scaffolder\Support\Contracts\ScaffolderThemeViewsInterface $themeViews
 78      * @param \Scaffolder\Support\Contracts\ScaffolderThemeLayoutsInterface $themeLayouts
 79      * @param \Scaffolder\Support\Contracts\ScaffolderThemeExtensionInterface $themeExtension
 80      * @param \Scaffolder\Support\Contracts\ScaffolderExtensionInterface[] $extensions
 81      *
 82      * @throws \Exception
 83      */
 84     public function __construct(ScaffolderThemeViewsInterface $themeViews, ScaffolderThemeLayoutsInterface $themeLayouts, ScaffolderThemeExtensionInterface $themeExtension, array $extensions)
 85     {
 86         parent::__construct();
 87 
 88         foreach ($extensions as $extension)
 89         {
 90             if (!$extension instanceof ScaffolderExtensionInterface)
 91             {
 92                 throw new \Exception('Scaffolder extensions must implement \Scaffolder\Support\Contracts\ScaffolderExtensionInterface');
 93             }
 94         }
 95 
 96         $this->themeViews = $themeViews;
 97         $this->themeLayouts = $themeLayouts;
 98         $this->themeExtension = $themeExtension;
 99         $this->extensions = $extensions;
100         $this->stubsDirectory = __DIR__ . '/../../../stubs/';
101     }
102 
103     /**
104      * Execute the Command.
105      */
106     public function handle()
107     {
108         $scaffoldApi = $this->option('api');
109         $webExecution = $this->option('webExecution');
110 
111         try
112         {
113             // Get all the models
114             $modelFiles = File::allFiles(base_path('scaffolder-config/models/'));
115 
116             // Get app config
117             $scaffolderConfig = Json::decodeFile(base_path('scaffolder-config/app.json'));
118 
119             $apiDirectory = strtolower(str_replace(' ', '-', $scaffolderConfig->name . '-api'));
120 
121             // Compilers
122             $modelCompiler = new ModelCompiler();
123             $apiModelCompiler = new ApiModelCompiler();
124             $migrationCompiler = new MigrationCompiler();
125             $controllerCompiler = new ControllerCompiler();
126             $apiControllerCompiler = new ApiControllerCompiler();
127             $indexViewCompiler = new IndexViewCompiler();
128             $createViewCompiler = new CreateViewCompiler();
129             $editViewCompiler = new EditViewCompiler();
130             $dashboardViewCompiler = new DashboardViewCompiler();
131             $welcomeViewCompiler = new WelcomeViewCompiler();
132             $loginViewCompiler = new LoginViewCompiler();
133             $pageLayoutViewCompiler = new PageLayoutCompiler();
134             $createLayoutCompiler = new CreateLayoutCompiler();
135             $editLayoutCompiler = new EditLayoutCompiler();
136             $routeCompiler = new RouteCompiler();
137             $apiRouteCompiler = new ApiRouteCompiler();
138 
139             // Compiler output
140             $modelCompilerOutput = [];
141             $apiModelCompilerOutput = [];
142             $controllerCompilerOutput = [];
143             $apiControllerCompilerOutput = [];
144             $viewCompilerOutput = [];
145             $migrationCompilerOutput = [];
146 
147             // Sidenav links
148             $sidenavLinks = [];
149 
150             // Compiled routes
151             $compiledRoutes = '';
152             $compiledApiRoutes = '';
153 
154             // Get stubs
155             $modelStub = File::get($this->stubsDirectory . 'Model.php');
156             $apiModelStub = File::get($this->stubsDirectory . 'api/Model.php');
157             $migrationStub = File::get($this->stubsDirectory . 'Migration.php');
158             $controllerStub = File::get($this->stubsDirectory . 'Controller.php');
159             $apiControllerStub = File::get($this->stubsDirectory . 'api/Controller.php');
160             $indexViewStub = File::get($this->themeViews->getIndexPath());
161             $createViewStub = File::get($this->themeViews->getCreatePath());
162             $editViewStub = File::get($this->themeViews->getEditPath());
163             $routeStub = File::get($this->stubsDirectory . 'ResourceRoute.php');
164             $apiRouteStub = File::get($this->stubsDirectory . 'api/ResourceRoute.php');
165 
166             // Create models directory
167             Directory::createIfNotExists(app_path('Models'));
168 
169             // Initialize API
170             if ($scaffoldApi)
171             {
172                 if (!(new \FilesystemIterator(base_path('../' . $apiDirectory)))->valid())
173                 {
174                     $this->writeStatus('Initializing API ...', $webExecution);
175 
176                     $this->call('scaffolder:api-initialize', [
177                         'name' => $scaffolderConfig->name,
178                         'domain' => $scaffolderConfig->api->domain,
179                         '--webExecution' => $webExecution
180                     ]);
181 
182                     $this->output->newLine();
183                 }
184                 else
185                 {
186                     $this->writeStatus('API already initialized', $webExecution);
187                 }
188 
189                 // Create models directory
190                 Directory::createIfNotExists(base_path('../' . $apiDirectory . '/app/Models'));
191             }
192 
193             $this->writeStatus('Compiling ...', $webExecution);
194             $this->output->newLine();
195 
196             // Start progress bar
197             $this->output->progressStart(count($modelFiles));
198 
199             // Iterate over model files
200             foreach ($modelFiles as $modelFile)
201             {
202                 // Get model name
203                 $modelName = ucwords($modelFile->getBasename('.' . $modelFile->getExtension()));
204 
205                 // Get model data
206                 $modelData = Json::decodeFile($modelFile->getRealPath());
207 
208                 // Create views directory
209                 Directory::createIfNotExists(base_path('resources/views/' . strtolower($modelName)));
210 
211                 $modelHash = md5_file($modelFile->getRealPath());
212 
213                 // Compile stubs
214                 array_push($modelCompilerOutput, $modelCompiler->compile($modelStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->extensions));
215                 if ($scaffoldApi) array_push($apiModelCompilerOutput, $apiModelCompiler->compile($apiModelStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->extensions));
216                 array_push($controllerCompilerOutput, $controllerCompiler->compile($controllerStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->extensions));
217                 if ($scaffoldApi) array_push($apiControllerCompilerOutput, $apiControllerCompiler->compile($apiControllerStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->extensions));
218                 array_push($migrationCompilerOutput, $migrationCompiler->compile($migrationStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->extensions));
219                 array_push($viewCompilerOutput, $indexViewCompiler->compile($indexViewStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->themeExtension, $this->extensions));
220                 array_push($viewCompilerOutput, $createViewCompiler->compile($createViewStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->themeExtension, $this->extensions));
221                 array_push($viewCompilerOutput, $editViewCompiler->compile($editViewStub, $modelName, $modelData, $scaffolderConfig, $modelHash, $this->themeExtension, $this->extensions));
222                 $compiledRoutes .= $routeCompiler->compile($routeStub, $modelName, $modelData, $scaffolderConfig, null, $this->extensions);
223                 if ($scaffoldApi) $compiledApiRoutes .= $apiRouteCompiler->compile($apiRouteStub, $modelName, $modelData, $scaffolderConfig, null, $this->extensions);
224 
225                 // Add entity link
226                 array_push($sidenavLinks, ['modelName' => $modelName, 'modelLabel' => $modelData->modelLabel]);
227 
228                 // Advance progress
229                 $this->output->progressAdvance();
230             }
231 
232             // Finish progress
233             $this->output->progressFinish();
234 
235             // Store compiled routes
236             $routeCompiler->compileGroup(File::get($this->stubsDirectory . 'Routes.php'), $compiledRoutes, $scaffolderConfig);
237             if ($scaffoldApi) $apiRouteCompiler->compileGroup(File::get($this->stubsDirectory . 'api/Routes.php'), $compiledApiRoutes, $scaffolderConfig);
238 
239             // Create layouts directory
240             Directory::createIfNotExists(base_path('resources/views/layouts'));
241 
242             // Compile page layout
243             array_push($viewCompilerOutput, $pageLayoutViewCompiler->compile(File::get($this->themeLayouts->getPagePath()), null, null, $scaffolderConfig, null, $this->themeExtension, $this->extensions, ['links' => $sidenavLinks]));
244 
245             // Compile create layout
246             array_push($viewCompilerOutput, $createLayoutCompiler->compile(File::get($this->themeLayouts->getCreatePath()), null, null, $scaffolderConfig, null, $this->themeExtension, $this->extensions));
247 
248             // Compile edit layout
249             array_push($viewCompilerOutput, $editLayoutCompiler->compile(File::get($this->themeLayouts->getEditPath()), null, null, $scaffolderConfig, null, $this->themeExtension, $this->extensions));
250 
251             // Compile dashboard view
252             array_push($viewCompilerOutput, $dashboardViewCompiler->compile(File::get($this->themeViews->getDashboardPath()), null, null, $scaffolderConfig, null, $this->themeExtension, $this->extensions));
253 
254             // Compile welcome view
255             array_push($viewCompilerOutput, $welcomeViewCompiler->compile(File::get($this->themeViews->getWelcomePath()), null, null, $scaffolderConfig, null, $this->themeExtension, $this->extensions));
256 
257             // Compile login view
258             array_push($viewCompilerOutput, $loginViewCompiler->compile(File::get($this->themeViews->getLoginPath()), null, null, $scaffolderConfig, null, $this->themeExtension, $this->extensions));
259 
260             // Summary
261             $this->comment('- Files created');
262 
263             $this->comment('- - Views');
264             foreach ($viewCompilerOutput as $viewFile)
265             {
266                 $this->info('- - - ' . $viewFile);
267             }
268 
269             $this->comment('- - Controllers');
270             foreach ($controllerCompilerOutput as $controllerFile)
271             {
272                 $this->info('- - - ' . $controllerFile);
273             }
274 
275             $this->comment('- - Migrations');
276             foreach ($migrationCompilerOutput as $migrationFile)
277             {
278                 $this->info('- - - ' . $migrationFile);
279             }
280 
281             $this->comment('- - Models');
282             foreach ($modelCompilerOutput as $modelFile)
283             {
284                 $this->info('- - - ' . $modelFile);
285             }
286 
287             if ($scaffoldApi)
288             {
289                 $this->comment('- - API Controllers');
290                 foreach ($apiControllerCompilerOutput as $apiControllerFile)
291                 {
292                     $this->info('- - - ' . $apiControllerFile);
293                 }
294 
295                 $this->comment('- - API Models');
296                 foreach ($apiModelCompilerOutput as $apiModelFile)
297                 {
298                     $this->info('- - - ' . $apiModelFile);
299                 }
300             }
301 
302             $this->writeStatus('Done', $webExecution);
303         }
304         catch (\Exception $exception)
305         {
306             $this->writeStatus('Error', $webExecution);
307             Log::error($exception->getMessage());
308         }
309     }
310 }
311 
Scaffolder v2.0.0 API documentation generated by ApiGen