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.23 - PHP"
18 : *
19 : * @author Viktor Mašíček <viktor@masicek.net>
20 : */
21 : class Libxslt1123phpProcessorDriver 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 TRUE;
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.23)|(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 2 : switch (PHP_OS)
97 : {
98 2 : case self::OS_WIN:
99 2 : $prefix = '"[LIBS]\Php\5.3.6\php.exe" -n -d extension="[PROCESSORS]\libxslt\1.1.23\php_xsl.dll"';
100 2 : break;
101 :
102 0 : case self::OS_LINUX:
103 0 : $prefix = $this->getLinuxCommandPrefix();
104 0 : break;
105 :
106 0 : }
107 :
108 : $phpScript =
109 : 'function errorHandler($errno, $errstr)' .
110 2 : '{' .
111 2 : ' throw new \ErrorException(\'Transformation failed: \' . $errstr);' .
112 2 : '}' .
113 2 : 'set_error_handler(\'errorHandler\');' .
114 2 : '$errorMessage = \'\';' .
115 2 : 'try {' .
116 2 : ' libxml_use_internal_errors(TRUE);' .
117 2 : ' $processor = new \XSLTProcessor();' .
118 2 : ' $processor->importStylesheet(new \SimpleXMLElement(\'[XSLT]\', 0, TRUE));' .
119 2 : ' $outputXml = $processor->transformToXml(new \SimpleXMLElement(\'[INPUT]\', 0, TRUE));' .
120 2 : ' file_put_contents(\'[OUTPUT]\', $outputXml);' .
121 2 : '}' .
122 2 : 'catch (\ErrorException $e)' .
123 2 : '{' .
124 2 : ' restore_error_handler();' .
125 2 : ' $errorMessage = $e->getMessage();' .
126 2 : '}' .
127 2 : 'catch (\Exception $e)' .
128 2 : '{' .
129 2 : ' restore_error_handler();' .
130 2 : ' $error = libxml_get_last_error();' .
131 2 : ' $errorMessage = $error->message . \': line \' . $error->line . \', column \' . $error->column . \', file \' . $error->file;' .
132 2 : '}' .
133 2 : 'restore_error_handler();' .
134 2 : 'if ($errorMessage)' .
135 2 : '{' .
136 2 : ' file_put_contents(\'[ERROR]\', $errorMessage);' .
137 : '}'
138 2 : ;
139 :
140 2 : if (PHP_OS == self::OS_LINUX)
141 2 : {
142 0 : $phpScript = str_replace('\\', '\\\\', $phpScript);
143 0 : $phpScript = str_replace('$', '\\$', $phpScript);
144 0 : }
145 :
146 2 : $commandTemplate = $prefix . ' -r "' . $phpScript . '"';
147 :
148 2 : return $commandTemplate;
149 : }
150 :
151 :
152 : /**
153 : * Full name of processor (with version)
154 : *
155 : * @return string
156 : */
157 : public function getFullName()
158 : {
159 3 : return 'libxslt 1.1.23 - PHP';
160 : }
161 :
162 :
163 : /**
164 : * Return name of processor kernel.
165 : * Available kernels are const of this class with prefix "KERNEL_"
166 : *
167 : * Examples:
168 : * Saxon 6.5.5 -> Saxon
169 : * xsltproc -> libxslt
170 : *
171 : * @return string
172 : */
173 : public function getKernel()
174 : {
175 1 : return self::KERNEL_LIBXSLT;
176 : }
177 :
178 :
179 : /**
180 : * Return prefix of command for running PHP on Linux with extension XSL.
181 : * It expected, that XSL extnesion is available as buildin extension or included.
182 : *
183 : * @return string
184 : */
185 : private function getLinuxCommandPrefix()
186 : {
187 0 : if (!self::$linuxCommandPrefix)
188 0 : {
189 0 : exec('find /usr/lib/php5/ -type f | grep \'/xsl.so\'', $output);
190 0 : if (isset($output[0]) || $output[0])
191 0 : {
192 0 : self::$linuxCommandPrefix = 'php -n -d extension="' . $output[0] . '"';
193 0 : }
194 : else
195 : {
196 0 : self::$linuxCommandPrefix = 'php -n';
197 : }
198 0 : }
199 0 : return self::$linuxCommandPrefix;
200 : }
201 :
202 :
203 : }
|