Skip to content

Commit

Permalink
Merge pull request #2 from Gerfey/LDS-AL-create_basic_structures
Browse files Browse the repository at this point in the history
v0.1
  • Loading branch information
Gerfey authored Mar 15, 2021
2 parents f45c19e + c2ed43f commit 5bf4d02
Show file tree
Hide file tree
Showing 14 changed files with 507 additions and 49 deletions.
83 changes: 42 additions & 41 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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
}
80 changes: 80 additions & 0 deletions config/structure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

return [
[
'name' => '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'
]
];
22 changes: 20 additions & 2 deletions src/Console/LaravelDomainSkeletonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
35 changes: 35 additions & 0 deletions src/LaravelDomainSkeleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Gerfey\LaravelDomainSkeleton;

use Exception;
use Gerfey\LaravelDomainSkeleton\Storage\StorageDomainSkeleton;

class LaravelDomainSkeleton
{
/**
* @var
*/
protected $domainName;

/**
* @param string $domainName
*/
public function setNameDomain(string $domainName): void
{
$this->domainName = $domainName;
}

/**
* @throws Exception
*/
public function make(): void
{
$storage = new StorageDomainSkeleton();
try {
$storage->createStructure($this->domainName);
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
}
17 changes: 11 additions & 6 deletions src/LaravelDomainSkeletonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ class LaravelDomainSkeletonServiceProvider extends ServiceProvider
public function boot()
{
if ($this->app->runningInConsole()) {
$this->commands([
LaravelDomainSkeletonCommand::class
]);
$this->commands(
[
LaravelDomainSkeletonCommand::class
]
);
}
}

Expand All @@ -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'
);
}
}
}
67 changes: 67 additions & 0 deletions src/Storage/StorageDomainSkeleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Gerfey\LaravelDomainSkeleton\Storage;

use Exception;
use Gerfey\LaravelDomainSkeleton\Storage\Structures\DirectoryStructure;
use Gerfey\LaravelDomainSkeleton\Storage\Structures\FileStructure;

class StorageDomainSkeleton
{
/**
* @var string
*/
private $structurePath = __DIR__ . '/../../config/structure.php';

/**
* @param string $domainName
* @throws Exception
*/
public function createStructure(string $domainName)
{
$pathDomainName = config('default_directory.domain') . '/' . $domainName;
$pathDirectoryDomain = app_path($pathDomainName);

if (!file_exists($pathDirectoryDomain)) {
if (!$this->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;
}
}
54 changes: 54 additions & 0 deletions src/Storage/Structures/DirectoryStructure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Gerfey\LaravelDomainSkeleton\Storage\Structures;

use Exception;

class DirectoryStructure extends Structure
{
/**
* @var mixed
*/
protected $children;

/**
* @param array $params
* @param string $domainName
*/
public function __construct(array $params, string $domainName)
{
parent::__construct($params, $domainName);

$this->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;
}
}
Loading

0 comments on commit 5bf4d02

Please sign in to comment.