forked from joachim-n/drupal-core-development-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDevelopmentProjectCommands.php
121 lines (101 loc) · 3.85 KB
/
DevelopmentProjectCommands.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Drush\Commands\core_development;
use Composer\Autoload\ClassLoader;
use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Psr\Container\ContainerInterface as DrushContainer;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Replaces the cache:rebuild command to correctly work with symlinked Drupal.
*
* This prevents contrib modules from vanishing when the cache:rebuild command
* is run.
*
* For maintainability, changes from the original command method in
* Drush\Commands\core\CacheCommands are marked 'CHANGE'.
*/
class DevelopmentProjectCommands extends DrushCommands {
public function __construct(
private ClassLoader $autoloader
) {
parent::__construct();
}
public static function createEarly(DrushContainer $drush_container): self {
$commandHandler = new static(
$drush_container->get('loader')
);
return $commandHandler;
}
/**
* @hook replace-command cache:rebuild
*/
public function rebuild($options = ['cache-clear' => true]) {
if (!$options['cache-clear']) {
$this->logger()->info(dt("Skipping cache-clear operation due to --no-cache-clear option."));
return true;
}
// CHANGE: Get the app root ourselves instead of using DRUPAL_ROOT.
$app_root = $this->getAppRoot();
chdir($this->getAppRoot());
// We no longer clear APC and similar caches as they are useless on CLI.
// See https://github.com/drush-ops/drush/pull/2450
require_once DRUSH_DRUPAL_CORE . '/includes/utility.inc';
$request = Drush::bootstrap()->getRequest();
DrupalKernel::bootEnvironment();
// Avoid 'Only variables should be passed by reference'
// CHANGE: Don't use DRUPAL_ROOT.
$root = $app_root;
$site_path = DrupalKernel::findSitePath($request);
Settings::initialize($root, $site_path, $this->autoloader);
// drupal_rebuild() calls drupal_flush_all_caches() itself, so we don't do it manually.
// CHANGE: call our own version of drupal_rebuild().
$this->drupal_rebuild($this->autoloader, $request);
$this->logger()->success(dt('Cache rebuild complete.'));
}
/**
* Replacement for drupal_rebuild().
*
* This passes the app root to DrupalKernel.
*/
function drupal_rebuild($class_loader, Request $request) {
// Remove Drupal's error and exception handlers; they rely on a working
// service container and other subsystems and will only cause a fatal error
// that hides the actual error.
restore_error_handler();
restore_exception_handler();
// Invalidate the container.
// Bootstrap up to where caches exist and clear them.
// CHANGE: Pass the correct app root to DrupalKernel.
$kernel = new DrupalKernel('prod', $class_loader, TRUE, $this->getAppRoot());
$kernel->setSitePath(DrupalKernel::findSitePath($request));
$kernel->invalidateContainer();
$kernel->boot();
$kernel->preHandle($request);
// Ensure our request includes the session if appropriate.
if (PHP_SAPI !== 'cli') {
$request->setSession($kernel->getContainer()->get('session'));
}
drupal_flush_all_caches($kernel);
// Disable recording of cached pages.
\Drupal::service('page_cache_kill_switch')->trigger();
// Restore Drupal's error and exception handlers.
// @see \Drupal\Core\DrupalKernel::boot()
set_error_handler('_drupal_error_handler');
set_exception_handler('_drupal_exception_handler');
}
/**
* Gets the app root.
*
* @return string
* The app root.
*/
protected function getAppRoot(): string {
// This core belongs to the project template, so we can hardcode the
// location of this file relative to the project root, and the scaffold
// location defined in the project's composer.json.
return dirname(__DIR__, 3) . '/web';
}
}