Skip to content

Maker Commands - Resource #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,23 @@ Options:
--include-query-bus Add a query bus dependency
--include-command-bus Add a command bus dependency
```

### Resource

This command can be used to generate an [Api Platform](https://api-platform.com/) resource. Minimum required version is [2.7](https://api-platform.com/docs/core/upgrade-guide/#api-platform-2730) for the PHP attributes support.

#### Command Output

```bash
Description:
Creates a new API Platform resource

Usage:
make:ddd:resource [options] [--] [<name>]

Arguments:
name The name of the model class to create the resource for (e.g. Customer). Model must exist already.

Options:
--config Config flavor to create (attribute|xml).
```
12 changes: 12 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ services:
tags:
- { name: maker.command }

GeekCell\DddBundle\Maker\MakeResource:
class: GeekCell\DddBundle\Maker\MakeResource
arguments:
- '@maker.file_manager'
- '@GeekCell\DddBundle\Maker\Doctrine\ApiPlatformConfigUpdator'
tags:
- { name: maker.command }

GeekCell\DddBundle\Maker\Doctrine\DoctrineConfigUpdater:
class: GeekCell\DddBundle\Maker\Doctrine\DoctrineConfigUpdater
public: false

GeekCell\DddBundle\Maker\Doctrine\ApiPlatformConfigUpdator:
class: GeekCell\DddBundle\Maker\ApiPlatform\ApiPlatformConfigUpdater
public: false
41 changes: 41 additions & 0 deletions src/Maker/AbstractBaseConfigUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace GeekCell\DddBundle\Maker;

use Assert;
use Assert\AssertionFailedException;
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;

abstract class AbstractBaseConfigUpdater
{
private ?YamlSourceManipulator $manipulator;

/**
* Creates a YamlSourceManipulator from a YAML source.
*
* @param string $yamlSource
* @return array<string, string|string[]>
*/
protected function read(string $yamlSource): array
{
$this->manipulator = new YamlSourceManipulator($yamlSource);
return $this->manipulator->getData();
}

/**
* Returns the updated YAML contents for the given data.
*
* @param array<string, string|string[]> $yamlData
* @return string
* @throws AssertionFailedException
*/
protected function write(array $yamlData): string
{
Assert\Assertion::notNull($this->manipulator);
$this->manipulator->setData($yamlData);

return $this->manipulator->getContents();
}
}
27 changes: 27 additions & 0 deletions src/Maker/ApiPlatform/ApiPlatformConfigUpdater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace GeekCell\DddBundle\Maker\ApiPlatform;

use Assert;
use GeekCell\DddBundle\Maker\AbstractBaseConfigUpdater;

class ApiPlatformConfigUpdater extends AbstractBaseConfigUpdater
{
/**
* @param string $yamlSource
* @param string $path
* @return string
*/
public function addCustomPath(string $yamlSource, string $path): string
{
$data = $this->read($yamlSource);

/** @var array|null $currentPaths */
$currentPaths = $data['api_platform']['mapping']['paths'];
$data['api_platform']['mapping']['paths'] = array_unique(array_merge($currentPaths, [$path]));

return $this->write($data);
}
}
43 changes: 6 additions & 37 deletions src/Maker/Doctrine/DoctrineConfigUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@
namespace GeekCell\DddBundle\Maker\Doctrine;

use Assert;
use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator;
use GeekCell\DddBundle\Maker\AbstractBaseConfigUpdater;

class DoctrineConfigUpdater
class DoctrineConfigUpdater extends AbstractBaseConfigUpdater
{
/**
* @var null|YamlSourceManipulator
*/
private ?YamlSourceManipulator $manipulator;

/**
* Registers a custom DBAL mapping type.
*
Expand All @@ -25,10 +20,10 @@ class DoctrineConfigUpdater
*/
public function addCustomDBALMappingType(string $yamlSource, string $identifier, string $mappingClass): string
{
$data = $this->createYamlSourceManipulator($yamlSource);
$data = $this->read($yamlSource);
$data['doctrine']['dbal']['types'][$identifier] = $mappingClass;

return $this->getYamlContentsFromData($data);
return $this->write($data);
}

/**
Expand All @@ -44,39 +39,13 @@ public function updateORMDefaultEntityMapping(string $yamlSource, string $mappin
{
Assert\Assertion::inArray($mappingType, ['xml', 'attribute'], 'Invalid mapping type: %s');

$data = $this->createYamlSourceManipulator($yamlSource);
$data = $this->read($yamlSource);
$data['doctrine']['orm']['mappings']['App']['type'] = $mappingType;
$data['doctrine']['orm']['mappings']['App']['dir'] = $directory;
$data['doctrine']['orm']['mappings']['App']['prefix'] = 'App\Domain\Model';
$data['doctrine']['orm']['mappings']['App']['alias'] = 'App';
$data['doctrine']['orm']['mappings']['App']['is_bundle'] = false;

return $this->getYamlContentsFromData($data);
}

/**
* Creates a YamlSourceManipulator from a YAML source.
*
* @param string $yamlSource
* @return array<string, string|string[]>
*/
private function createYamlSourceManipulator(string $yamlSource): array
{
$this->manipulator = new YamlSourceManipulator($yamlSource);
return $this->manipulator->getData();
}

/**
* Returns the updated YAML contents for the given data.
*
* @param array<string, string|string[]> $yamlData
* @return string
*/
private function getYamlContentsFromData(array $yamlData): string
{
Assert\Assertion::notNull($this->manipulator);
$this->manipulator->setData($yamlData);

return $this->manipulator->getContents();
return $this->write($data);
}
}
7 changes: 2 additions & 5 deletions src/Maker/MakeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,13 @@ final class MakeModel extends AbstractMaker implements InputAwareMakerInterface
*/
private $templateVariables = [];


/**
* Constructor.
*
* @param DoctrineConfigUpdater $doctrineUpdater
* @param FileManager $fileManager
*/
public function __construct(
private DoctrineConfigUpdater $doctrineUpdater,
private FileManager $fileManager,
private readonly DoctrineConfigUpdater $doctrineUpdater,
private readonly FileManager $fileManager,
) {}

/**
Expand Down
Loading