|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: thales |
|
5
|
|
|
* Date: 22/01/2019 |
|
6
|
|
|
* Time: 11:16 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Saci\Console\Infrastructure\Domain\Services; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use cristianoc72\codegen\model\PhpClass; |
|
13
|
|
|
use Saci\Console\Domain\Entity\Command; |
|
14
|
|
|
use Saci\Console\Domain\Events\CommandWasCreated; |
|
15
|
|
|
use Saci\Console\Domain\Services\ClassCommandHandlerMakerSubscriber; |
|
|
|
|
|
|
16
|
|
|
use Saci\Console\Domain\Services\Exception\MappingNotFoundException; |
|
17
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
18
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
19
|
|
|
|
|
20
|
|
|
class AddingCommandInMappingSubscriber implements ClassCommandHandlerMakerSubscriber, EventSubscriberInterface |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
public static function getSubscribedEvents() |
|
24
|
|
|
{ |
|
25
|
|
|
return [CommandWasCreated::NAME => 'create']; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param CommandWasCreated $commandWasCreated |
|
30
|
|
|
* @return bool |
|
31
|
|
|
* @throws MappingNotFoundException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function create(CommandWasCreated $commandWasCreated) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!$commandWasCreated->getCommand()->getModule()->mappingExists()) { |
|
36
|
|
|
throw new MappingNotFoundException('O arquivo Mapping não foi encontrado! Crie primeiro o arquivo Mapping.php para depois criar os Commands!'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$class = PhpClass::fromFile($commandWasCreated->getCommand()->getModule()->getLocalMapping()); |
|
40
|
|
|
|
|
41
|
|
|
$method = $class->getMethod('__invoke'); |
|
42
|
|
|
|
|
43
|
|
|
$body = $this->createBody($method->getBody(), $commandWasCreated->getCommand()); |
|
44
|
|
|
|
|
45
|
|
|
$method->setBody($body); |
|
46
|
|
|
|
|
47
|
|
|
$generator = GeneratorClassFactory::create(); |
|
48
|
|
|
$stringClass = $generator->generate($class); |
|
49
|
|
|
|
|
50
|
|
|
$stringClass = "<?php\n" . $stringClass; |
|
51
|
|
|
|
|
52
|
|
|
(new Filesystem())->dumpFile($commandWasCreated->getCommand()->getModule()->getPathModule() . DIRECTORY_SEPARATOR . 'Mapping.php', $stringClass); |
|
53
|
|
|
|
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function createBody(string $body, Command $command): string |
|
58
|
|
|
{ |
|
59
|
|
|
$bodyNormalized = $this->normalizeBody($body); |
|
60
|
|
|
$bodyArray = $this->bodyToArray($bodyNormalized); |
|
61
|
|
|
|
|
62
|
|
|
$string = ''; |
|
63
|
|
|
foreach ($bodyArray as $key => $value) { |
|
64
|
|
|
if ($key > 0) { |
|
65
|
|
|
$string .= "\n\t"; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$string .= "$value,"; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $body = <<<BODY |
|
|
|
|
|
|
72
|
|
|
return [ |
|
73
|
|
|
{$string} |
|
74
|
|
|
{$command->getClassNameCommand()}::class => {$command->getClassNameCommandHandler()}::class, |
|
75
|
|
|
]; |
|
76
|
|
|
BODY; |
|
77
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function normalizeBody(string $body): string |
|
81
|
|
|
{ |
|
82
|
|
|
return str_replace(['return [', '];'], ['', ''], $body); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function bodyToArray(string $body): array |
|
86
|
|
|
{ |
|
87
|
|
|
return explode(',', $body); |
|
88
|
|
|
} |
|
89
|
|
|
} |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: