These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Paro\BuildParametersHandler; |
||
3 | use Composer\Script\Event; |
||
4 | use Incenteev\ParameterHandler\Processor; |
||
5 | use Symfony\Component\Yaml\Parser; |
||
6 | use Symfony\Component\Yaml\Yaml; |
||
7 | |||
8 | class ParametersHandler |
||
9 | { |
||
10 | public static function buildParameters(Event $event) |
||
11 | { |
||
12 | $extras = $event->getComposer()->getPackage()->getExtra(); |
||
13 | if (!isset($extras['build-parameters'])) { |
||
14 | throw new \InvalidArgumentException('The parameter handler needs to be configured through the extra.build-parameters setting.'); |
||
15 | } |
||
16 | $configs = $extras['build-parameters']; |
||
17 | if (!is_array($configs)) { |
||
18 | throw new \InvalidArgumentException('The extra.build-parameters setting must be an array or a configuration object.'); |
||
19 | } |
||
20 | |||
21 | if (!isset($configs['build-folder'])) { |
||
22 | $configs['build-folder'] = 'build'; |
||
23 | } |
||
24 | |||
25 | self::initBuildDirectory($configs['build-folder']); |
||
26 | self::processFiles($configs, $event); |
||
27 | self::processIncenteevParameters($configs, $event); |
||
28 | } |
||
29 | |||
30 | private static function processIncenteevParameters($configs, $event) { |
||
31 | if (!isset($configs['incenteev-parameters'])) { |
||
32 | return true; |
||
33 | } |
||
34 | |||
35 | $processor = new Processor($event->getIO()); |
||
36 | $parameters = $configs['incenteev-parameters']; |
||
37 | View Code Duplication | if (array_keys($parameters) !== range(0, count($parameters) - 1)) { |
|
0 ignored issues
–
show
|
|||
38 | $parameters = array($parameters); |
||
39 | } |
||
40 | |||
41 | foreach ($parameters as $config) { |
||
42 | if (!is_array($config)) { |
||
43 | throw new \InvalidArgumentException('The extra.build-parameters setting must be an array of configuration objects.'); |
||
44 | } |
||
45 | |||
46 | $file = self::preparePath($config['file'], $event); |
||
47 | |||
48 | $config['dist-file'] = $file; |
||
49 | $config['file'] = $configs['build-folder'] . '/' . (isset($config['name'])? $config['name'] : $file); |
||
50 | $processor->processFile($config); |
||
51 | self::processFile($config); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | View Code Duplication | private static function preparePath($path, $event) { |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
56 | if (($env = self::getEnvParameter($event)) !== false) { |
||
57 | return str_replace("{env}", $env, $path); |
||
58 | } else { |
||
59 | return $path; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | private static function getEnvParameter(Event $event) { |
||
64 | $arguments = $event->getArguments(); |
||
65 | if (!is_array($arguments)) { |
||
66 | return false; |
||
67 | } |
||
68 | |||
69 | View Code Duplication | return array_reduce($arguments, function($ret, $item) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
70 | if (substr(strtolower($item), 0, 5) == '--env') { |
||
71 | $val = explode('=', $item); |
||
72 | return trim($val[1]); |
||
73 | } |
||
74 | }, false); |
||
75 | |||
76 | } |
||
77 | |||
78 | private static function processFiles($configs, $event) { |
||
79 | if (!isset($configs['files'])) { |
||
80 | return true; |
||
81 | } |
||
82 | |||
83 | $files = $configs['files']; |
||
84 | View Code Duplication | if (array_keys($files) !== range(0, count($files) - 1)) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
85 | $files = array($files); |
||
86 | } |
||
87 | foreach ($files as $file) { |
||
88 | if (!is_array($file)) { |
||
89 | throw new \InvalidArgumentException('The extra.files setting must be an array of configuration objects.'); |
||
90 | } |
||
91 | |||
92 | $path = self::preparePath($file['file'], $event); |
||
93 | $destination = $configs['build-folder'] . '/' . (isset($file['name']) ? $file['name'] : $path); |
||
94 | copy($path, $destination); |
||
95 | if (isset($file['name'])) { |
||
96 | $event->getIO()->write(sprintf('<info>Copying the "%s" into "%s" file</info>', $path, $destination)); |
||
97 | } else { |
||
98 | $event->getIO()->write(sprintf('<info>Copying the "%s" file</info>', $path)); |
||
99 | } |
||
100 | |||
101 | } |
||
102 | } |
||
103 | |||
104 | private static function initBuildDirectory($dir) { |
||
105 | if (!is_dir($dir)) { |
||
106 | mkdir($dir); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | private static function processFile($config) |
||
111 | { |
||
112 | $file = $config['file']; |
||
113 | $yamlParser = new Parser(); |
||
114 | $sourceValues = $yamlParser->parse(file_get_contents($file)); |
||
115 | View Code Duplication | $values = array_map(function($item) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
116 | $item = trim($item); |
||
117 | if (substr(strtolower($item), 0, 5) === "%env(" && substr(strtolower($item), -2) == ')%') { |
||
118 | $envName = substr(trim($item), 5); |
||
119 | $envName = substr($envName, 0, strlen($envName) - 2); |
||
120 | return getenv($envName); |
||
121 | } else { |
||
122 | return $item; |
||
123 | } |
||
124 | }, $sourceValues['parameters']); |
||
125 | |||
126 | file_put_contents($file, sprintf("# This file is auto-generated during the build process at %s\n", date(DATE_ATOM)) . Yaml::dump($values, 99)); |
||
127 | } |
||
128 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.