Skip to content

Commit

Permalink
Merge branch 'master' into feature/fixes-documentation
Browse files Browse the repository at this point in the history
 Conflicts:
	README.md
	docs/004_config.md
  • Loading branch information
Chemaclass committed Apr 18, 2021
2 parents cc4f208 + 9838f75 commit 84e67c2
Show file tree
Hide file tree
Showing 30 changed files with 494 additions and 147 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/.vscode/
/vendor/
.phpunit.*
.php_cs.cache
.php_cs.cache
11 changes: 1 addition & 10 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,22 @@ $finder = PhpCsFixer\Finder::create()
return PhpCsFixer\Config::create()
->setFinder($finder)
->setRules([
'@PSR2' => true,
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'blank_line_after_opening_tag' => true,
'braces' => ['allow_single_line_closure' => true],
'compact_nullable_typehint' => true,
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'declare_strict_types' => true,
'final_class' => true,
'function_typehint_space' => true,
'header_comment' => ['header' => ''],
'list_syntax' => ['syntax' => 'short'],
'new_with_braces' => true,
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
'no_empty_phpdoc' => true,
'no_empty_statement' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_whitespace_before_comma_in_array' => true,
'no_unused_imports' => true,
'no_whitespace_in_blank_line' => true,
'normalize_index_brace' => true,
'ordered_imports' => true,
'php_unit_method_casing' => ['case' => 'snake_case'],
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_annotation_without_dot' => true,
Expand All @@ -45,7 +37,6 @@ return PhpCsFixer\Config::create()
'phpdoc_types' => true,
'phpdoc_var_annotation_correct_order' => true,
'phpdoc_var_without_name' => true,
'return_type_declaration' => ['space_before' => 'none'],
'single_trait_insert_per_statement' => true,
'single_quote' => true,
'trailing_comma_in_multiline_array' => true,
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
}
],
"require": {
"ext-json": "*",
"php": ">=7.4"
},
"require-dev": {
Expand All @@ -39,14 +40,16 @@
"scripts": {
"test-all": [
"@test-quality",
"@test-unit",
"@test-integration"
],
"test-quality": [
"@csrun",
"@psalm"
],
"test-unit": "./vendor/bin/phpunit --testsuite=unit",
"test-integration": "./vendor/bin/phpunit --testsuite=integration",
"test-coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --testsuite=integration --coverage-html=coverage",
"test-coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --testsuite=unit,integration --coverage-html=coverage",
"psalm": "./vendor/bin/psalm",
"csfix": "./vendor/bin/php-cs-fixer fix --allow-risky=yes",
"csrun": "./vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run"
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
</php>

<testsuites>
<testsuite name="unit">
<directory suffix="Test.php">tests/Unit</directory>
</testsuite>

<testsuite name="integration">
<directory suffix="Test.php">tests/Integration</directory>
</testsuite>
Expand Down
47 changes: 47 additions & 0 deletions src/CodeGenerator/CodeGeneratorConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Gacela\CodeGenerator;

use Gacela\Framework\AbstractConfig;
use Gacela\Framework\Config;
use LogicException;

final class CodeGeneratorConfig extends AbstractConfig
{
public function getFacadeMakerTemplate(): string
{
return $this->getCommandTemplateContent('facade-maker.txt');
}

public function getFactoryMakerTemplate(): string
{
return $this->getCommandTemplateContent('factory-maker.txt');
}

public function getConfigMakerTemplate(): string
{
return $this->getCommandTemplateContent('config-maker.txt');
}

public function getDependencyProviderMakerTemplate(): string
{
return $this->getCommandTemplateContent('dependency-provider-maker.txt');
}

private function getCommandTemplateContent(string $filename): string
{
return file_get_contents(__DIR__ . '/Infrastructure/Template/Command/' . $filename);
}

public function getComposerJsonContentAsArray(): array
{
$filename = Config::getApplicationRootDir() . '/composer.json';
if (!file_exists($filename)) {
throw new LogicException('composer.json file not found but it is required');
}

return json_decode(file_get_contents($filename), true);
}
}
14 changes: 4 additions & 10 deletions src/CodeGenerator/CodeGeneratorFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,11 @@ final class CodeGeneratorFacade extends AbstractFacade
*/
public function runCommand(string $commandName, array $arguments = []): void
{
[$rootNamespace, $targetDirectory] = array_pad($arguments, 2, null);
$commandArguments = $this->getFactory()
->createCommandArgumentsParser()
->parse($arguments);

if ($rootNamespace === null) {
throw new InvalidArgumentException('Expected 1st argument to be root-namespace of the project');
}

if ($targetDirectory === null) {
throw new InvalidArgumentException('Expected 2nd argument to be target-directory inside the project');
}

$this->createMaker($commandName)->make($rootNamespace, $targetDirectory);
$this->createMaker($commandName)->make($commandArguments);
}

private function createMaker(string $commandName): MakerInterface
Expand Down
23 changes: 19 additions & 4 deletions src/CodeGenerator/CodeGeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
use Gacela\CodeGenerator\Domain\Command\FacadeMaker;
use Gacela\CodeGenerator\Domain\Command\FactoryMaker;
use Gacela\CodeGenerator\Domain\Command\ModuleMaker;
use Gacela\CodeGenerator\Domain\Io\CommandArguments\CommandArgumentsParser;
use Gacela\CodeGenerator\Domain\Io\MakerIoInterface;
use Gacela\CodeGenerator\Infrastructure\Io\SystemMakerIo;
use Gacela\Framework\AbstractFactory;

/**
* @method CodeGeneratorConfig getConfig()
*/
final class CodeGeneratorFactory extends AbstractFactory
{
public function createModuleMaker(): ModuleMaker
Expand All @@ -31,33 +35,44 @@ public function createModuleMaker(): ModuleMaker
public function createFacadeMaker(): FacadeMaker
{
return new FacadeMaker(
$this->createGeneratorIo()
$this->createGeneratorIo(),
$this->getConfig()->getFacadeMakerTemplate()
);
}

public function createFactoryMaker(): FactoryMaker
{
return new FactoryMaker(
$this->createGeneratorIo()
$this->createGeneratorIo(),
$this->getConfig()->getFactoryMakerTemplate()
);
}

public function createConfigMaker(): ConfigMaker
{
return new ConfigMaker(
$this->createGeneratorIo()
$this->createGeneratorIo(),
$this->getConfig()->getConfigMakerTemplate()
);
}

public function createDependencyProviderMaker(): DependencyProviderMaker
{
return new DependencyProviderMaker(
$this->createGeneratorIo()
$this->createGeneratorIo(),
$this->getConfig()->getDependencyProviderMakerTemplate()
);
}

private function createGeneratorIo(): MakerIoInterface
{
return new SystemMakerIo();
}

public function createCommandArgumentsParser(): CommandArgumentsParser
{
return new CommandArgumentsParser(
$this->getConfig()->getComposerJsonContentAsArray()
);
}
}
26 changes: 16 additions & 10 deletions src/CodeGenerator/Domain/Command/AbstractMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,36 @@
namespace Gacela\CodeGenerator\Domain\Command;

use Gacela\CodeGenerator\Domain\Io\MakerIoInterface;
use Gacela\CodeGenerator\Domain\ReadModel\CommandArguments;

abstract class AbstractMaker implements MakerInterface
{
private MakerIoInterface $io;
private string $template;

public function __construct(MakerIoInterface $io)
public function __construct(MakerIoInterface $io, string $template)
{
$this->template = $template;
$this->io = $io;
}

public function make(string $rootNamespace, string $targetDirectory): void
public function make(CommandArguments $commandArguments): void
{
$pieces = explode('/', $targetDirectory);
$moduleName = end($pieces);
$this->io->createDirectory($commandArguments->directory());

$this->io->createDirectory($targetDirectory);

$path = sprintf('%s/%s.php', $targetDirectory, $this->className());
$this->io->filePutContents($path, $this->generateFileContent("$rootNamespace\\$moduleName"));
$path = sprintf('%s/%s.php', $commandArguments->directory(), $this->className());
$this->io->filePutContents($path, $this->generateFileContent($commandArguments->namespace()));

$this->io->writeln("> Path '$path' created successfully");
}

abstract protected function generateFileContent(string $namespace): string;

abstract protected function className(): string;

private function generateFileContent(string $namespace): string
{
$search = ['$NAMESPACE$', '$CLASS_NAME$'];
$replace = [$namespace, $this->className()];

return str_replace($search, $replace, $this->template);
}
}
18 changes: 0 additions & 18 deletions src/CodeGenerator/Domain/Command/ConfigMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,6 @@

final class ConfigMaker extends AbstractMaker
{
protected function generateFileContent(string $namespace): string
{
return <<<TEXT
<?php
declare(strict_types=1);
namespace {$namespace};
use Gacela\Framework\AbstractConfig;
final class {$this->className()} extends AbstractConfig
{
}
TEXT;
}

protected function className(): string
{
return 'Config';
Expand Down
22 changes: 0 additions & 22 deletions src/CodeGenerator/Domain/Command/DependencyProviderMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,6 @@

final class DependencyProviderMaker extends AbstractMaker
{
protected function generateFileContent(string $namespace): string
{
return <<<TEXT
<?php
declare(strict_types=1);
namespace {$namespace};
use Gacela\Framework\AbstractDependencyProvider;
use Gacela\Framework\Container\Container;
final class {$this->className()} extends AbstractDependencyProvider
{
public function provideModuleDependencies(Container \$container): void
{
}
}
TEXT;
}

protected function className(): string
{
return 'DependencyProvider';
Expand Down
21 changes: 0 additions & 21 deletions src/CodeGenerator/Domain/Command/FacadeMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,6 @@

final class FacadeMaker extends AbstractMaker
{
protected function generateFileContent(string $namespace): string
{
return <<<TEXT
<?php
declare(strict_types=1);
namespace {$namespace};
use Gacela\Framework\AbstractFacade;
/**
* @method Factory getFactory()
*/
final class {$this->className()} extends AbstractFacade
{
}
TEXT;
}

protected function className(): string
{
return 'Facade';
Expand Down
21 changes: 0 additions & 21 deletions src/CodeGenerator/Domain/Command/FactoryMaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,6 @@

final class FactoryMaker extends AbstractMaker
{
protected function generateFileContent(string $namespace): string
{
return <<<TEXT
<?php
declare(strict_types=1);
namespace {$namespace};
use Gacela\Framework\AbstractFactory;
/**
* @method Config getConfig()
*/
final class {$this->className()} extends AbstractFactory
{
}
TEXT;
}

protected function className(): string
{
return 'Factory';
Expand Down
4 changes: 3 additions & 1 deletion src/CodeGenerator/Domain/Command/MakerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Gacela\CodeGenerator\Domain\Command;

use Gacela\CodeGenerator\Domain\ReadModel\CommandArguments;

interface MakerInterface
{
public function make(string $rootNamespace, string $targetDirectory): void;
public function make(CommandArguments $commandArguments): void;
}
Loading

0 comments on commit 84e67c2

Please sign in to comment.