Overview
Namespaces
Classes
Exceptions
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: $this->args = func_get_args();
47: }
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: $driverName = ucfirst(strtolower($driverName));
61: $driverNameSuffix = $this->getDriversNamesSuffix();
62: $fileName = $driverName . $driverNameSuffix . '.php';
63:
64: // require selected driver
65: $filePath = P::mcf($this->getDriversDirectory(), $fileName);
66: require_once $filePath;
67:
68: // create class name
69: $driverNamespace = $this->getDriversNamespace();
70: if ($driverNamespace && ($driverNamespace[strlen($driverNamespace) - 1] !== '\\'))
71: {
72: $driverNamespace = $driverNamespace . '\\';
73: }
74: $className = $driverNamespace . $driverName . $driverNameSuffix;
75:
76: // create new instance of driver with parameters
77: $reflection = new \ReflectionClass($className);
78: $args = func_get_args();
79: unset($args[0]);
80: $args = array_merge($this->args, $args); // TODO do test for join arguments
81: $this->driver = $reflection->newInstanceArgs($args);
82:
83: 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: if (!in_array($name, get_class_methods($this->driver)))
99: {
100: throw new \XSLTBenchmarking\UnknownMethodException('On driver "' . get_class($this->driver) . '" is not method "' . $name . '"');
101: }
102:
103: 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: $reflection = new \ReflectionClass($this);
115: return dirname($reflection->getFileName());
116: }
117:
118:
119: /**
120: * Return namespace of drivers
121: *
122: * @return string
123: */
124: protected function getDriversNamespace()
125: {
126: $reflection = new \ReflectionClass($this);
127: return $reflection->getNamespaceName();
128: }
129:
130:
131: /**
132: * Return drivers suffix of name
133: *
134: * @return string
135: */
136: protected function getDriversNamesSuffix()
137: {
138: $reflection = new \ReflectionClass($this);
139: return $reflection->getShortName() . 'Driver';
140: }
141:
142:
143: }
144: