diff --git a/src/Commands/ComponentMakeCommand.php b/src/Commands/ComponentMakeCommand.php new file mode 100644 index 0000000..4b55603 --- /dev/null +++ b/src/Commands/ComponentMakeCommand.php @@ -0,0 +1,149 @@ +createViewTemplate(); + + return 0; + } + + public function getDefaultNamespace() : string + { + $module = $this->laravel['module']; + + return $module->config('paths.generator.component.namespace') ?: $module->config('paths.generator.component.path'); + } + + /** + * Get the console command arguments. + * + * @return array + */ + protected function getArguments() + { + return [ + ['name', InputArgument::REQUIRED, 'The name of the command.'], + ['alias', InputArgument::OPTIONAL, 'The alias of module will be used.'], + ]; + } + + /** + * @return mixed + */ + protected function getTemplateContents() + { + $module = $this->getModule(); + + return (new Stub('/component.stub', [ + 'ALIAS' => $this->getModuleAlias(), + 'NAMESPACE' => $this->getClassNamespace($module), + 'CLASS' => $this->getClass(), + 'VIEW_NAME' => 'components.' . $this->getViewName() + ]))->render(); + } + + /** + * @return mixed + */ + protected function getDestinationFilePath() + { + $path = module()->getModulePath($this->getModuleAlias()); + + $config = GenerateConfigReader::read('component'); + + return $path . $config->getPath() . '/' . $this->getFileName() . '.php'; + } + + /** + * @return string + */ + protected function getFileName() + { + return Str::studly($this->argument('name')); + } + + /** + * Create the view template of the component. + * + * @return void + */ + protected function createViewTemplate() + { + $overwrite_file = $this->hasOption('force') ? $this->option('force') : false; + + $path = $this->getViewTemplatePath(); + + $contents = $this->getViewTemplateContents(); + + (new FileGenerator($path, $contents))->withFileOverwrite($overwrite_file)->generate(); + } + + protected function getViewTemplatePath() + { + $module_path = $this->laravel['module']->getModulePath($this->getModuleAlias()); + + $folder = $module_path . GenerateConfigReader::read('view')->getPath() . '/components/'; + + if (!File::isDirectory($folder)) { + File::makeDirectory($folder); + } + + return $folder . $this->getViewName() . '.blade.php'; + } + + protected function getViewTemplateContents() + { + $quote = Inspiring::quote(); + + return << + {$quote} + + HTML; + } + + protected function getViewName() + { + return Str::kebab($this->argument('name')); + } +} diff --git a/src/Commands/ProviderMakeCommand.php b/src/Commands/ProviderMakeCommand.php index 805051d..a6671eb 100644 --- a/src/Commands/ProviderMakeCommand.php +++ b/src/Commands/ProviderMakeCommand.php @@ -76,19 +76,20 @@ protected function getTemplateContents() $module = $this->getModule(); return (new Stub('/' . $stub . '.stub', [ - 'ALIAS' => $module->getAlias(), - 'NAMESPACE' => $this->getClassNamespace($module), - 'CLASS' => $this->getClass(), - 'MODULE' => $this->getModuleName(), - 'NAME' => $this->getFileName(), - 'STUDLY_NAME' => $module->getStudlyName(), - 'MODULE_NAMESPACE' => $this->laravel['module']->config('namespace'), - 'PATH_VIEWS' => GenerateConfigReader::read('view')->getPath(), - 'PATH_LANG' => GenerateConfigReader::read('lang')->getPath(), - 'PATH_CONFIG' => GenerateConfigReader::read('config')->getPath(), - 'MIGRATIONS_PATH' => GenerateConfigReader::read('migration')->getPath(), - 'FACTORIES_PATH' => GenerateConfigReader::read('factory')->getPath(), - 'ROUTES_PATH' => GenerateConfigReader::read('route')->getPath(), + 'ALIAS' => $module->getAlias(), + 'NAMESPACE' => $this->getClassNamespace($module), + 'CLASS' => $this->getClass(), + 'MODULE' => $this->getModuleName(), + 'NAME' => $this->getFileName(), + 'STUDLY_NAME' => $module->getStudlyName(), + 'MODULE_NAMESPACE' => $this->laravel['module']->config('namespace'), + 'COMPONENT_NAMESPACE' => $this->getComponentNamespace(), + 'PATH_VIEWS' => GenerateConfigReader::read('view')->getPath(), + 'PATH_LANG' => GenerateConfigReader::read('lang')->getPath(), + 'PATH_CONFIG' => GenerateConfigReader::read('config')->getPath(), + 'MIGRATIONS_PATH' => GenerateConfigReader::read('migration')->getPath(), + 'FACTORIES_PATH' => GenerateConfigReader::read('factory')->getPath(), + 'ROUTES_PATH' => GenerateConfigReader::read('route')->getPath(), ]))->render(); } /** @@ -110,4 +111,19 @@ protected function getDestinationFilePath() return $path . $generatorPath->getPath() . '/' . $this->getFileName() . '.php'; } + + protected function getComponentNamespace() + { + $module = $this->laravel['module']; + + $namespace = $module->config('namespace'); + + $namespace .= '\\' . $this->getModuleName() . '\\'; + + $namespace .= $module->config('paths.generator.component.namespace') ?: $module->config('paths.generator.component.path'); + + $namespace = str_replace('/', '\\', $namespace); + + return trim($namespace, '\\'); + } } diff --git a/src/Commands/stubs/component.stub b/src/Commands/stubs/component.stub new file mode 100644 index 0000000..916f918 --- /dev/null +++ b/src/Commands/stubs/component.stub @@ -0,0 +1,28 @@ +loadConfig(); $this->loadViews(); + $this->loadViewComponents(); $this->loadTranslations(); $this->loadMigrations(); } @@ -66,6 +68,16 @@ class $NAME$ extends Provider }, \Config::get('view.paths')), [$sourcePath]), '$ALIAS$'); } + /** + * Load view components. + * + * @return void + */ + public function loadViewComponents() + { + Blade::componentNamespace('$COMPONENT_NAMESPACE$', '$ALIAS$'); + } + /** * Load translations. * diff --git a/src/Config/module.php b/src/Config/module.php index 04ec9cb..764562e 100644 --- a/src/Config/module.php +++ b/src/Config/module.php @@ -122,6 +122,7 @@ 'email' => ['path' => 'Emails', 'generate' => false], 'notification' => ['path' => 'Notifications', 'generate' => false], 'route' => ['path' => 'Routes', 'generate' => true], + 'component' => ['path' => 'View/Components', 'generate' => false], ], ], diff --git a/src/Providers/Console.php b/src/Providers/Console.php index bf8123c..a23a089 100644 --- a/src/Providers/Console.php +++ b/src/Providers/Console.php @@ -3,6 +3,7 @@ namespace Akaunting\Module\Providers; use Akaunting\Module\Commands\CommandMakeCommand; +use Akaunting\Module\Commands\ComponentMakeCommand; use Akaunting\Module\Commands\ControllerMakeCommand; use Akaunting\Module\Commands\DeleteCommand; use Akaunting\Module\Commands\DisableCommand; @@ -52,6 +53,7 @@ class Console extends ServiceProvider */ protected $commands = [ CommandMakeCommand::class, + ComponentMakeCommand::class, ControllerMakeCommand::class, DeleteCommand::class, DisableCommand::class,