1 : <?php
2 :
3 : /**
4 : * XSLT Benchmarking
5 : * @link https://github.com/masicek/XSLT-Benchmarking
6 : * @author Viktor Mašíček <viktor@masicek.net>
7 : * @license "New" BSD License
8 : */
9 :
10 : namespace XSLTBenchmarking\TestsGenerator;
11 :
12 : require_once __DIR__ . '/IParamsDriver.php';
13 : require_once __DIR__ . '/../XmlGenerator/XmlGenerator.php';
14 : require_once LIBS . '/PhpPath/PhpPath.min.php';
15 :
16 : use PhpPath\P;
17 :
18 : /**
19 : * Xml prarams for the collection of tests
20 : *
21 : * @author Viktor Mašíček <viktor@masicek.net>
22 : */
23 : class XmlParamsDriver implements IParamsDriver
24 : {
25 :
26 : /**
27 : * Params loaded from xml file
28 : *
29 : * @var \SimpleXMLElement
30 : */
31 : private $tests;
32 :
33 : /**
34 : * The path of the directory with definition of generated tests
35 : *
36 : * @var string
37 : */
38 : private $rootDirectory;
39 :
40 : /**
41 : * The path of the temporary directory
42 : *
43 : * @var string
44 : */
45 : private $tmpDirectoryPath;
46 :
47 : /**
48 : * The cache of list all files path and their id defined in params file
49 : *
50 : * @var string
51 : */
52 : private $allFilesPaths = NULL;
53 :
54 :
55 : /**
56 : * Object for generating XML files
57 : *
58 : * @var XmlGenerator
59 : */
60 : private $xmlGenerator = NULL;
61 :
62 :
63 : /**
64 : * Choose the params driver by extension
65 : *
66 : * @param \XSLTBenchmarking\TestsGenerator\XmlGenerator $xmlGenerator Object for generating XML files
67 : * @param string $tmpDirectoryPath The path of the temporary directory
68 : * @param string $paramsFilePath The path of the file with deffinition of generated tests
69 : *
70 : * @throws \XSLTBenchmarking\InvalidArgumentException Wrong format of file with params
71 : */
72 : public function __construct(
73 : \XSLTBenchmarking\TestsGenerator\XmlGenerator $xmlGenerator,
74 : $tmpDirectoryPath,
75 : $paramsFilePath)
76 : {
77 21 : P::cd($tmpDirectoryPath);
78 20 : P::cf($paramsFilePath);
79 :
80 : // validate
81 19 : $dom = new \DOMDocument();
82 19 : $dom->load($paramsFilePath);
83 : try {
84 19 : $dom->schemaValidate(P::m(__DIR__, 'XmlParamsDriver.xsd'));
85 19 : } catch (\Exception $e) {
86 17 : $error = libxml_get_last_error();
87 17 : throw new \XSLTBenchmarking\InvalidArgumentException(
88 17 : 'File "' . $paramsFilePath . '" has wrong format: ' . $error->message
89 17 : );
90 : }
91 :
92 2 : $this->rootDirectory = dirname($paramsFilePath);
93 2 : $this->xmlGenerator = $xmlGenerator;
94 2 : $this->tmpDirectoryPath = $tmpDirectoryPath;
95 2 : $this->tests = new \SimpleXMLElement($paramsFilePath, 0, TRUE);
96 2 : }
97 :
98 :
99 : /**
100 : * Return the name of tests collection
101 : *
102 : * @return string
103 : */
104 : public function getTemplateName()
105 : {
106 2 : return (string)$this->tests['name'];
107 : }
108 :
109 :
110 : /**
111 : * Return the path to the template file
112 : *
113 : * @return string
114 : */
115 : public function getTemplatePath()
116 : {
117 1 : return P::m($this->rootDirectory, (string)$this->tests['template']);
118 : }
119 :
120 :
121 : /**
122 : * Return the type of templating
123 : *
124 : * @return string
125 : */
126 : public function getTemplatingType()
127 : {
128 1 : return (string)$this->tests['templatingType'];
129 : }
130 :
131 :
132 : /**
133 : * Return the list of tests names
134 : *
135 : * @return array
136 : */
137 : public function getTestsNames()
138 : {
139 1 : $names = array();
140 :
141 1 : foreach ($this->tests->test as $test)
142 : {
143 1 : $names[] = (string)$test['name'];
144 1 : }
145 :
146 1 : return $names;
147 : }
148 :
149 :
150 : /**
151 : * Return the list of input files paths
152 : * and paths of their expected output files for selected test
153 : *
154 : * @param string $testName The name of the selected test
155 : *
156 : * @return array
157 : */
158 : public function getTestFilesPaths($testName)
159 : {
160 1 : $files = array();
161 1 : $test = $this->tests->xpath('//test[@name="' . $testName . '"]');
162 1 : $allFiles = $this->getAllFilesPaths();
163 :
164 1 : foreach ($test[0]->file as $file)
165 : {
166 1 : $input = $allFiles[(string)$file['input']];
167 1 : $output = $allFiles[(string)$file['output']];
168 1 : $files[$input] = $output;
169 1 : }
170 :
171 1 : return $files;
172 : }
173 :
174 :
175 : /**
176 : * Return the list of settings for the selected test
177 : *
178 : * @param string $testName The name of the selected test
179 : *
180 : * @return array
181 : */
182 : public function getTestSettings($testName)
183 : {
184 1 : $settings = array();
185 :
186 1 : $test = $this->tests->xpath('//test[@name="' . $testName . '"]');
187 1 : foreach ($test[0]->setting as $setting)
188 : {
189 1 : $settings[(string)$setting['name']] = (string)$setting;
190 1 : }
191 :
192 1 : return $settings;
193 : }
194 :
195 :
196 : /**
197 : * Return the name of file with params of the test
198 : *
199 : * @param string $testName The name of the selected test
200 : *
201 : * @return string|NULL
202 : */
203 : public function getTestParamsFileName($testName)
204 : {
205 1 : $test = $this->tests->xpath('//test[@name="' . $testName . '"]');
206 1 : $name = NULL;
207 1 : if (isset($test[0]['paramsFile']))
208 1 : {
209 1 : $name = (string)$test[0]['paramsFile'];
210 1 : }
211 :
212 1 : return $name;
213 : }
214 :
215 :
216 : /**
217 : * Return list of possible paths of files for testing with their ids
218 : *
219 : * @return array ([id] => [path], ...)
220 : */
221 : private function getAllFilesPaths()
222 : {
223 1 : if ($this->allFilesPaths)
224 1 : {
225 1 : return $this->allFilesPaths;
226 : }
227 :
228 1 : $this->allFilesPaths = $this->createAllFilesPaths();
229 1 : return $this->allFilesPaths;
230 : }
231 :
232 :
233 : /**
234 : * Create list of possible paths of files for testing with their ids
235 : *
236 : * @return array ([id] => [path], ...)
237 : */
238 : private function createAllFilesPaths()
239 : {
240 1 : $files = array();
241 :
242 : // existed xml files
243 1 : foreach ($this->tests->files->file as $file)
244 : {
245 1 : $files[(string)$file['id']] = P::m($this->rootDirectory, (string)$file);
246 1 : }
247 :
248 : // make tmp subdirectory for generating files
249 1 : if (count($this->tests->files->generated) > 0)
250 1 : {
251 1 : $tmpSubdirectory = strtolower(trim($this->getTemplateName()));
252 1 : $tmpSubdirectory = preg_replace('/[^a-z0-9-_]/', '-', $tmpSubdirectory);
253 1 : $tmpSubdirectory = preg_replace('/-+/', '-', $tmpSubdirectory);
254 1 : $tmpSubdirectoryPath = P::m($this->tmpDirectoryPath, $tmpSubdirectory);
255 1 : if (!is_dir($tmpSubdirectoryPath))
256 1 : {
257 1 : mkdir($tmpSubdirectoryPath);
258 1 : }
259 1 : }
260 : // generated xml files
261 1 : foreach ($this->tests->files->generated as $generated)
262 : {
263 : // read settings
264 1 : $settings = array();
265 1 : foreach ($generated->setting as $setting)
266 : {
267 1 : $settings[(string)$setting['name']] = (string)$setting;
268 1 : }
269 :
270 : // generate file into TMP
271 1 : $type = (string)$generated['generator'];
272 1 : $outputPath = P::m($this->tmpDirectoryPath, $tmpSubdirectory, (string)$generated['output']);
273 1 : $this->xmlGenerator->setDriver($type);
274 1 : $this->xmlGenerator->generate($outputPath, $this->rootDirectory, $settings);
275 :
276 : // add generated file into list of files
277 1 : $files[(string)$generated['id']] = $outputPath;
278 1 : }
279 :
280 1 : return $files;
281 : }
282 :
283 :
284 : }
|