Skip to content

Commit

Permalink
added view components support
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Nov 11, 2020
1 parent 6706d88 commit 8e25be5
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 13 deletions.
149 changes: 149 additions & 0 deletions src/Commands/ComponentMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php

namespace Akaunting\Module\Commands;

use Akaunting\Module\Generators\FileGenerator;
use Akaunting\Module\Support\Config\GenerateConfigReader;
use Akaunting\Module\Support\Stub;
use Akaunting\Module\Traits\ModuleCommandTrait;
use Illuminate\Support\Facades\File;
use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;

class ComponentMakeCommand extends GeneratorCommand
{
use ModuleCommandTrait;

/**
* The name of argument name.
*
* @var string
*/
protected $argumentName = 'name';

/**
* The console command name.
*
* @var string
*/
protected $name = 'module:make-component';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate new component for the specified module.';

public function handle()
{
if (parent::handle() === E_ERROR) {
return E_ERROR;
}

$this->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 <<<HTML
<div>
{$quote}
</div>
HTML;
}

protected function getViewName()
{
return Str::kebab($this->argument('name'));
}
}
42 changes: 29 additions & 13 deletions src/Commands/ProviderMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
/**
Expand All @@ -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, '\\');
}
}
28 changes: 28 additions & 0 deletions src/Commands/stubs/component.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace $NAMESPACE$;

use Illuminate\View\Component;

class $CLASS$ extends Component
{
/**
* Create a new component instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|string
*/
public function render()
{
return view('$ALIAS$::$VIEW_NAME$');
}
}
12 changes: 12 additions & 0 deletions src/Commands/stubs/scaffold/provider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace $NAMESPACE$;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider as Provider;
use Illuminate\Database\Eloquent\Factory;

Expand All @@ -16,6 +17,7 @@ class $NAME$ extends Provider
{
$this->loadConfig();
$this->loadViews();
$this->loadViewComponents();
$this->loadTranslations();
$this->loadMigrations();
}
Expand Down Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions src/Config/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
],
],

Expand Down
2 changes: 2 additions & 0 deletions src/Providers/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,6 +53,7 @@ class Console extends ServiceProvider
*/
protected $commands = [
CommandMakeCommand::class,
ComponentMakeCommand::class,
ControllerMakeCommand::class,
DeleteCommand::class,
DisableCommand::class,
Expand Down

0 comments on commit 8e25be5

Please sign in to comment.