1: <?php
2:
3: 4: 5: 6: 7: 8:
9:
10: namespace XSLTBenchmarking\TestsGenerator;
11:
12: require_once __DIR__ . '/../Exceptions.php';
13: require_once LIBS . '/PhpPath/PhpPath.min.php';
14: require_once ROOT . '/Printer.php';
15:
16: use PhpPath\P;
17: use XSLTBenchmarking\Printer;
18:
19: 20: 21: 22: 23:
24: class Generator
25: {
26:
27: 28: 29: 30: 31:
32: private $factory;
33:
34: 35: 36: 37: 38:
39: private $params;
40:
41: 42: 43: 44: 45:
46: private $templating;
47:
48: 49: 50: 51: 52:
53: private $paramsTest;
54:
55: 56: 57: 58: 59:
60: private $templatesDirectory;
61:
62: 63: 64: 65: 66:
67: private $testsDirectory;
68:
69: 70: 71: 72: 73:
74: private $templates = array();
75:
76:
77: 78: 79: 80: 81: 82: 83: 84: 85: 86:
87: public function __construct(
88: \XSLTBenchmarking\Factory $factory,
89: \XSLTBenchmarking\TestsGenerator\Params $params,
90: \XSLTBenchmarking\TestsGenerator\Templating $templating,
91: \XSLTBenchmarking\TestsRunner\Params $paramsTest,
92: $templatesDirectory,
93: $testsDirectory)
94: {
95: P::cd($templatesDirectory);
96: P::cd($testsDirectory);
97:
98: $this->factory = $factory;
99: $this->params = $params;
100: $this->templating = $templating;
101: $this->paramsTest = $paramsTest;
102: $this->templatesDirectory = $templatesDirectory;
103: $this->testsDirectory = $testsDirectory;
104: }
105:
106:
107: 108: 109: 110: 111:
112: public function getTests()
113: {
114: return $this->templates;
115: }
116:
117:
118: 119: 120: 121: 122: 123: 124: 125: 126: 127:
128: public function addTests($templateDirectory, $testParamsFile = '__params.xml')
129: {
130: $testParamsPath = P::mcf($this->templatesDirectory, $templateDirectory, $testParamsFile);
131: $this->params->setFile($testParamsPath);
132: $templateName = $this->params->getTemplateName();
133: $templatePath = $this->params->getTemplatePath();
134: $templatingType = $this->params->getTemplatingType();
135: foreach ($this->params->getTestsNames() as $testName)
136: {
137: $fullName = $templateName . ' - ' . $testName;
138: if (isset($this->templates[$fullName]))
139: {
140: throw new \XSLTBenchmarking\CollisionException('Duplicate name of test "' . $fullName . '"');
141: }
142:
143: $test = $this->factory->getTestsGeneratorTest($fullName);
144: $test->setTemplatePath($templatePath);
145: $test->setTemplatingType($templatingType);
146: $test->setPath($this->testsDirectory);
147: $test->addFilesPaths($this->params->getTestFilesPaths($testName));
148: $test->addSettings($this->params->getTestSettings($testName));
149: $test->setParamsFilePath($this->params->getTestParamsFileName($testName));
150: $this->templates[$fullName] = $test;
151: }
152: }
153:
154:
155: 156: 157: 158: 159: 160: 161:
162: public function generateAll($verbose = FALSE)
163: {
164: $tests = $this->getTests();
165: $testsCount = count($tests);
166: $testsGenerated = 0;
167: foreach ($tests as $test)
168: {
169: $regenerate = $this->generateTest($test);
170: $testsGenerated++;
171: if ($verbose)
172: {
173: $preffix = $testsGenerated . '/' . $testsCount . " - ";
174: if ($regenerate)
175: {
176: Printer::info($preffix . 'Tests from template "' . $test->getName() . '" were regenerated.');
177: }
178: else
179: {
180: Printer::info($preffix . 'Tests from template "' . $test->getName() . '" were generated.');
181: }
182: }
183: }
184:
185: return $testsCount;
186: }
187:
188:
189: 190: 191: 192: 193: 194: 195:
196: private function generateTest(Test $test)
197: {
198: $destinationDirectory = $test->getPath();
199: $regenrated = TRUE;
200: if (!is_dir($destinationDirectory))
201: {
202: mkdir($destinationDirectory);
203: $regenrated = FALSE;
204: }
205:
206:
207: foreach ($test->getFilesPaths() as $inputPath => $outputPath)
208: {
209: $destinationInputPath = P::m($destinationDirectory, basename($inputPath));
210: $destinationOutputPath = P::m($destinationDirectory, basename($outputPath));
211: copy($inputPath, $destinationInputPath);
212: copy($outputPath, $destinationOutputPath);
213: }
214:
215:
216: $this->templating->setDriver($test->getTemplatingType());
217: $this->templating->generate($test->getTemplatePath(), $test->getXsltPath(), $test->getSettings());
218:
219:
220: $this->paramsTest->setFile($test->getParamsFilePath(), FALSE);
221: $this->paramsTest->generate(
222: $test->getName(),
223: $test->getXsltName(),
224: $test->getFilesPaths()
225: );
226:
227: return $regenrated;
228: }
229:
230:
231: }
232: