|
4 | 4 |
|
5 | 5 | namespace Bone\Generator\Service; |
6 | 6 |
|
| 7 | +use Bone\Generator\Exception\GeneratorException; |
| 8 | +use Nette\PhpGenerator\PhpFile; |
| 9 | +use Nette\PhpGenerator\PsrPrinter; |
| 10 | + |
| 11 | +use function explode; |
| 12 | +use function file_exists; |
| 13 | +use function file_put_contents; |
| 14 | +use function implode; |
| 15 | +use function in_array; |
| 16 | +use function sprintf; |
| 17 | + |
7 | 18 | class ControllerGeneratorService |
8 | 19 | { |
| 20 | + const F_ENTITY_MANAGER = 'entity manager'; |
| 21 | + const F_I18N = 'i18n'; |
| 22 | + const F_LOGGER = 'logger'; |
| 23 | + const F_PDO = 'pdo'; |
| 24 | + const F_SERIALIZER = 'serializer'; |
| 25 | + const F_SESSION = 'session'; |
| 26 | + const F_SITE_CONFIG = 'site config'; |
| 27 | + const F_VIEW = 'view'; |
| 28 | + |
| 29 | + const FEATURES = [ |
| 30 | + self::F_ENTITY_MANAGER, |
| 31 | + self::F_I18N, |
| 32 | + self::F_LOGGER, |
| 33 | + self::F_PDO, |
| 34 | + self::F_SERIALIZER, |
| 35 | + self::F_SESSION, |
| 36 | + self::F_SITE_CONFIG, |
| 37 | + self::F_VIEW, |
| 38 | + ]; |
| 39 | + |
| 40 | + const INTERFACES = [ |
| 41 | + self::F_ENTITY_MANAGER => 'Bone\BoneDoctrine\EntityManagerAwareInterface', |
| 42 | + self::F_I18N => 'Bone\I18n\I18nAwareInterface', |
| 43 | + self::F_LOGGER => 'Bone\Log\LoggerAwareInterface', |
| 44 | + self::F_PDO => 'Bone\Db\DbProviderInterface', |
| 45 | + self::F_SERIALIZER => 'Bone\Controller\SerializerAwareInterface', |
| 46 | + self::F_SESSION => 'Bone\Server\SessionAwareInterface', |
| 47 | + self::F_SITE_CONFIG => 'Bone\Server\SiteConfigAwareInterface', |
| 48 | + self::F_VIEW => 'Bone\View\ViewAwareInterface', |
| 49 | + ]; |
| 50 | + |
| 51 | + const TRAITS = [ |
| 52 | + self::F_ENTITY_MANAGER => 'Bone\BoneDoctrine\Traits\HasEntityManagerTrait', |
| 53 | + self::F_I18N => 'Bone\I18n\Traits\HasTranslatorTrait', |
| 54 | + self::F_LOGGER => 'Bone\Log\Traits\HasLoggerTrait', |
| 55 | + self::F_PDO => 'Bone\Db\HasDbTrait', |
| 56 | + self::F_SERIALIZER => 'Bone\Controller\Traits\HasSerializer', |
| 57 | + self::F_SESSION => 'Bone\Server\Traits\HasSessionTrait', |
| 58 | + self::F_SITE_CONFIG => 'Bone\Server\Traits\HasSiteConfigTrait', |
| 59 | + self::F_VIEW => 'Bone\View\Traits\HasViewTrait', |
| 60 | + ]; |
| 61 | + |
9 | 62 | public function generateController( |
10 | 63 | string $srcFolderNamespace, |
11 | 64 | string $controllerNamespace, |
12 | 65 | string $controllerName, |
13 | | - string $features |
| 66 | + array $features |
14 | 67 | ): void { |
| 68 | + $controllerNamespacePath = implode('/', explode('\\', $controllerNamespace)); |
| 69 | + $fileName = './src/' . $controllerNamespacePath . '/' . $controllerName . '.php'; |
| 70 | + $this->ensureNoFileExists($fileName); |
| 71 | + $file = new PhpFile(); |
| 72 | + $file->setStrictTypes(); |
| 73 | + $namespace = $srcFolderNamespace . '\\' . $controllerNamespace; |
| 74 | + $namespace = $file->addNamespace($namespace); |
| 75 | + $namespace->addUse('Bone\Http\Response'); |
| 76 | + $namespace->addUse('Psr\Http\Message\ResponseInterface'); |
| 77 | + $namespace->addUse('Psr\Http\Message\ServerRequestInterface'); |
| 78 | + $class = $namespace->addClass($controllerName); |
| 79 | + |
| 80 | + foreach ($features as $feature) { |
| 81 | + $namespace->addUse(self::INTERFACES[$feature]); |
| 82 | + $namespace->addUse(self::TRAITS[$feature]); |
| 83 | + $file->addUse(self::INTERFACES[$feature]); |
| 84 | + $file->addUse(self::TRAITS[$feature]); |
| 85 | + $class->addImplement(self::INTERFACES[$feature]); |
| 86 | + $class->addTrait(self::TRAITS[$feature]); |
| 87 | + } |
| 88 | + |
| 89 | + $method = $class->addMethod('index'); |
| 90 | + $method->addParameter('request')->setType('Psr\Http\Message\ServerRequestInterface'); |
| 91 | + $method->setReturnType('Psr\Http\Message\ResponseInterface'); |
| 92 | + $method->setBody('return new Response();'); |
| 93 | + |
| 94 | + if (in_array('logger', $features)) { |
| 95 | + $method = $class->addMethod('getChannel'); |
| 96 | + $method->setReturnType('string'); |
| 97 | + $method->setBody('return \'default\';'); |
| 98 | + } |
| 99 | + |
| 100 | + $printer = new PsrPrinter(); |
| 101 | + $code = $printer->printFile($file); |
| 102 | + $result = file_put_contents($fileName, $code); |
| 103 | + |
| 104 | + if (!$result) { |
| 105 | + throw new GeneratorException(sprintf(GeneratorException::FILE_WRITE_ERROR, $fileName)); |
| 106 | + } |
| 107 | + } |
15 | 108 |
|
| 109 | + private function ensureNoFileExists(string $fileName): void |
| 110 | + { |
| 111 | + if (file_exists($fileName)) { |
| 112 | + throw new GeneratorException(GeneratorException::FILE_EXISTS); |
| 113 | + } |
16 | 114 | } |
17 | 115 | } |
0 commit comments