|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the package friendsoftypo3/kickstarter. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please read the |
| 9 | + * LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace FriendsOfTYPO3\Kickstarter\Creator\ViewHelper; |
| 13 | + |
| 14 | +use FriendsOfTYPO3\Kickstarter\Creator\FileManager; |
| 15 | +use FriendsOfTYPO3\Kickstarter\Information\ViewHelperInformation; |
| 16 | +use FriendsOfTYPO3\Kickstarter\PhpParser\NodeFactory; |
| 17 | +use FriendsOfTYPO3\Kickstarter\PhpParser\Structure\ClassStructure; |
| 18 | +use FriendsOfTYPO3\Kickstarter\PhpParser\Structure\DeclareStructure; |
| 19 | +use FriendsOfTYPO3\Kickstarter\PhpParser\Structure\FileStructure; |
| 20 | +use FriendsOfTYPO3\Kickstarter\PhpParser\Structure\MethodStructure; |
| 21 | +use FriendsOfTYPO3\Kickstarter\PhpParser\Structure\NamespaceStructure; |
| 22 | +use FriendsOfTYPO3\Kickstarter\PhpParser\Structure\UseStructure; |
| 23 | +use FriendsOfTYPO3\Kickstarter\Traits\FileStructureBuilderTrait; |
| 24 | +use PhpParser\BuilderFactory; |
| 25 | +use PhpParser\Node\Stmt\Return_; |
| 26 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 27 | +use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractTagBasedViewHelper; |
| 28 | +use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; |
| 29 | + |
| 30 | +class ViewHelperCreator implements ViewHelperCreatorInterface |
| 31 | +{ |
| 32 | + use FileStructureBuilderTrait; |
| 33 | + |
| 34 | + private BuilderFactory $builderFactory; |
| 35 | + |
| 36 | + public function __construct( |
| 37 | + private readonly NodeFactory $nodeFactory, |
| 38 | + private readonly FileManager $fileManager, |
| 39 | + ) { |
| 40 | + $this->builderFactory = new BuilderFactory(); |
| 41 | + } |
| 42 | + |
| 43 | + public function create(ViewHelperInformation $viewHelperInformation): void |
| 44 | + { |
| 45 | + GeneralUtility::mkdir_deep($viewHelperInformation->getPath()); |
| 46 | + |
| 47 | + $filePath = $viewHelperInformation->getPath() . $viewHelperInformation->getFilename(); |
| 48 | + |
| 49 | + if (is_file($filePath)) { |
| 50 | + $viewHelperInformation->getCreatorInformation()->fileExists( |
| 51 | + $filePath, |
| 52 | + sprintf( |
| 53 | + 'ViewHelpers can only be created, not modified. The file %s already exists and cannot be overridden. ', |
| 54 | + $filePath |
| 55 | + ) |
| 56 | + ); |
| 57 | + return; |
| 58 | + } |
| 59 | + $fileStructure = $this->buildFileStructure($filePath); |
| 60 | + $this->addClassNodes($fileStructure, $viewHelperInformation); |
| 61 | + $this->fileManager->createFile($filePath, $fileStructure->getFileContents(), $viewHelperInformation->getCreatorInformation()); |
| 62 | + } |
| 63 | + |
| 64 | + private function addClassNodes(FileStructure $fileStructure, ViewHelperInformation $viewHelperInformation): void |
| 65 | + { |
| 66 | + $fileStructure->addDeclareStructure( |
| 67 | + new DeclareStructure($this->nodeFactory->createDeclareStrictTypes()) |
| 68 | + ); |
| 69 | + if ($viewHelperInformation->isTagBased()) { |
| 70 | + $this->createTagBasedViewHelper($fileStructure, $viewHelperInformation); |
| 71 | + } else { |
| 72 | + $this->createPlainViewHelper($fileStructure, $viewHelperInformation); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private function createPlainViewHelper(FileStructure $fileStructure, ViewHelperInformation $viewHelperInformation): void |
| 77 | + { |
| 78 | + $fileStructure->addUseStructure( |
| 79 | + new UseStructure($this->nodeFactory->createUseImport(AbstractViewHelper::class)) |
| 80 | + ); |
| 81 | + $fileStructure->addNamespaceStructure( |
| 82 | + new NamespaceStructure($this->nodeFactory->createNamespace( |
| 83 | + $viewHelperInformation->getNamespace(), |
| 84 | + $viewHelperInformation->getExtensionInformation(), |
| 85 | + )) |
| 86 | + ); |
| 87 | + $fileStructure->addClassStructure( |
| 88 | + new ClassStructure( |
| 89 | + $this->builderFactory |
| 90 | + ->class($viewHelperInformation->getClassname()) |
| 91 | + ->makeFinal() |
| 92 | + ->extend('AbstractViewHelper') |
| 93 | + ->getNode(), |
| 94 | + ) |
| 95 | + ); |
| 96 | + $fileStructure->addMethodStructure( |
| 97 | + new MethodStructure( |
| 98 | + $this->builderFactory |
| 99 | + ->method('initializeArguments') |
| 100 | + ->makePublic() |
| 101 | + ->setReturnType('void') |
| 102 | + ->getNode() |
| 103 | + ) |
| 104 | + ); |
| 105 | + $fileStructure->addMethodStructure( |
| 106 | + new MethodStructure( |
| 107 | + $this->builderFactory |
| 108 | + ->method('render') |
| 109 | + ->makePublic() |
| 110 | + ->setReturnType('string') |
| 111 | + ->addStmt(new Return_($this->builderFactory->val('ViewHelper ' . $viewHelperInformation->getClassname() . ' content. '))) |
| 112 | + ->getNode() |
| 113 | + ) |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + private function createTagBasedViewHelper(FileStructure $fileStructure, ViewHelperInformation $viewHelperInformation): void |
| 118 | + { |
| 119 | + |
| 120 | + $fileStructure->addUseStructure( |
| 121 | + new UseStructure($this->nodeFactory->createUseImport(AbstractTagBasedViewHelper::class)) |
| 122 | + ); |
| 123 | + $fileStructure->addNamespaceStructure( |
| 124 | + new NamespaceStructure($this->nodeFactory->createNamespace( |
| 125 | + $viewHelperInformation->getNamespace(), |
| 126 | + $viewHelperInformation->getExtensionInformation(), |
| 127 | + )) |
| 128 | + ); |
| 129 | + $fileStructure->addClassStructure( |
| 130 | + new ClassStructure( |
| 131 | + $this->builderFactory |
| 132 | + ->class($viewHelperInformation->getClassname()) |
| 133 | + ->makeFinal() |
| 134 | + ->extend('AbstractTagBasedViewHelper') |
| 135 | + ->getNode(), |
| 136 | + ) |
| 137 | + ); |
| 138 | + $fileStructure->addMethodStructure( |
| 139 | + new MethodStructure( |
| 140 | + $this->builderFactory |
| 141 | + ->method('initializeArguments') |
| 142 | + ->makePublic() |
| 143 | + ->setReturnType('void') |
| 144 | + ->getNode() |
| 145 | + ) |
| 146 | + ); |
| 147 | + $fileStructure->addMethodStructure( |
| 148 | + new MethodStructure( |
| 149 | + $this->builderFactory |
| 150 | + ->method('render') |
| 151 | + ->makePublic() |
| 152 | + ->setReturnType('string') |
| 153 | + ->addStmt(new Return_($this->builderFactory->val('ViewHelper ' . $viewHelperInformation->getClassname() . ' content. '))) |
| 154 | + ->getNode() |
| 155 | + ) |
| 156 | + ); |
| 157 | + } |
| 158 | +} |
0 commit comments