Skip to content

Commit

Permalink
Added ability to create resources, jobs, exceptions repositories(with…
Browse files Browse the repository at this point in the history
… default CRUDs) and services,

Fixed problem with generating a model (stub not found)
Fixed problem with generating migrations when creating models,
"routes" has been capitalized, to enforce consistency
  • Loading branch information
Julius-Bendt committed Jan 31, 2022
1 parent 007d0f9 commit 2972d8f
Show file tree
Hide file tree
Showing 19 changed files with 817 additions and 62 deletions.
32 changes: 18 additions & 14 deletions src/Console/DomainControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function getStub()

if ($this->option('api') && is_null($stub)) {
$stub = '/stubs/controller.api.stub';
} elseif ($this->option('api') && ! is_null($stub) && ! $this->option('invokable')) {
} elseif ($this->option('api') && !is_null($stub) && !$this->option('invokable')) {
$stub = str_replace('.stub', '.api.stub', $stub);
}

Expand All @@ -76,7 +76,7 @@ protected function getStub()
*/
protected function resolveStubPath($stub)
{
$localPath = __DIR__ . '/..'. $stub;
$localPath = __DIR__ . '/..' . $stub;
$publishedPath = $this->laravel->basePath(trim($stub, '/'));
return file_exists($publishedPath)
? $publishedPath
Expand Down Expand Up @@ -140,7 +140,9 @@ protected function buildClass($name)
$replace["use {$controllerNamespace}\Controller;\n"] = '';

return str_replace(
array_keys($replace), array_values($replace), parent::buildClass($name)
array_keys($replace),
array_values($replace),
parent::buildClass($name)
);
}

