Skip to content

Commit

Permalink
printers added for model and model collection factories
Browse files Browse the repository at this point in the history
  • Loading branch information
spider-mane committed Jun 22, 2022
1 parent 2f45ddf commit 0d6a5d0
Show file tree
Hide file tree
Showing 18 changed files with 313 additions and 20 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"nesbot/carbon": "^2.54",
"nette/php-generator": "^3.6 || ^4.0",
"panamax/panamax": "@stable",
"php-parallel-lint/php-console-highlighter": "^1.0",
"psr/cache": "^1.0 || ^2.0 || ^3.0",
"psr/container": "^1.0 || ^2.0",
"psr/event-dispatcher": "^1.0",
Expand Down Expand Up @@ -72,7 +73,6 @@
"opis/closure": "^3.6",
"oscarotero/env": "^2.1",
"php-http/guzzle7-adapter": "^1.0",
"php-parallel-lint/php-console-highlighter": "^1.0",
"phpbench/phpbench": "@dev",
"phpmailer/phpmailer": "^6.6",
"phpoption/phpoption": "^1.8",
Expand Down
11 changes: 6 additions & 5 deletions hoplite.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
hook:
path: src/Leonidas/Hooks
make:
hook:
path: src/Leonidas/Hooks

model:
interfaces: src/Contracts/System/Model
classes: src/Library/System/Model
model:
interfaces: src/Contracts/System/Model
classes: src/Library/System/Model

util:
method:
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/Make/HookCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$type = $input->getArgument('type');
$converted = $this->convert($tag)->toPascal();

$parts = explode('/', $config['hook']['path']);
$parts = explode('/', $config['make']['hook']['path']);
$root = array_shift($parts);
$namespace = implode('\\', $parts);
$dir = $root . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, array_slice($parts, 1));
Expand Down
46 changes: 44 additions & 2 deletions src/Console/Command/Make/ModelCollectionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Leonidas\Console\Command\Make;

use Leonidas\Console\Command\HopliteCommand;
use Leonidas\Console\Library\ModelCollectionFactoryPrinter;
use Leonidas\Console\Library\ModelCollectionsFactory;
use Leonidas\Console\Library\ModelConverterPrinter;
use Leonidas\Console\Library\ModelInterfacePrinter;
use Leonidas\Console\Library\ModelPrinter;
use Leonidas\Console\Library\ModelQueryFactoryPrinter;
use Leonidas\Console\Library\ModelRepositoryInterfacePrinter;
use Leonidas\Console\Library\ModelRepositoryPrinter;
use Leonidas\Contracts\System\Model\Post\PostCollectionInterface;
Expand Down Expand Up @@ -33,11 +36,51 @@ protected function execute(InputInterface $input, OutputInterface $output): int

protected function runTests(): void
{
$playground = getcwd() . '/.playground/factory';
$playground = getcwd() . '/.playground/model';

if (!is_dir($playground)) {
mkdir($playground, 0777, true);
}

$this->testCollection($playground);
$this->testRepository($playground);
$this->testModel($playground);
$this->testFactories($playground);
}

protected function testFactories(string $playground): void
{
$namespace = 'Leonidas\Library\System\Model\Post';

$collection = new ModelCollectionFactoryPrinter(
$namespace,
'PostCollectionFactory',
$namespace . '\\' . 'PostCollection'
);

$query = new ModelQueryFactoryPrinter(
$namespace,
'PostQueryFactory',
$namespace . '\\' . 'PostQuery',
'post'
);

$model = new ModelConverterPrinter(
$namespace,
'PostConverter',
'Leonidas\Library\System\Model\Post\Post',
'Leonidas\Contracts\System\Model\Post\PostInterface',
'user'
);

$this->printPhp($collection = $collection->printFile());
file_put_contents($playground . '/factory-collection.php', $collection);

$this->printPhp($query = $query->printFile());
file_put_contents($playground . '/factory-query.php', $query);

$this->printPhp($model = $model->printFile());
file_put_contents($playground . '/factory-model.php', $model);
}

protected function testModel(string $playground): void
Expand Down Expand Up @@ -121,7 +164,6 @@ protected function testCollection(string $playground): void
$file = $playground . '/collection-' . $class . '.php';

$this->printPhp($code);

file_put_contents($file, $code);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Library/Abstracts/AbstractClassPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,5 @@ protected function finishClass($class): void
/**
* @return ClassType|TraitType|InterfaceType
*/
abstract protected function setupClass(PhpNamespace $namespace);
abstract protected function setupClass(PhpNamespace $namespace): object;
}
2 changes: 1 addition & 1 deletion src/Console/Library/ModelCollectionAbstractPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class ModelCollectionAbstractPrinter extends AbstractCompleteModelCollectionPrinter
{
protected function setupClass(PhpNamespace $namespace)
protected function setupClass(PhpNamespace $namespace): object
{
$base = AbstractModelCollection::class;

Expand Down
2 changes: 1 addition & 1 deletion src/Console/Library/ModelCollectionAsChildPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(
$this->type = $type;
}

protected function setupClass(PhpNamespace $namespace)
protected function setupClass(PhpNamespace $namespace): object
{
$util = PoweredByModelCollectionKernelTrait::class;

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

namespace Leonidas\Console\Library;

use Leonidas\Console\Library\Abstracts\AbstractClassPrinter;
use Leonidas\Contracts\System\Schema\EntityCollectionFactoryInterface;
use Nette\PhpGenerator\PhpNamespace;

class ModelCollectionFactoryPrinter extends AbstractClassPrinter
{
protected string $collection;

public function __construct(string $namespace, string $class, string $collection)
{
parent::__construct($namespace, $class);
$this->collection = $collection;
}

protected function setupClass(PhpNamespace $namespace): object
{
$base = EntityCollectionFactoryInterface::class;
$return = explode('\\', $this->collection);
$return = end($return);

$class = $namespace->addUse($base)->addClass($this->class);

$class->addImplement($base);
$class->addMethod('createEntityCollection')
->setVariadic(true)
->setReturnType($this->collection)
->setBody(sprintf('return new %s(...$entities);', $return))
->addParameter('entities')
->setType('object');

return $class;
}
}
2 changes: 1 addition & 1 deletion src/Console/Library/ModelCollectionInterfacePrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class ModelCollectionInterfacePrinter extends AbstractModelCollectionPrinter
{
protected function setupClass(PhpNamespace $namespace)
protected function setupClass(PhpNamespace $namespace): object
{
return $namespace->addUse($this->model)->addInterface($this->class);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Library/ModelCollectionPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class ModelCollectionPrinter extends AbstractCompleteModelCollectionPrinter
{
protected function setupClass(PhpNamespace $namespace)
protected function setupClass(PhpNamespace $namespace): object
{
$base = AbstractModelCollection::class;
$util = PoweredByModelCollectionKernelTrait::class;
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Library/ModelCollectionTraitPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class ModelCollectionTraitPrinter extends AbstractCompleteModelCollectionPrinter
{
protected function setupClass(PhpNamespace $namespace)
protected function setupClass(PhpNamespace $namespace): object
{
return $this->buildDefaultNamespace($namespace)->addTrait($this->class);
}
Expand Down
132 changes: 132 additions & 0 deletions src/Console/Library/ModelConverterPrinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace Leonidas\Console\Library;

use Leonidas\Console\Library\Abstracts\AbstractClassPrinter;
use Leonidas\Contracts\System\Schema\Comment\CommentConverterInterface;
use Leonidas\Contracts\System\Schema\Post\PostConverterInterface;
use Leonidas\Contracts\System\Schema\Term\TermConverterInterface;
use Leonidas\Contracts\System\Schema\User\UserConverterInterface;
use Leonidas\Library\System\Model\Abstracts\AbstractModelConverter;
use Leonidas\Library\System\Schema\Exceptions\UnexpectedEntityException;
use Nette\PhpGenerator\PhpNamespace;
use WP_Comment;
use WP_Post;
use WP_Term;
use WP_User;

class ModelConverterPrinter extends AbstractClassPrinter
{
public const TEMPLATES = [
'post' => WP_Post::class,
'post:h' => WP_Post::class,
'attachment' => WP_Post::class,
'term' => WP_Term::class,
'term:h' => WP_Term::class,
'user' => WP_User::class,
'comment' => WP_Comment::class,
];

public const CORES = [
'post' => 'post',
'post:h' => 'post',
'attachment' => 'post',
'term' => 'term',
'term:h' => 'term',
'user' => 'user',
'comment' => 'comment',
];

public const CONVERTERS = [
'post' => PostConverterInterface::class,
'post:h' => PostConverterInterface::class,
'attachment' => PostConverterInterface::class,
'term' => TermConverterInterface::class,
'term:h' => TermConverterInterface::class,
'user' => UserConverterInterface::class,
'comment' => CommentConverterInterface::class,
];

public const FUNCTIONS = [
'post' => 'get_post(%s)',
'post:h' => 'get_post(%s)',
'attachment' => 'get_post(%s)',
'term' => 'get_term(%s)',
'term:h' => 'get_term(%s)',
'user' => 'get_user_by(\'id\', %s)',
'comment' => 'get_comment(%s)',
];

protected string $model;

protected string $contract;

protected string $template;

public function __construct(string $namespace, string $class, string $model, string $contract, string $template)
{
parent::__construct($namespace, $class);

$this->model = $model;
$this->contract = $contract;
$this->template = $template;
}

protected function setupClass(PhpNamespace $namespace): object
{
$base = AbstractModelConverter::class;
$error = UnexpectedEntityException::class;
$converter = static::CONVERTERS[$this->template];
$template = static::TEMPLATES[$this->template];
$core = static::CORES[$this->template];
$function = static::FUNCTIONS[$this->template];

$class = $namespace
->addUse($base)
->addUse($error)
->addUse($converter)
->addUse($template)
->addUse($this->contract)
->addClass($this->class)
->setExtends($base)
->addImplement($converter);

$model = explode('\\', $this->model);
$model = end($model);

$class->addMethod('convert')
->setPublic()
->setReturnType($this->contract)
->addBody(sprintf(
'return new %s($%s, $this->autoInvoker);',
$model,
$core
))
->addParameter($core)
->setType($template);

$contract = explode('\\', $this->contract);
$contract = end($contract);

$throw = explode('\\', $error);
$throw = end($throw);

$call = sprintf($function, '$' . $core . '->getId()');

$class->addMethod('revert')
->setPublic()
->setReturnType($template)
->addBody(sprintf('if ($%s instanceof %s) {', $core, $contract))
->addBody(sprintf(' return %s;', $call))
->addBody("} \n")
->addBody(sprintf('throw new %s(', $throw))
->addBody(sprintf(' %s::class,', $contract))
->addBody(sprintf(' $%s,', $core))
->addBody(' __METHOD__')
->addBody(');')
->addParameter($core)
->setType('object');

return $class;
}
}
2 changes: 1 addition & 1 deletion src/Console/Library/ModelInterfacePrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(string $namespace, string $class, string $template =
$this->template = $template;
}

protected function setupClass(PhpNamespace $namespace)
protected function setupClass(PhpNamespace $namespace): object
{
$interface = $namespace->addInterface($this->class);

Expand Down
2 changes: 1 addition & 1 deletion src/Console/Library/ModelPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function __construct(
$this->template = $template;
}

protected function setupClass(PhpNamespace $namespace)
protected function setupClass(PhpNamespace $namespace): object
{
$accessTrait = AllAccessGrantedTrait::class;
$lazyLoadTrait = LazyLoadableRelationshipsTrait::class;
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Library/ModelQueryAsChildPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function __construct(
$this->template = $template;
}

protected function setupClass(PhpNamespace $namespace)
protected function setupClass(PhpNamespace $namespace): object
{
$engine = static::ENGINES[$this->template];
$converter = static::CONVERTERS[$this->template];
Expand Down

0 comments on commit 0d6a5d0

Please sign in to comment.