1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Action; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Creation\Src\Entity\DataTransferObjects\DtoCreator; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
|
9
|
|
|
class CreateDtosForAllEntitiesAction implements ActionInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var DtoCreator |
13
|
|
|
*/ |
14
|
|
|
private $dataTransferObjectCreator; |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $projectRootNamespace; |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $projectRootDirectory; |
23
|
|
|
/** |
24
|
|
|
* @var NamespaceHelper |
25
|
|
|
*/ |
26
|
|
|
private $namespaceHelper; |
27
|
|
|
|
28
|
|
|
public function __construct(DtoCreator $dataTransferObjectCreator, NamespaceHelper $namespaceHelper) |
29
|
|
|
{ |
30
|
|
|
$this->dataTransferObjectCreator = $dataTransferObjectCreator; |
31
|
|
|
$this->namespaceHelper = $namespaceHelper; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function run(): void |
35
|
|
|
{ |
36
|
|
|
$entityFqns = $this->findAllEntityFqns(); |
37
|
|
|
foreach ($entityFqns as $entityFqn) { |
38
|
|
|
$this->dataTransferObjectCreator->setNewObjectFqnFromEntityFqn($entityFqn) |
39
|
|
|
->createTargetFileObject() |
40
|
|
|
->write(); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
private function findAllEntityFqns() |
45
|
|
|
{ |
46
|
|
|
$finder = new Finder(); |
47
|
|
|
$finder->files()->in($this->projectRootDirectory . '/src/Entities')->name('*.php'); |
48
|
|
|
$entityFqns = []; |
49
|
|
|
foreach ($finder as $splFileInfo) { |
50
|
|
|
$path = $splFileInfo->getRealPath(); |
51
|
|
|
$subPath = \substr($path, \strpos($path, '/src/Entities/') + \strlen('/src/Entities/')); |
52
|
|
|
$subPath = \str_replace('.php', '', $subPath); |
53
|
|
|
$entityFqns[] = $this->namespaceHelper->tidy( |
54
|
|
|
$this->projectRootNamespace . '\\Entities\\' . \str_replace('/', '\\', $subPath) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $entityFqns; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setProjectRootNamespace(string $projectRootNamespace): self |
62
|
|
|
{ |
63
|
|
|
$this->projectRootNamespace = $projectRootNamespace; |
64
|
|
|
$this->dataTransferObjectCreator->setProjectRootNamespace($projectRootNamespace); |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setProjectRootDirectory(string $projectRootDirectory): self |
70
|
|
|
{ |
71
|
|
|
$this->projectRootDirectory = $projectRootDirectory; |
72
|
|
|
$this->dataTransferObjectCreator->setProjectRootDirectory($projectRootDirectory); |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|