Expand All @@ -153,7 +155,7 @@ protected function buildParentReplacements()
{
$parentModelClass = $this->parseModel($this->option('parent'));

if (! class_exists($parentModelClass)) {
if (!class_exists($parentModelClass)) {
if ($this->confirm("A {$parentModelClass} model does not exist. Do you want to generate it?", true)) {
$this->call('domain:make:model', ['name' => $parentModelClass]);
}
Expand Down Expand Up @@ -182,7 +184,7 @@ protected function buildModelReplacements(array $replace)
{
$modelClass = $this->parseModel($this->option('model'));

if (! class_exists($modelClass)) {
if (!class_exists($modelClass)) {
if ($this->confirm("A {$modelClass} model does not exist. Do you want to generate it?", true)) {
$this->call('domain:make:model', ['domain' => $this->domain, 'name' => $modelClass]);
}
Expand Down Expand Up @@ -237,25 +239,27 @@ protected function buildFormRequestReplacements(array $replace, $modelClass)
$namespace = 'App\\Http\\Requests';

[$storeRequestClass, $updateRequestClass] = $this->generateFormRequests(
$modelClass, $storeRequestClass, $updateRequestClass
$modelClass,
$storeRequestClass,
$updateRequestClass
);
}

$namespacedRequests = $namespace.'\\'.$storeRequestClass.';';
$namespacedRequests = $namespace . '\\' . $storeRequestClass . ';';

if ($storeRequestClass !== $updateRequestClass) {
$namespacedRequests .= PHP_EOL.'use '.$namespace.'\\'.$updateRequestClass.';';
$namespacedRequests .= PHP_EOL . 'use ' . $namespace . '\\' . $updateRequestClass . ';';
}

return array_merge($replace, [
'{{ storeRequest }}' => $storeRequestClass,
'{{storeRequest}}' => $storeRequestClass,
'{{ updateRequest }}' => $updateRequestClass,
'{{updateRequest}}' => $updateRequestClass,
'{{ namespacedStoreRequest }}' => $namespace.'\\'.$storeRequestClass,
'{{namespacedStoreRequest}}' => $namespace.'\\'.$storeRequestClass,
'{{ namespacedUpdateRequest }}' => $namespace.'\\'.$updateRequestClass,
'{{namespacedUpdateRequest}}' => $namespace.'\\'.$updateRequestClass,
'{{ namespacedStoreRequest }}' => $namespace . '\\' . $storeRequestClass,
'{{namespacedStoreRequest}}' => $namespace . '\\' . $storeRequestClass,
'{{ namespacedUpdateRequest }}' => $namespace . '\\' . $updateRequestClass,
'{{namespacedUpdateRequest}}' => $namespace . '\\' . $updateRequestClass,
'{{ namespacedRequests }}' => $namespacedRequests,
'{{namespacedRequests}}' => $namespacedRequests,
]);
Expand All @@ -271,14 +275,14 @@ protected function buildFormRequestReplacements(array $replace, $modelClass)
*/
protected function generateFormRequests($modelClass, $storeRequestClass, $updateRequestClass)
{
$storeRequestClass = 'Store'.class_basename($modelClass).'Request';
$storeRequestClass = 'Store' . class_basename($modelClass) . 'Request';

$this->call('domain:make:request', [
'name' => $storeRequestClass,
'domain' => $this->domain
]);

$updateRequestClass = 'Update'.class_basename($modelClass).'Request';
$updateRequestClass = 'Update' . class_basename($modelClass) . 'Request';

$this->call('domain:make:request', [
'name' => $updateRequestClass,
Expand Down
122 changes: 122 additions & 0 deletions src/Console/DomainExceptionMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace PhpSquad\DomainMaker\Console;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Illuminate\Support\Str;



/// Modified version of:
/// https://github.com/laravel/framework/blob/8.x/src/Illuminate/Foundation/Console/ExceptionMakeCommand.php
class DomainExceptionMakeCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'domain:make:exception';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new custom exception class for a domain';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Exception';

/**
* The domain to create the exception in
*
* @var string
*/
protected $domain;

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
if ($this->option('render')) {
return $this->option('report')
? $this->resolveStubPath('/stubs/exception-render-report.stub')
: $this->resolveStubPath('/stubs/exception-render.stub');
}

return $this->option('report')
? $this->resolveStubPath('/stubs/exception-report.stub')
: $this->resolveStubPath('/stubs/exception.stub');
}


/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
$localPath = __DIR__ . '/..' . $stub;
$publishedPath = $this->laravel->basePath(trim($stub, '/'));
return file_exists($publishedPath)
? $publishedPath
: $localPath;
}

/**
* Determine if the class already exists.
*
* @param string $rawName
* @return bool
*/
protected function alreadyExists($rawName)
{
return class_exists($this->rootNamespace() . 'Exceptions\\' . $rawName);
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
$this->domain = Str::studly($this->argument('domain'));
return $rootNamespace . '\Domains\\' . $this->domain . '\\Exceptions';
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['render', null, InputOption::VALUE_NONE, 'Create the exception with an empty render method'],

['report', null, InputOption::VALUE_NONE, 'Create the exception with an empty report method'],
];
}

protected function getArguments()
{
return [
['domain', InputArgument::REQUIRED, 'The domain of the class'],
['name', InputArgument::REQUIRED, 'The name of the class'],
];
}
}
26 changes: 15 additions & 11 deletions src/Console/DomainFactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ protected function getStub()
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
$localPath = __DIR__ . '/..' . $stub;
$publishedPath = $this->laravel->basePath(trim($stub, '/'));
return file_exists($publishedPath)
? $publishedPath
: $localPath;
}

/**
Expand All @@ -62,13 +64,13 @@ protected function buildClass($name)
$factory = class_basename(Str::ucfirst(str_replace('Factory', '', $name)));

$namespaceModel = $this->option('model')
? $this->qualifyModel($this->option('model'))
: $this->qualifyModel($this->guessModelName($name));
? $this->qualifyModel($this->option('model'))
: $this->qualifyModel($this->guessModelName($name));

$model = class_basename($namespaceModel);

if (Str::startsWith($namespaceModel, $this->rootNamespace().'Models')) {
$namespace = Str::beforeLast('Database\\Factories\\'.Str::after($namespaceModel, $this->rootNamespace().'Models\\'), '\\');
if (Str::startsWith($namespaceModel, $this->rootNamespace() . 'Models')) {
$namespace = Str::beforeLast('Database\\Factories\\' . Str::after($namespaceModel, $this->rootNamespace() . 'Models\\'), '\\');
} else {
$namespace = 'Database\\Factories';
}
Expand All @@ -86,7 +88,9 @@ protected function buildClass($name)
];

return str_replace(
array_keys($replace), array_values($replace), parent::buildClass($name)
array_keys($replace),
array_values($replace),
parent::buildClass($name)
);
}

Expand All @@ -100,7 +104,7 @@ protected function getPath($name)
{
$name = (string) Str::of($name)->replaceFirst($this->rootNamespace(), '')->finish('Factory');

return $this->laravel->databasePath().'/factories/'.str_replace('\\', '/', $name).'.php';
return $this->laravel->databasePath() . '/factories/' . str_replace('\\', '/', $name) . '.php';
}

/**
Expand All @@ -122,10 +126,10 @@ protected function guessModelName($name)
}

if (is_dir(app_path('Models/'))) {
return $this->rootNamespace().'Models\Model';
return $this->rootNamespace() . 'Models\Model';
}

return $this->rootNamespace().'Model';
return $this->rootNamespace() . 'Model';
}

/**
Expand Down
97 changes: 97 additions & 0 deletions src/Console/DomainJobMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace PhpSquad\DomainMaker\Console;

use Illuminate\Console\Concerns\CreatesMatchingTest;
use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Illuminate\Support\Str;

/// Modified version of:
/// https://github.com/laravel/framework/blob/8.x/src/Illuminate/Foundation/Console/JobMakeCommand.php
class DomainJobMakeCommand extends GeneratorCommand
{
use CreatesMatchingTest;

/**
* The console command name.
*
* @var string
*/
protected $name = 'domain:make:job';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new job class';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Job';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return $this->option('sync')
? $this->resolveStubPath('/stubs/job.stub')
: $this->resolveStubPath('/stubs/job.queued.stub');
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
$localPath = __DIR__ . '/..' . $stub;
$publishedPath = $this->laravel->basePath(trim($stub, '/'));
return file_exists($publishedPath)
? $publishedPath
: $localPath;
}


/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
$this->domain = Str::studly($this->argument('domain'));
return $rootNamespace . '\Domains\\' . $this->domain . '\\Jobs';
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['sync', null, InputOption::VALUE_NONE, 'Indicates that job should be synchronous'],
];
}

protected function getArguments()
{
return [
['domain', InputArgument::REQUIRED, 'The domain of the class'],
['name', InputArgument::REQUIRED, 'The name of the class'],
];
}
}
Loading

0 comments on commit 2972d8f

Please sign in to comment.