first upload all files
This commit is contained in:
249
app/Scaffold/Module/Generators/EntityGenerator.php
Normal file
249
app/Scaffold/Module/Generators/EntityGenerator.php
Normal file
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
|
||||
namespace FleetCart\Scaffold\Module\Generators;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class EntityGenerator extends Generator
|
||||
{
|
||||
/**
|
||||
* Array of views to be generated.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $views = [
|
||||
'views/index.stub' => 'Resources/views/admin/$ENTITY_NAME$/index.blade.php',
|
||||
'views/create.stub' => 'Resources/views/admin/$ENTITY_NAME$/create.blade.php',
|
||||
'views/edit.stub' => 'Resources/views/admin/$ENTITY_NAME$/edit.blade.php',
|
||||
'views/shortcuts.stub' => 'Resources/views/admin/$ENTITY_NAME$/partials/shortcuts.blade.php',
|
||||
];
|
||||
|
||||
/**
|
||||
* Generate the given entities.
|
||||
*
|
||||
* @param array $entities
|
||||
* @param bool $generateSidebar
|
||||
* @return void
|
||||
*/
|
||||
public function generate(array $entities, $generateSidebar = true)
|
||||
{
|
||||
if (count($entities) !== 0 && $generateSidebar) {
|
||||
$this->generateSidebarExtender($entities);
|
||||
}
|
||||
|
||||
foreach ($entities as $entity) {
|
||||
$this->appendPermissions($entity);
|
||||
$this->generateMigrations($entity);
|
||||
$this->generateEntity($entity);
|
||||
$this->generateController($entity);
|
||||
$this->generateRequests($entity);
|
||||
$this->generateLang($entity);
|
||||
$this->generateViews($entity);
|
||||
$this->appendRoutes($entity);
|
||||
$this->appendSidebarExtender($entity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a filled sidebar view composer
|
||||
* Or an empty one of no entities.
|
||||
*
|
||||
* @param $entities
|
||||
* @return void
|
||||
*/
|
||||
private function generateSidebarExtender($entities)
|
||||
{
|
||||
return $this->finder->put(
|
||||
$this->getModulesPath('Sidebar/SidebarExtender.php'),
|
||||
$this->getContentForStub('sidebar/sidebar-extender.stub', $entities[0])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append permissions.
|
||||
*
|
||||
* @param string $entity
|
||||
* @return void
|
||||
*/
|
||||
private function appendPermissions($entity)
|
||||
{
|
||||
$permissionsContent = $this->finder->get($this->getModulesPath('Config/permissions.php'));
|
||||
|
||||
$this->finder->put(
|
||||
$this->getModulesPath('Config/permissions.php'),
|
||||
str_replace('// append', $this->getContentForStub('config/permissions-append.stub', $entity), $permissionsContent)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate migrations file for eloquent entities.
|
||||
*
|
||||
* @param string $entity
|
||||
* @return void
|
||||
*/
|
||||
private function generateMigrations($entity)
|
||||
{
|
||||
$entityName = snake_case(str_plural($entity));
|
||||
$migrationFileName = $this->getDateTimePrefix() . "create_{$entityName}_table.php";
|
||||
|
||||
$this->finder->put(
|
||||
$this->getModulesPath("Database/Migrations/{$migrationFileName}"),
|
||||
$this->getContentForStub('migrations/create-table-migration.stub', $entity)
|
||||
);
|
||||
|
||||
$migrationFileName = $this->getDateTimePrefix() . 'create_' . str_singular($entityName) . '_translations_table.php';
|
||||
|
||||
$this->finder->put(
|
||||
$this->getModulesPath("Database/Migrations/{$migrationFileName}"),
|
||||
$this->getContentForStub('migrations/create-translation-table-migration.stub', $entity)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current time with microseconds.
|
||||
*
|
||||
* @return string
|
||||
* @return void
|
||||
*/
|
||||
private function getDateTimePrefix()
|
||||
{
|
||||
$time = microtime(true);
|
||||
$micro = sprintf('%06d', ($time - floor($time)) * 1000000);
|
||||
$date = new DateTime(date('Y-m-d H:i:s.' . $micro, $time));
|
||||
|
||||
return $date->format('Y_m_d_Hisu_');
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate entity.
|
||||
*
|
||||
* @param string $entity
|
||||
* @return void
|
||||
*/
|
||||
private function generateEntity($entity)
|
||||
{
|
||||
$this->finder->put(
|
||||
$this->getModulesPath("Entities/{$entity}.php"),
|
||||
$this->getContentForStub('entities/entity.stub', $entity)
|
||||
);
|
||||
|
||||
$this->finder->put(
|
||||
$this->getModulesPath("Entities/{$entity}Translation.php"),
|
||||
$this->getContentForStub('entities/translation-entity.stub', $entity)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the controller for the given entity.
|
||||
*
|
||||
* @param string $entity
|
||||
* @return void
|
||||
*/
|
||||
private function generateController($entity)
|
||||
{
|
||||
$this->createDirectory('Http/Controllers/Admin');
|
||||
|
||||
$this->finder->put(
|
||||
$this->getModulesPath("Http/Controllers/Admin/{$entity}Controller.php"),
|
||||
$this->getContentForStub('admin-controller.stub', $entity)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the requests for the given entity.
|
||||
*
|
||||
* @param string $entity
|
||||
* @return void
|
||||
*/
|
||||
private function generateRequests($entity)
|
||||
{
|
||||
$this->createDirectory('Http/Requests');
|
||||
|
||||
$this->finder->put(
|
||||
$this->getModulesPath("Http/Requests/Save{$entity}Request.php"),
|
||||
$this->getContentForStub('save-entity-request.stub', $entity)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate views for the given entity.
|
||||
*
|
||||
* @param string $entity
|
||||
* @return void
|
||||
*/
|
||||
private function generateViews($entity)
|
||||
{
|
||||
$entityName = snake_case(str_plural($entity));
|
||||
|
||||
$this->createDirectory("Resources/views/admin/{$entityName}/partials");
|
||||
|
||||
foreach ($this->views as $stub => $view) {
|
||||
$view = str_replace('$ENTITY_NAME$', $entityName, $view);
|
||||
|
||||
$this->finder->put(
|
||||
$this->getModulesPath($view),
|
||||
$this->getContentForStub($stub, $entity)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate language files for the given entity.
|
||||
*
|
||||
* @param string $entity
|
||||
* @return void
|
||||
*/
|
||||
private function generateLang($entity)
|
||||
{
|
||||
$this->createDirectory('Resources/lang/en');
|
||||
|
||||
$entityName = snake_case(str_plural($entity));
|
||||
|
||||
$this->finder->put(
|
||||
$this->getModulesPath("Resources/lang/en/{$entityName}.php"),
|
||||
$this->getContentForStub('lang/entity.stub', $entity)
|
||||
);
|
||||
|
||||
$this->finder->put(
|
||||
$this->getModulesPath('Resources/lang/en/permissions.php'),
|
||||
$this->getContentForStub('lang/permissions.stub', $entity)
|
||||
);
|
||||
|
||||
$this->finder->put(
|
||||
$this->getModulesPath('Resources/lang/en/attributes.php'),
|
||||
$this->getContentForStub('lang/attributes.stub', $entity)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append the routes for the given entity to the routes file.
|
||||
*
|
||||
* @param string $entity
|
||||
* @return void
|
||||
*/
|
||||
private function appendRoutes($entity)
|
||||
{
|
||||
$routeContent = $this->finder->get($this->getModulesPath('Routes/admin.php'));
|
||||
|
||||
$this->finder->put(
|
||||
$this->getModulesPath('Routes/admin.php'),
|
||||
str_replace('// append', $this->getContentForStub('routes/routes-append.stub', $entity), $routeContent)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append sidebar extender.
|
||||
*
|
||||
* @param string $entity
|
||||
* @return void
|
||||
*/
|
||||
private function appendSidebarExtender($entity)
|
||||
{
|
||||
$sidebarComposerContent = $this->finder->get($this->getModulesPath('Sidebar/SidebarExtender.php'));
|
||||
|
||||
$this->finder->put(
|
||||
$this->getModulesPath('Sidebar/SidebarExtender.php'),
|
||||
str_replace('// append', $this->getContentForStub('sidebar/sidebar-extender-append.stub', $entity), $sidebarComposerContent)
|
||||
);
|
||||
}
|
||||
}
|
||||
58
app/Scaffold/Module/Generators/FilesGenerator.php
Normal file
58
app/Scaffold/Module/Generators/FilesGenerator.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace FleetCart\Scaffold\Module\Generators;
|
||||
|
||||
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
||||
|
||||
class FilesGenerator extends Generator
|
||||
{
|
||||
/**
|
||||
* Generate the given files.
|
||||
*
|
||||
* @param array $files
|
||||
* @return void
|
||||
*/
|
||||
public function generate(array $files)
|
||||
{
|
||||
foreach ($files as $stub => $file) {
|
||||
$this->finder->put(
|
||||
$this->getModulesPath($file),
|
||||
$this->getContentFor($stub)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the base module service provider.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function generateModuleProvider()
|
||||
{
|
||||
$this->finder->put(
|
||||
$this->getModulesPath("Providers/{$this->name}ServiceProvider.php"),
|
||||
$this->getContentFor('providers/module-service-provider.stub')
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content for the given file.
|
||||
*
|
||||
* @param string $stub
|
||||
* @return string
|
||||
*
|
||||
* @throws FileNotFoundException
|
||||
*/
|
||||
private function getContentFor($stub)
|
||||
{
|
||||
$stub = $this->finder->get($this->getStubPath($stub));
|
||||
|
||||
return str_replace(
|
||||
['$MODULE$', '$LOWERCASE_MODULE$', '$PLURAL_MODULE$', '$UPPERCASE_PLURAL_MODULE$'],
|
||||
[$this->name, strtolower($this->name), strtolower(str_plural($this->name)), str_plural($this->name)],
|
||||
$stub
|
||||
);
|
||||
}
|
||||
}
|
||||
134
app/Scaffold/Module/Generators/Generator.php
Normal file
134
app/Scaffold/Module/Generators/Generator.php
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace FleetCart\Scaffold\Module\Generators;
|
||||
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
|
||||
abstract class Generator
|
||||
{
|
||||
/**
|
||||
* The instance of Filesystem.
|
||||
*
|
||||
* @var \Illuminate\Filesystem\Filesystem
|
||||
*/
|
||||
protected $finder;
|
||||
|
||||
/**
|
||||
* Name of the module.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*
|
||||
* @param \Illuminate\Filesystem\Filesystem $finder
|
||||
*/
|
||||
public function __construct(Filesystem $finder)
|
||||
{
|
||||
$this->finder = $finder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the given files.
|
||||
*
|
||||
* @param array $files
|
||||
* @return void
|
||||
*/
|
||||
abstract public function generate(array $files);
|
||||
|
||||
/**
|
||||
* Set the module name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return $this
|
||||
*/
|
||||
public function module($name)
|
||||
{
|
||||
$this->name = ucfirst($name);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current module path.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
protected function getModulesPath($path = '')
|
||||
{
|
||||
return config('modules.paths.modules') . "/{$this->name}/{$path}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Create directory if not exists.
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
protected function createDirectory($path)
|
||||
{
|
||||
$path = $this->getModulesPath($path);
|
||||
|
||||
if (! $this->finder->isDirectory($path)) {
|
||||
$this->finder->makeDirectory($path, 0755, true);
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get stub path for the given stub file.
|
||||
*
|
||||
* @param string $filename
|
||||
* @return string
|
||||
*/
|
||||
protected function getStubPath($filename)
|
||||
{
|
||||
return __DIR__ . "/../stubs/{$filename}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get content of the given stub.
|
||||
*
|
||||
* @param string $stub
|
||||
* @param string $class
|
||||
* @return string
|
||||
*/
|
||||
protected function getContentForStub($stub, $class = '')
|
||||
{
|
||||
$stub = $this->finder->get($this->getStubPath($stub));
|
||||
|
||||
return str_replace([
|
||||
'$MODULE_NAME$',
|
||||
'$LOWERCASE_MODULE_NAME$',
|
||||
'$LCFIRST_ENTITY_NAME$',
|
||||
'$ENTITY_NAME$',
|
||||
'$LOWERCASE_ENTITY_NAME$',
|
||||
'$TITLE_CASE_ENTITY_NAME$',
|
||||
'$SNAKE_CASE_ENTITY_NAME$',
|
||||
'$KEBAB_CASE_ENTITY_NAME$',
|
||||
'$PLURAL_ENTITY_NAME$',
|
||||
'$PLURAL_LOWERCASE_ENTITY_NAME$',
|
||||
'$PLURAL_TITLE_CASE_ENTITY_NAME$',
|
||||
'$PLURAL_SNAKE_CASE_ENTITY_NAME$',
|
||||
'$PLURAL_KEBAB_CASE_ENTITY_NAME$',
|
||||
], [
|
||||
$this->name,
|
||||
strtolower($this->name),
|
||||
lcfirst($class),
|
||||
$class,
|
||||
strtolower($class),
|
||||
title_case(str_replace('_', ' ', snake_case($class))),
|
||||
snake_case($class),
|
||||
kebab_case($class),
|
||||
str_plural($class),
|
||||
str_plural(strtolower($class)),
|
||||
title_case(str_replace('_', ' ', snake_case(str_plural($class)))),
|
||||
snake_case(str_plural($class)),
|
||||
kebab_case(str_plural($class)),
|
||||
], $stub);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user