From c2ed43f73bf5e9a739a6513a888d28d4a5b6d5d0 Mon Sep 17 00:00:00 2001 From: Gerfey Date: Mon, 15 Mar 2021 19:56:52 +0700 Subject: [PATCH] v0.1.0 --- composer.json | 83 ++++++++++--------- config/structure.php | 80 ++++++++++++++++++ src/Console/LaravelDomainSkeletonCommand.php | 22 ++++- src/LaravelDomainSkeleton.php | 35 ++++++++ src/LaravelDomainSkeletonServiceProvider.php | 17 ++-- src/Storage/StorageDomainSkeleton.php | 67 +++++++++++++++ src/Storage/Structures/DirectoryStructure.php | 54 ++++++++++++ src/Storage/Structures/FileStructure.php | 46 ++++++++++ src/Storage/Structures/Structure.php | 63 ++++++++++++++ src/Templates/Api.php | 10 +++ src/Templates/Controller.php | 14 ++++ src/Templates/Model.php | 18 ++++ src/Templates/Repository.php | 14 ++++ src/Templates/ServicesProvider.php | 33 ++++++++ 14 files changed, 507 insertions(+), 49 deletions(-) create mode 100644 config/structure.php create mode 100644 src/LaravelDomainSkeleton.php create mode 100644 src/Storage/StorageDomainSkeleton.php create mode 100644 src/Storage/Structures/DirectoryStructure.php create mode 100644 src/Storage/Structures/FileStructure.php create mode 100644 src/Storage/Structures/Structure.php create mode 100644 src/Templates/Api.php create mode 100644 src/Templates/Controller.php create mode 100644 src/Templates/Model.php create mode 100644 src/Templates/Repository.php create mode 100644 src/Templates/ServicesProvider.php diff --git a/composer.json b/composer.json index 945af2b..e075edd 100644 --- a/composer.json +++ b/composer.json @@ -1,44 +1,45 @@ { - "name": "gerfey/laravel-domain-skeleton", - "description": "Make a Domain skeleton", - "license": "MIT", - "homepage": "https://vk.com/gerfey", - "keywords": [ - "gerfey", - "laravel", - "domain", - "skeleton", - "laravel-domain-skeleton" - ], - "authors": [ - { - "name": "Alexander Levchenkov", - "email": "sanjia@live.ru" - } - ], - "require": { - "php": "^7.1" - }, - "autoload": { - "psr-4": { - "Gerfey\\LaravelDomainSkeleton\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "Gerfey\\LaravelDomainSkeleton\\Tests\\": "tests/" - } - }, - "extra": { - "branch-alias": { - "dev-master": "0.0.1-dev" + "name": "gerfey/laravel-domain-skeleton", + "description": "Make a Domain skeleton", + "license": "MIT", + "homepage": "https://vk.com/gerfey", + "keywords": [ + "gerfey", + "laravel", + "domain", + "skeleton", + "laravel-domain-skeleton" + ], + "authors": [ + { + "name": "Alexander Levchenkov", + "email": "sanjia@live.ru" + } + ], + "require": { + "php": "^7.1", + "gerfey/repository": "@dev" }, - "laravel": { - "providers": [ - "Gerfey\\LaravelDomainSkeleton\\LaravelDomainSkeletonServiceProvider" - ] - } - }, - "minimum-stability": "dev", - "prefer-stable": true + "autoload": { + "psr-4": { + "Gerfey\\LaravelDomainSkeleton\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Gerfey\\LaravelDomainSkeleton\\Tests\\": "tests/" + } + }, + "extra": { + "branch-alias": { + "dev-master": "0.1.0-dev" + }, + "laravel": { + "providers": [ + "Gerfey\\LaravelDomainSkeleton\\LaravelDomainSkeletonServiceProvider" + ] + } + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/config/structure.php b/config/structure.php new file mode 100644 index 0000000..8ff4bca --- /dev/null +++ b/config/structure.php @@ -0,0 +1,80 @@ + 'Database', + 'type' => 'dir', + 'children' => [ + [ + 'name' => 'Migrations', + 'type' => 'dir', + 'children' => [] + ], + [ + 'name' => 'Models', + 'type' => 'dir', + 'children' => [ + [ + 'name' => '_DomainName_.php', + 'type' => 'file', + 'template' => 'Model' + ] + ] + ], + [ + 'name' => 'Repository', + 'type' => 'dir', + 'children' => [ + [ + 'name' => '_DomainName_Repository.php', + 'type' => 'file', + 'template' => 'Repository' + ] + ] + ] + ] + ], + [ + 'name' => 'Http', + 'type' => 'dir', + 'children' => [ + [ + 'name' => 'Controller', + 'type' => 'dir', + 'children' => [ + [ + 'name' => '_DomainName_Controller.php', + 'type' => 'file', + 'template' => 'Controller' + ] + ] + ], + [ + 'name' => 'Middleware', + 'type' => 'dir', + 'children' => [] + ], + [ + 'name' => 'Requests', + 'type' => 'dir', + 'children' => [] + ] + ] + ], + [ + 'name' => 'Routes', + 'type' => 'dir', + 'children' => [ + [ + 'name' => 'api.php', + 'type' => 'file', + 'template' => 'Api' + ] + ] + ], + [ + 'name' => '_DomainName_ServicesProvider.php', + 'type' => 'file', + 'template' => 'ServicesProvider' + ] +]; diff --git a/src/Console/LaravelDomainSkeletonCommand.php b/src/Console/LaravelDomainSkeletonCommand.php index f49c41c..d2793cb 100644 --- a/src/Console/LaravelDomainSkeletonCommand.php +++ b/src/Console/LaravelDomainSkeletonCommand.php @@ -2,16 +2,34 @@ namespace Gerfey\LaravelDomainSkeleton\Console; +use Exception; +use Gerfey\LaravelDomainSkeleton\LaravelDomainSkeleton; use Illuminate\Console\Command; class LaravelDomainSkeletonCommand extends Command { + /** + * @var string + */ protected $signature = 'make:skeleton:domain {domainName}'; - protected $description = 'Make Domain skeleton.'; + /** + * @var string + */ + protected $description = 'Creating a skeleton domain framework.'; public function handle() { - $this->info('Service skeleton create complete!'); + $domainName = $this->argument('domainName'); + + try { + $domainSkeleton = new LaravelDomainSkeleton(); + $domainSkeleton->setNameDomain($domainName); + $domainSkeleton->make(); + + $this->info('Service skeleton create complete!'); + } catch (Exception $exception) { + $this->error($exception->getMessage()); + } } } diff --git a/src/LaravelDomainSkeleton.php b/src/LaravelDomainSkeleton.php new file mode 100644 index 0000000..ffc6f75 --- /dev/null +++ b/src/LaravelDomainSkeleton.php @@ -0,0 +1,35 @@ +domainName = $domainName; + } + + /** + * @throws Exception + */ + public function make(): void + { + $storage = new StorageDomainSkeleton(); + try { + $storage->createStructure($this->domainName); + } catch (Exception $e) { + throw new Exception($e->getMessage()); + } + } +} diff --git a/src/LaravelDomainSkeletonServiceProvider.php b/src/LaravelDomainSkeletonServiceProvider.php index 75a8b0f..e909dc9 100644 --- a/src/LaravelDomainSkeletonServiceProvider.php +++ b/src/LaravelDomainSkeletonServiceProvider.php @@ -10,9 +10,11 @@ class LaravelDomainSkeletonServiceProvider extends ServiceProvider public function boot() { if ($this->app->runningInConsole()) { - $this->commands([ - LaravelDomainSkeletonCommand::class - ]); + $this->commands( + [ + LaravelDomainSkeletonCommand::class + ] + ); } } @@ -21,9 +23,12 @@ public function register() if ($this->app->runningInConsole()) { $this->mergeConfigFrom(__DIR__ . '/../config/domain-skeleton.php', 'domain-skeleton'); - $this->publishes([ - __DIR__ . '/../config/domain-skeleton.php' => config_path('domain-skeleton.php'), - ], 'domain-skeleton'); + $this->publishes( + [ + __DIR__ . '/../config/domain-skeleton.php' => config_path('domain-skeleton.php'), + ], + 'domain-skeleton' + ); } } } diff --git a/src/Storage/StorageDomainSkeleton.php b/src/Storage/StorageDomainSkeleton.php new file mode 100644 index 0000000..019ca9a --- /dev/null +++ b/src/Storage/StorageDomainSkeleton.php @@ -0,0 +1,67 @@ +createRootDirectoryDomain($pathDirectoryDomain)) { + throw new Exception('Error creating a directory: ' . $pathDomainName); + } + } else { + throw new Exception('Directory on the path: ' . $pathDomainName . ' already exists.'); + } + + foreach ($this->getStructure() as $structure) { + switch ($structure['type']) { + case 'dir': + $structure = new DirectoryStructure($structure, $domainName); + break; + case 'file': + $structure = new FileStructure($structure, $domainName); + break; + } + + try { + $structure->make($pathDirectoryDomain); + } catch (Exception $e) { + throw new Exception($e->getMessage()); + } + } + } + + /** + * @param string $pathDirectoryDomain + * @return bool + */ + private function createRootDirectoryDomain(string $pathDirectoryDomain): bool + { + return mkdir($pathDirectoryDomain); + } + + /** + * @return array + */ + private function getStructure(): array + { + return require $this->structurePath; + } +} diff --git a/src/Storage/Structures/DirectoryStructure.php b/src/Storage/Structures/DirectoryStructure.php new file mode 100644 index 0000000..d5a98a8 --- /dev/null +++ b/src/Storage/Structures/DirectoryStructure.php @@ -0,0 +1,54 @@ +children = $params['children']; + } + + /** + * @param string $pathDirectoryName + * @return bool + * + * @throws Exception + */ + protected function create(string $pathDirectoryName): bool + { + if (mkdir($pathDirectoryName)) { + if (count($this->children) > 0) { + foreach ($this->children as $structure) { + switch ($structure['type']) { + case 'dir': + $structure = new DirectoryStructure($structure, $this->domainName); + break; + case 'file': + $structure = new FileStructure($structure, $this->domainName); + break; + } + + $structure->make($pathDirectoryName); + } + } + + return true; + } + + return false; + } +} diff --git a/src/Storage/Structures/FileStructure.php b/src/Storage/Structures/FileStructure.php new file mode 100644 index 0000000..c003d62 --- /dev/null +++ b/src/Storage/Structures/FileStructure.php @@ -0,0 +1,46 @@ +pathTemplate = dirname(__FILE__, 3) . '/Templates/' . $params['template'] . '.php'; + } + + /** + * @param string $pathDirectoryName + * @return bool + */ + protected function create(string $pathDirectoryName): bool + { + $content = file_get_contents($this->pathTemplate); + $content = str_replace( + ['__DOMAIN_SKELETON_DIRECTORY__', '__DomainName__', '__DomainNameLower__'], + [ + ucfirst(config('domain-skeleton.default_directory')), + ucfirst($this->domainName), + mb_strtolower($this->domainName) + ], + $content + ); + + if (!file_put_contents($pathDirectoryName, $content)) { + return false; + } + + return true; + } +} diff --git a/src/Storage/Structures/Structure.php b/src/Storage/Structures/Structure.php new file mode 100644 index 0000000..9ffcef9 --- /dev/null +++ b/src/Storage/Structures/Structure.php @@ -0,0 +1,63 @@ +domainName = $domainName; + $this->name = $this->replacePathDirectory($params['name'], $domainName); + } + + /** + * @param string $pathDirectoryName + * @param string $domainName + * + * @return string + */ + private function replacePathDirectory(string $pathDirectoryName, string $domainName): string + { + return str_replace('_DomainName_', ucwords($domainName), $pathDirectoryName); + } + + /** + * @param string $pathDomainName + * @throws Exception + */ + public function make(string $pathDomainName) + { + $pathDirectory = $pathDomainName . '/' . $this->name; + + if (!file_exists($pathDirectory)) { + if (!$this->create($pathDirectory)) { + throw new Exception('Error creating: ' . $pathDirectory); + } + } + } + + /** + * @param string $pathDirectoryName + * @return bool + */ + protected function create(string $pathDirectoryName): bool + { + return true; + } +} diff --git a/src/Templates/Api.php b/src/Templates/Api.php new file mode 100644 index 0000000..269663a --- /dev/null +++ b/src/Templates/Api.php @@ -0,0 +1,10 @@ +group( + function () { + Route::get('__DomainNameLower__', [__DomainName__Controller::class, 'index']); + } +); diff --git a/src/Templates/Controller.php b/src/Templates/Controller.php new file mode 100644 index 0000000..ccb07c6 --- /dev/null +++ b/src/Templates/Controller.php @@ -0,0 +1,14 @@ +app->runningInConsole()) { + $this->loadMigrationsFrom(__DIR__ . '/Database/Migrations'); + } + + parent::boot(); + } + + public function map() + { + $this->mapRoutes(); + } + + protected function mapRoutes() + { + Route::middleware('api') + ->prefix('api') + ->namespace($this->namespace) + ->group(base_path('app/__DOMAIN_SKELETON_DIRECTORY__/__DomainName__/Routes/api.php')); + } +}