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;
11 :
12 : require_once LIBS . '/PhpPath/PhpPath.min.php';
13 : require_once ROOT . '/Exceptions.php';
14 :
15 : use PhpPath\P;
16 :
17 : /**
18 : * Class for better work with drivers.
19 : *
20 : * @author Viktor Mašíček <viktor@masicek.net>
21 : */
22 : abstract class DriversContainer
23 : {
24 :
25 :
26 : /**
27 : * Instance of selected driver
28 : *
29 : * @var IDriver|ADriver
30 : */
31 : protected $driver = NULL;
32 :
33 : /**
34 : * Arguments for construct of each driver
35 : *
36 : * @var array
37 : */
38 : private $args = array();
39 :
40 :
41 : /**
42 : * Save arguments for pass into each driver
43 : */
44 : public function __construct()
45 : {
46 14 : $this->args = func_get_args();
47 14 : }
48 :
49 :
50 : /**
51 : * Make new object of driver selected by name.
52 : *
53 : * @param string $driverName Name of driver for using
54 : *
55 : * @return this
56 : */
57 : public function setDriver($driverName)
58 : {
59 : // create driver file name
60 6 : $driverName = ucfirst(strtolower($driverName));
61 6 : $driverNameSuffix = $this->getDriversNamesSuffix();
62 6 : $fileName = $driverName . $driverNameSuffix . '.php';
63 :
64 : // require selected driver
65 6 : $filePath = P::mcf($this->getDriversDirectory(), $fileName);
66 6 : require_once $filePath;
67 :
68 : // create class name
69 6 : $driverNamespace = $this->getDriversNamespace();
70 6 : if ($driverNamespace && ($driverNamespace[strlen($driverNamespace) - 1] !== '\\'))
71 6 : {
72 6 : $driverNamespace = $driverNamespace . '\\';
73 6 : }
74 6 : $className = $driverNamespace . $driverName . $driverNameSuffix;
75 :
76 : // create new instance of driver with parameters
77 6 : $reflection = new \ReflectionClass($className);
78 6 : $args = func_get_args();
79 6 : unset($args[0]);
80 6 : $args = array_merge($this->args, $args); // TODO do test for join arguments
81 6 : $this->driver = $reflection->newInstanceArgs($args);
82 :
83 6 : return $this;
84 : }
85 :
86 :
87 : /**
88 : * Call drivers method.
89 : *
90 : * @param string $name Name of method
91 : * @param array $arguments List of arguments for calling method
92 : *
93 : * @throws \XSLTBenchmarking\UnknownMethodException Unknown method on set driver
94 : * @return mix
95 : */
96 : public function __call($name, $arguments)
97 : {
98 2 : if (!in_array($name, get_class_methods($this->driver)))
99 2 : {
100 2 : throw new \XSLTBenchmarking\UnknownMethodException('On driver "' . get_class($this->driver) . '" is not method "' . $name . '"');
101 : }
102 :
103 2 : return call_user_func_array(array($this->driver, $name), $arguments);
104 : }
105 :
106 :
107 : /**
108 : * Return directory containing drivers.
109 : *
110 : * @return string
111 : */
112 : protected function getDriversDirectory()
113 : {
114 6 : $reflection = new \ReflectionClass($this);
115 6 : return dirname($reflection->getFileName());
116 : }
117 :
118 :
119 : /**
120 : * Return namespace of drivers
121 : *
122 : * @return string
123 : */
124 : protected function getDriversNamespace()
125 : {
126 6 : $reflection = new \ReflectionClass($this);
127 6 : return $reflection->getNamespaceName();
128 : }
129 :
130 :
131 : /**
132 : * Return drivers suffix of name
133 : *
134 : * @return string
135 : */
136 : protected function getDriversNamesSuffix()
137 : {
138 6 : $reflection = new \ReflectionClass($this);
139 6 : return $reflection->getShortName() . 'Driver';
140 : }
141 :
142 :
143 : }
|