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