|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace NamespaceProtector\Command; |
|
4
|
|
|
|
|
5
|
|
|
use NamespaceProtector\Common\PathInterface; |
|
6
|
|
|
use NamespaceProtector\Scanner\ComposerJson; |
|
7
|
|
|
use NamespaceProtector\Common\FileSystemPath; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
use NamespaceProtector\Config\ConfigTemplateCreatorInterface; |
|
12
|
|
|
use NamespaceProtector\Exception\NamespaceProtectorExceptionInterface; |
|
13
|
|
|
|
|
14
|
|
|
final class NamespaceProtectorConfigCreatorCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
const CREATE_DEFAULT_CONFIG = 'create-default-config'; |
|
17
|
|
|
const NAMESPACE_PROTECTOR_JSON = 'namespace-protector.json'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var ConfigTemplateCreatorInterface */ |
|
20
|
|
|
private $configTemplateCreator; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct( |
|
23
|
|
|
string $name, |
|
24
|
|
|
ConfigTemplateCreatorInterface $configTemplateCreator |
|
25
|
|
|
) |
|
26
|
|
|
{ |
|
27
|
|
|
parent::__construct($name); |
|
28
|
|
|
$this->configTemplateCreator = $configTemplateCreator; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function configure(): void |
|
32
|
|
|
{ |
|
33
|
|
|
parent::configure(); |
|
34
|
|
|
$this->setName('create-config') |
|
35
|
|
|
->setDescription('Create config template') |
|
36
|
|
|
->setHelp('Create config template with default values'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
40
|
|
|
{ |
|
41
|
|
|
$this->configTemplateCreator->create($this->detectComposerJsonDirectory()); |
|
42
|
|
|
return self::SUCCESS; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function detectComposerJsonDirectory(): PathInterface |
|
46
|
|
|
{ |
|
47
|
|
|
$countMax = 5; |
|
48
|
|
|
$relativePath = ''; |
|
49
|
|
|
|
|
50
|
|
|
for ($i = 0; $i < $countMax; $i++) { |
|
51
|
|
|
$pathComposer = \getcwd() . DIRECTORY_SEPARATOR . $relativePath; |
|
52
|
|
|
$realPath = \safe\realpath($pathComposer . ComposerJson::FILE_NAME); |
|
53
|
|
|
|
|
54
|
|
|
if (\is_readable($realPath) === false) { |
|
55
|
|
|
$relativePath .= '..' . DIRECTORY_SEPARATOR; |
|
56
|
|
|
continue; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$jsonArray = \safe\json_decode( |
|
60
|
|
|
\safe\file_get_contents($pathComposer . DIRECTORY_SEPARATOR . ComposerJson::FILE_NAME), |
|
61
|
|
|
true |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
if ($jsonArray['name'] !== ComposerJson::NAME_PROJECT_IN_COMPOSER) { |
|
65
|
|
|
return new FileSystemPath($pathComposer); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
throw new \RuntimeException(NamespaceProtectorExceptionInterface::MSG_PLAIN_ERROR_COMPOSER_JSON_NOT_READABLE); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|