These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * phpDocumentor |
||
4 | * |
||
5 | * PHP Version 5.3 |
||
6 | * |
||
7 | * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com) |
||
8 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
||
9 | * @link http://phpdoc.org |
||
10 | */ |
||
11 | |||
12 | namespace phpDocumentor\Configuration; |
||
13 | |||
14 | use JMS\Serializer\Serializer; |
||
15 | use phpDocumentor\Console\Input\ArgvInput; |
||
16 | |||
17 | /** |
||
18 | * Loads the template and user-defined configuration file from disk and creates a Configuration object from it. |
||
19 | * |
||
20 | * This class will merge the template file and the user-defined configuration file together and serialize it into a |
||
21 | * configuration object (defaults to `phpDocumentor\Configuration`). |
||
22 | */ |
||
23 | class Loader |
||
24 | { |
||
25 | /** @var Serializer Object used to serialize configuration files to objects. */ |
||
26 | private $serializer; |
||
27 | |||
28 | /** @var Merger Object that merges variables, including objects. */ |
||
29 | private $merger; |
||
30 | |||
31 | /** |
||
32 | * Registers the dependencies with the loader. |
||
33 | * |
||
34 | * @param Serializer $serializer Object used to serialize configuration files to objects. |
||
35 | * @param Merger $merger Object that merges variables, including objects. |
||
36 | */ |
||
37 | public function __construct(Serializer $serializer, Merger $merger) |
||
38 | { |
||
39 | $this->serializer = $serializer; |
||
40 | $this->merger = $merger; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Loads the configuration from the provided paths and returns a populated configuration object. |
||
45 | * |
||
46 | * @param string $templatePath Path to configuration file containing default settings. |
||
47 | * @param string $userConfigurationPath Path to a file containing user overrides. |
||
48 | * @param string $class The class to instantiate and populate with these options. |
||
49 | * |
||
50 | * @return object |
||
51 | */ |
||
52 | public function load($templatePath, $userConfigurationPath, $class = 'phpDocumentor\Configuration') |
||
0 ignored issues
–
show
|
|||
53 | { |
||
54 | $userConfigFilePath = $this->fetchUserConfigFileFromCommandLineOptions(); |
||
55 | |||
56 | if ($this->isValidFile($userConfigFilePath)) { |
||
57 | chdir(dirname($userConfigFilePath)); |
||
58 | } else { |
||
59 | $userConfigFilePath = null; |
||
60 | } |
||
61 | |||
62 | return $this->createConfigurationObject($templatePath, $userConfigurationPath, $userConfigFilePath, $class); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Reads the `--config`, or `-c`, command line option and returns a path to the configuration file from those |
||
67 | * options or false if no existing path was given. |
||
68 | * |
||
69 | * @return bool|string |
||
70 | */ |
||
71 | private function fetchUserConfigFileFromCommandLineOptions() |
||
72 | { |
||
73 | $input = new ArgvInput(); |
||
74 | $userConfigFilePath = $input->getParameterOption('--config'); |
||
75 | |||
76 | if (!$userConfigFilePath) { |
||
77 | $userConfigFilePath = $input->getParameterOption('-c'); |
||
78 | } |
||
79 | |||
80 | if ($userConfigFilePath !== false) { |
||
81 | $userConfigFilePath = realpath($userConfigFilePath); |
||
82 | } |
||
83 | |||
84 | return $userConfigFilePath; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Verifies if the given path is valid and readable. |
||
89 | * |
||
90 | * @param bool|string $path |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | private function isValidFile($path) |
||
95 | { |
||
96 | return $path && $path != 'none' && is_readable($path); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Combines the given configuration files and serializes a new Configuration object from them. |
||
101 | * |
||
102 | * @param string $templatePath Path to the template configuration file. |
||
103 | * @param string $defaultUserConfigPath Path to the phpdoc.xml or phpdoc,dist.xml in the current working |
||
104 | * directory. |
||
105 | * @param null|bool|string $customUserConfigPath Path to the user-defined config file given using the command-line. |
||
106 | * @param string $class Base Configuration class name to construct and populate. |
||
107 | * |
||
108 | * @return null|object |
||
109 | */ |
||
110 | private function createConfigurationObject($templatePath, $defaultUserConfigPath, $customUserConfigPath, $class) |
||
0 ignored issues
–
show
|
|||
111 | { |
||
112 | $config = $this->serializer->deserialize(file_get_contents($templatePath), $class, 'xml'); |
||
113 | $customUserConfigPath = $customUserConfigPath ? : $defaultUserConfigPath; |
||
114 | |||
115 | if ($customUserConfigPath !== null && is_readable($customUserConfigPath)) { |
||
116 | $userConfigFile = $this->serializer->deserialize(file_get_contents($customUserConfigPath), $class, 'xml'); |
||
117 | |||
118 | $config = $this->merger->run($config, $userConfigFile); |
||
119 | } |
||
120 | |||
121 | return $config; |
||
122 | } |
||
123 | } |
||
124 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.