3 namespace Drupal\Driver;
5 use Drupal\Driver\Exception\BootstrapException;
7 use Behat\Behat\Tester\Exception\PendingException;
10 * Fully bootstraps Drupal and uses native API calls.
12 class DrupalDriver implements DriverInterface, SubDriverFinderInterface {
15 * Track whether Drupal has been bootstrapped.
19 private $bootstrapped = FALSE;
24 * @var \Drupal\Driver\Cores\CoreInterface
29 * System path to the Drupal installation.
36 * URI for the Drupal installation.
43 * Drupal core version.
50 * Set Drupal root and URI.
52 * @param string $drupal_root
53 * The Drupal root path.
55 * The URI for the Drupal installation.
57 * @throws BootstrapException
58 * Thrown when the Drupal installation is not found in the given root path.
60 public function __construct($drupal_root, $uri) {
61 $this->drupalRoot = realpath($drupal_root);
62 if (!$this->drupalRoot) {
63 throw new BootstrapException(sprintf('No Drupal installation found at %s', $drupal_root));
66 $this->version = $this->getDrupalVersion();
72 public function getRandom() {
73 return $this->getCore()->getRandom();
79 public function bootstrap() {
80 $this->getCore()->bootstrap();
81 $this->bootstrapped = TRUE;
87 public function isBootstrapped() {
88 // Assume the blackbox is always bootstrapped.
89 return $this->bootstrapped;
95 public function userCreate(\stdClass $user) {
96 $this->getCore()->userCreate($user);
102 public function userDelete(\stdClass $user) {
103 $this->getCore()->userDelete($user);
109 public function processBatch() {
110 $this->getCore()->processBatch();
116 public function userAddRole(\stdClass $user, $role_name) {
117 $this->getCore()->userAddRole($user, $role_name);
123 public function fetchWatchdog($count = 10, $type = NULL, $severity = NULL) {
124 throw new PendingException(sprintf('Currently no ability to access watchdog entries in %s', $this));
130 public function clearCache($type = NULL) {
131 $this->getCore()->clearCache();
137 public function getSubDriverPaths() {
138 // Ensure system is bootstrapped.
139 if (!$this->isBootstrapped()) {
143 return $this->getCore()->getExtensionPathList();
147 * Determine major Drupal version.
150 * The major Drupal version.
152 * @throws \Drupal\Driver\Exception\BootstrapException
153 * Thrown when the Drupal version could not be determined.
155 * @see drush_drupal_version()
157 public function getDrupalVersion() {
158 if (!isset($this->version)) {
159 // Support 6, 7 and 8.
160 $version_constant_paths = array(
162 '/modules/system/system.module',
164 '/includes/bootstrap.inc',
167 '/core/includes/bootstrap.inc',
170 if ($this->drupalRoot === FALSE) {
171 throw new BootstrapException('`drupal_root` parameter must be defined.');
174 foreach ($version_constant_paths as $path) {
175 if (file_exists($this->drupalRoot . $path)) {
176 require_once $this->drupalRoot . $path;
179 if (defined('VERSION')) {
182 elseif (defined('\Drupal::VERSION')) {
183 $version = \Drupal::VERSION;
186 throw new BootstrapException('Unable to determine Drupal core version. Supported versions are 6, 7, and 8.');
189 // Extract the major version from VERSION.
190 $version_parts = explode('.', $version);
191 if (is_numeric($version_parts[0])) {
192 $this->version = (integer) $version_parts[0];
195 throw new BootstrapException(sprintf('Unable to extract major Drupal core version from version string %s.', $version));
198 return $this->version;
202 * Instantiate and set Drupal core class.
204 * @param array $available_cores
205 * A major-version-keyed array of available core controllers.
207 public function setCore(array $available_cores) {
208 if (!isset($available_cores[$this->version])) {
209 throw new BootstrapException(sprintf('There is no available Drupal core controller for Drupal version %s.', $this->version));
211 $this->core = $available_cores[$this->version];
215 * Automatically set the core from the current version.
217 public function setCoreFromVersion() {
218 $core = '\Drupal\Driver\Cores\Drupal' . $this->getDrupalVersion();
219 $this->core = new $core($this->drupalRoot, $this->uri);
223 * Return current core.
225 public function getCore() {
232 public function createNode($node) {
233 return $this->getCore()->nodeCreate($node);
239 public function nodeDelete($node) {
240 return $this->getCore()->nodeDelete($node);
246 public function runCron() {
247 if (!$this->getCore()->runCron()) {
248 throw new \Exception('Failed to run cron.');
255 public function createTerm(\stdClass $term) {
256 return $this->getCore()->termCreate($term);
262 public function termDelete(\stdClass $term) {
263 return $this->getCore()->termDelete($term);
269 public function roleCreate(array $permissions) {
270 return $this->getCore()->roleCreate($permissions);
276 public function roleDelete($rid) {
277 $this->getCore()->roleDelete($rid);
283 public function isField($entity_type, $field_name) {
284 return $this->getCore()->isField($entity_type, $field_name);
290 public function languageCreate($language) {
291 return $this->getCore()->languageCreate($language);
297 public function languageDelete($language) {
298 $this->getCore()->languageDelete($language);
304 public function configGet($name, $key) {
305 return $this->getCore()->configGet($name, $key);
311 public function configSet($name, $key, $value) {
312 $this->getCore()->configSet($name, $key, $value);
318 public function clearStaticCaches() {
319 $this->getCore()->clearStaticCaches();