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\TestsRunner;
11 :
12 : // @codeCoverageIgnoreStart
13 : require_once __DIR__ . '/AProcessorDriver.php';
14 : // @codeCoverageIgnoreEnd
15 :
16 : /**
17 : * Driver for "libxslt 1.1.26 - PHP"
18 : *
19 : * @author Viktor Mašíček <viktor@masicek.net>
20 : */
21 : class Libxslt1126phpProcessorDriver extends AProcessorDriver
22 : {
23 :
24 :
25 : /**
26 : * Prefix of command for running PHP on Linux with extension XSL.
27 : *
28 : * @var string
29 : */
30 : static private $linuxCommandPrefix = '';
31 :
32 :
33 : /**
34 : * Return flag, if the driver is available.
35 : *
36 : * @return bool
37 : */
38 : public function isAvailable()
39 : {
40 4 : switch (PHP_OS)
41 : {
42 4 : case self::OS_WIN:
43 4 : return FALSE;
44 : break;
45 :
46 0 : case self::OS_LINUX:
47 0 : $available = FALSE;
48 :
49 : // php is needed
50 0 : exec('php -v 2> /dev/null | grep \'PHP\' | wc -l', $output);
51 0 : if ($output[0] != '0')
52 0 : {
53 0 : $available = TRUE;
54 0 : }
55 :
56 : if ($available)
57 0 : {
58 : // xsl extension is needed
59 0 : $output = NULL;
60 0 : exec('php --ri xsl | grep -P \'(libxslt Version => 1.1.26)|(XSL => enabled)\' | wc -l', $output);
61 0 : if ($output[0] == '2')
62 0 : {
63 0 : $available = TRUE;
64 0 : }
65 : else
66 : {
67 0 : $available = FALSE;
68 : }
69 0 : }
70 :
71 0 : return $available;
72 :
73 : break;
74 :
75 0 : default:
76 0 : return FALSE;
77 : break;
78 0 : }
79 : }
80 :
81 :
82 : /**
83 : * Return template of command
84 : *
85 : * Templates substitutions:
86 : * [XSLT] = path of XSLT template for transformation
87 : * [INPUT] = path of input XML file
88 : * [OUTPUT] = path of generated output XML file
89 : * [ERROR] = path of file for eventual generated error message
90 : * [LIBS] = path of directory containing XSLT processors (libraries, command-line program etc.)
91 : *
92 : * @return string
93 : */
94 : public function getCommandTemplate()
95 : {
96 0 : switch (PHP_OS)
97 : {
98 0 : case self::OS_LINUX:
99 0 : $prefix = $this->getLinuxCommandPrefix();
100 0 : break;
101 :
102 0 : }
103 :
104 : $phpScript =
105 : 'function errorHandler($errno, $errstr)' .
106 0 : '{' .
107 0 : ' throw new \ErrorException(\'Transformation failed: \' . $errstr);' .
108 0 : '}' .
109 0 : 'set_error_handler(\'errorHandler\');' .
110 0 : '$errorMessage = \'\';' .
111 0 : 'try {' .
112 0 : ' libxml_use_internal_errors(TRUE);' .
113 0 : ' $processor = new \XSLTProcessor();' .
114 0 : ' $processor->importStylesheet(new \SimpleXMLElement(\'[XSLT]\', 0, TRUE));' .
115 0 : ' $outputXml = $processor->transformToXml(new \SimpleXMLElement(\'[INPUT]\', 0, TRUE));' .
116 0 : ' file_put_contents(\'[OUTPUT]\', $outputXml);' .
117 0 : '}' .
118 0 : 'catch (\ErrorException $e)' .
119 0 : '{' .
120 0 : ' restore_error_handler();' .
121 0 : ' $errorMessage = $e->getMessage();' .
122 0 : '}' .
123 0 : 'catch (\Exception $e)' .
124 0 : '{' .
125 0 : ' restore_error_handler();' .
126 0 : ' $error = libxml_get_last_error();' .
127 0 : ' $errorMessage = $error->message . \': line \' . $error->line . \', column \' . $error->column . \', file \' . $error->file;' .
128 0 : '}' .
129 0 : 'restore_error_handler();' .
130 0 : 'if ($errorMessage)' .
131 0 : '{' .
132 0 : ' file_put_contents(\'[ERROR]\', $errorMessage);' .
133 : '}'
134 0 : ;
135 :
136 0 : if (PHP_OS == self::OS_LINUX)
137 0 : {
138 0 : $phpScript = str_replace('\\', '\\\\', $phpScript);
139 0 : $phpScript = str_replace('$', '\\$', $phpScript);
140 0 : }
141 :
142 0 : $commandTemplate = $prefix . ' -r "' . $phpScript . '"';
143 :
144 0 : return $commandTemplate;
145 : }
146 :
147 :
148 : /**
149 : * Full name of processor (with version)
150 : *
151 : * @return string
152 : */
153 : public function getFullName()
154 : {
155 0 : return 'libxslt 1.1.26 - PHP';
156 : }
157 :
158 :
159 : /**
160 : * Return name of processor kernel.
161 : * Available kernels are const of this class with prefix "KERNEL_"
162 : *
163 : * Examples:
164 : * Saxon 6.5.5 -> Saxon
165 : * xsltproc -> libxslt
166 : *
167 : * @return string
168 : */
169 : public function getKernel()
170 : {
171 0 : return self::KERNEL_LIBXSLT;
172 : }
173 :
174 :
175 : /**
176 : * Return prefix of command for running PHP on Linux with extension XSL.
177 : * It expected, that XSL extnesion is available as buildin extension or included.
178 : *
179 : * @return string
180 : */
181 : private function getLinuxCommandPrefix()
182 : {
183 0 : if (!self::$linuxCommandPrefix)
184 0 : {
185 0 : exec('find /usr/lib/php5/ -type f | grep \'/xsl.so\'', $output);
186 0 : if (isset($output[0]) || $output[0])
187 0 : {
188 0 : self::$linuxCommandPrefix = 'php -n -d extension="' . $output[0] . '"';
189 0 : }
190 : else
191 : {
192 0 : self::$linuxCommandPrefix = 'php -n';
193 : }
194 0 : }
195 0 : return self::$linuxCommandPrefix;
196 : }
197 :
198 :
199 : }
|