1 | <?php |
||
18 | class DrupalDriver implements DriverInterface, SubDriverFinderInterface { |
||
19 | |||
20 | /** |
||
21 | * Track whether Drupal has been bootstrapped. |
||
22 | * |
||
23 | * @var bool |
||
24 | */ |
||
25 | private $bootstrapped = FALSE; |
||
26 | |||
27 | /** |
||
28 | * Drupal core object. |
||
29 | * |
||
30 | * @var \Drupal\Driver\Cores\CoreInterface |
||
31 | */ |
||
32 | public $core; |
||
33 | |||
34 | /** |
||
35 | * System path to the Drupal installation. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $drupalRoot; |
||
40 | |||
41 | /** |
||
42 | * URI for the Drupal installation. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $uri; |
||
47 | |||
48 | /** |
||
49 | * Drupal core version. |
||
50 | * |
||
51 | * @var integer |
||
52 | */ |
||
53 | public $version; |
||
54 | |||
55 | /** |
||
56 | * Set Drupal root and URI. |
||
57 | * |
||
58 | * @param string $drupal_root |
||
59 | * The Drupal root path. |
||
60 | * @param string $uri |
||
61 | * The URI for the Drupal installation. |
||
62 | * |
||
63 | * @throws BootstrapException |
||
64 | * Thrown when the Drupal installation is not found in the given root path. |
||
65 | */ |
||
66 | public function __construct($drupal_root, $uri) { |
||
67 | $this->drupalRoot = realpath($drupal_root); |
||
68 | if (!$this->drupalRoot) { |
||
69 | throw new BootstrapException(sprintf('No Drupal installation found at %s', $drupal_root)); |
||
70 | } |
||
71 | $this->uri = $uri; |
||
72 | $this->version = $this->getDrupalVersion(); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function getRandom() { |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function bootstrap() { |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | public function isBootstrapped() { |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | public function userCreate(\stdClass $user) { |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function userDelete(\stdClass $user) { |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function processBatch() { |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | public function userAddRole(\stdClass $user, $role_name) { |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | public function fetchWatchdog($count = 10, $type = NULL, $severity = NULL) { |
||
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | public function clearCache($type = NULL) { |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function getSubDriverPaths() { |
||
144 | // Ensure system is bootstrapped. |
||
145 | if (!$this->isBootstrapped()) { |
||
146 | $this->bootstrap(); |
||
147 | } |
||
148 | |||
149 | return $this->getCore()->getExtensionPathList(); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Determine major Drupal version. |
||
154 | * |
||
155 | * @return int |
||
156 | * The major Drupal version. |
||
157 | * |
||
158 | * @throws \Drupal\Driver\Exception\BootstrapException |
||
159 | * Thrown when the Drupal version could not be determined. |
||
160 | * |
||
161 | * @see drush_drupal_version() |
||
162 | */ |
||
163 | public function getDrupalVersion() { |
||
164 | if (!isset($this->version)) { |
||
165 | // Support 6, 7 and 8. |
||
166 | $version_constant_paths = array( |
||
167 | // Drupal 6. |
||
168 | '/modules/system/system.module', |
||
169 | // Drupal 7. |
||
170 | '/includes/bootstrap.inc', |
||
171 | // Drupal 8. |
||
172 | '/autoload.php', |
||
173 | '/core/includes/bootstrap.inc', |
||
174 | ); |
||
175 | |||
176 | if ($this->drupalRoot === FALSE) { |
||
177 | throw new BootstrapException('`drupal_root` parameter must be defined.'); |
||
178 | } |
||
179 | |||
180 | foreach ($version_constant_paths as $path) { |
||
181 | if (file_exists($this->drupalRoot . $path)) { |
||
182 | require_once $this->drupalRoot . $path; |
||
183 | } |
||
184 | } |
||
185 | if (defined('VERSION')) { |
||
186 | $version = VERSION; |
||
187 | } |
||
188 | elseif (defined('\Drupal::VERSION')) { |
||
189 | $version = \Drupal::VERSION; |
||
190 | } |
||
191 | else { |
||
192 | throw new BootstrapException('Unable to determine Drupal core version. Supported versions are 6, 7, and 8.'); |
||
193 | } |
||
194 | |||
195 | // Extract the major version from VERSION. |
||
196 | $version_parts = explode('.', $version); |
||
197 | if (is_numeric($version_parts[0])) { |
||
198 | $this->version = (integer) $version_parts[0]; |
||
199 | } |
||
200 | else { |
||
201 | throw new BootstrapException(sprintf('Unable to extract major Drupal core version from version string %s.', $version)); |
||
202 | } |
||
203 | } |
||
204 | return $this->version; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Instantiate and set Drupal core class. |
||
209 | * |
||
210 | * @param array $available_cores |
||
211 | * A major-version-keyed array of available core controllers. |
||
212 | */ |
||
213 | public function setCore(array $available_cores) { |
||
214 | if (!isset($available_cores[$this->version])) { |
||
215 | throw new BootstrapException(sprintf('There is no available Drupal core controller for Drupal version %s.', $this->version)); |
||
216 | } |
||
217 | $this->core = $available_cores[$this->version]; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Automatically set the core from the current version. |
||
222 | */ |
||
223 | public function setCoreFromVersion() { |
||
227 | |||
228 | /** |
||
229 | * Return current core. |
||
230 | */ |
||
231 | public function getCore() { |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | public function createNode($node) { |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | public function nodeDelete($node) { |
||
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | public function runCron() { |
||
253 | if (!$this->getCore()->runCron()) { |
||
254 | throw new \Exception('Failed to run cron.'); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | public function createTerm(\stdClass $term) { |
||
264 | |||
265 | /** |
||
266 | * {@inheritdoc} |
||
267 | */ |
||
268 | public function termDelete(\stdClass $term) { |
||
271 | |||
272 | /** |
||
273 | * {@inheritdoc} |
||
274 | */ |
||
275 | public function roleCreate(array $permissions) { |
||
278 | |||
279 | /** |
||
280 | * {@inheritdoc} |
||
281 | */ |
||
282 | public function roleDelete($rid) { |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | */ |
||
289 | public function isField($entity_type, $field_name) { |
||
292 | |||
293 | /** |
||
294 | * {@inheritdoc} |
||
295 | */ |
||
296 | public function languageCreate($language) { |
||
299 | |||
300 | /** |
||
301 | * {@inheritdoc} |
||
302 | */ |
||
303 | public function languageDelete($language) { |
||
306 | |||
307 | /** |
||
308 | * {@inheritdoc} |
||
309 | */ |
||
310 | public function configSet($name, $key, $value) { |
||
311 | $this->getCore()->configSet($name, $key, $value); |
||
313 | |||
314 | /** |
||
315 | * {@inheritdoc} |
||
316 | */ |
||
317 | public function configGet($name, $key) { |
||
320 | |||
321 | /** |
||
322 | * {@inheritdoc} |
||
323 | */ |
||
324 | public function clearStaticCaches() { |
||
327 | |||
328 | } |
||
329 |