Skip to content

Commit

Permalink
Update make commands
Browse files Browse the repository at this point in the history
  • Loading branch information
nguereza-tony committed Sep 17, 2023
1 parent 471dff1 commit 67d5d0b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/Console/Command/MakeEntityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ public function getClassTemplate(): string
/**
* @class %classname%
* @package %namespace%
* @extends Entity<%classname%>
*/
class %classname% extends Entity
{
/**
* {@inheritdoc}
* @param EntityMapperInterface<%classname%> \$mapper
* @return void
*/
public static function mapEntity(EntityMapperInterface \$mapper): void
{
Expand Down
11 changes: 8 additions & 3 deletions src/Console/Command/MakeFormParamCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public function getClassTemplate(): string
/**
* @class %classname%
* @package %namespace%
* %template_entity%
*/
class %classname% extends BaseParam
{
Expand Down Expand Up @@ -278,10 +279,12 @@ public function $getterName(): string
protected function getFromEntityBody(string $content): string
{
$result = '';
$templateEntity = '@template TEntity as Entity';
if ($this->createInstanceFormEntity && !empty($this->fromEntityMaps)) {
$result = <<<EOF
/**
* {@inheritdoc}
* @param TEntity \$entity
* @return \$this
*/
public function fromEntity(Entity \$entity): self
{
Expand All @@ -301,8 +304,10 @@ public function fromEntity(Entity \$entity): self
EOF;
}

return str_replace('%from_entity%', $result, $content);

$template = (string) str_replace('%from_entity%', $result, $content);

return str_replace('%template_entity%', $templateEntity, $template);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Console/Command/MakeRepositoryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,14 @@ public function getClassTemplate(): string
/**
* @class %classname%
* @package %namespace%
* @extends Repository<%entity_base_class%>
*/
class %classname% extends Repository
{
/**
* Create new instance
* @param EntityManager \$manager
* @param EntityManager<%entity_base_class%> \$manager
*/
public function __construct(EntityManager \$manager)
{
Expand Down Expand Up @@ -164,7 +165,9 @@ protected function getUsesContent(): string
protected function getEntityBody(string $content): string
{
$entityName = basename($this->entityClass) . '::class';
$entityBaseName = $this->getClassBaseName($this->entityClass);

return str_replace('%entity_class%', $entityName, $content);
$template = str_replace('%entity_base_class%', $entityBaseName, $content);
return str_replace('%entity_class%', $entityName, $template);
}
}
16 changes: 16 additions & 0 deletions src/Console/MakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ public function interact(Reader $reader, Writer $writer): void

$this->className = $name;
}

/**
* Return the base class name
* @param string|object $fullClassName
* @return string
*/
public function getClassBaseName($fullClassName): string
{
if (is_object($fullClassName)) {
$fullClassName = get_class($fullClassName);
}

$temp = explode('\\', $fullClassName);

return end($temp);
}

/**
* Return the the class template
Expand Down
5 changes: 5 additions & 0 deletions tests/Console/Command/MakeActionCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Platine\Framework\App\Application;
use Platine\Framework\Console\Command\MakeActionCommand;
use Platine\Test\Framework\Console\BaseCommandTestCase;
use stdClass;

/*
* @group core
Expand Down Expand Up @@ -47,6 +48,10 @@ public function testExecuteDefault(): void
$o->bind($consoleApp);
$o->parse(['platine', $actionName]);
$this->assertEquals('make:action', $o->getName());

// Only to coverage tests
$this->assertEquals('MakeActionCommandTest', $o->getClassBaseName($this));
$this->assertEquals(stdClass::class, $o->getClassBaseName(stdClass::class));

$o->interact($reader, $writer);
$o->execute();
Expand Down

0 comments on commit 67d5d0b

Please sign in to comment.