4 use Civi\Setup\Event\CheckAuthorizedEvent
;
5 use Civi\Setup\Event\CheckRequirementsEvent
;
6 use Civi\Setup\Event\CheckInstalledEvent
;
7 use Civi\Setup\UI\Event\UIConstructEvent
;
8 use Civi\Setup\Event\InitEvent
;
9 use Civi\Setup\Event\InstallDatabaseEvent
;
10 use Civi\Setup\Event\InstallFilesEvent
;
11 use Civi\Setup\Event\UninstallDatabaseEvent
;
12 use Civi\Setup\Event\UninstallFilesEvent
;
13 use Civi\Setup\Exception\InitException
;
14 use Psr\Log\NullLogger
;
15 use Civi\Core\CiviEventDispatcher
;
19 const PROTOCOL
= '1.1';
21 const PRIORITY_START
= 2000;
22 const PRIORITY_PREPARE
= 1000;
23 const PRIORITY_MAIN
= 0;
24 const PRIORITY_LATE
= -1000;
25 const PRIORITY_END
= -2000;
27 private static $instance;
30 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
32 protected $dispatcher;
35 * @var \Civi\Setup\Model
40 * @var \Psr\Log\LoggerInterface
47 protected $pendingAction = NULL;
49 // ----- Static initialization -----
52 * The initialization process loads any `*.civi-setup.php` files and
53 * fires the `civi.setup.init` event.
55 * @param array $modelValues
56 * List of default configuration options.
57 * Recommended fields: 'srcPath', 'cms'
58 * @param callable $pluginCallback
59 * Function which manipulates the list of plugin files.
60 * Use this to add, remove, or re-order callbacks.
61 * function(array $files) => array
62 * Ex: ['hello' => '/var/www/plugins/hello.civi-setup.php']
63 * @param \Psr\Log\LoggerInterface $log
65 public static function init($modelValues = array(), $pluginCallback = NULL, $log = NULL) {
66 if (!defined('CIVI_SETUP')) {
67 define('CIVI_SETUP', 1);
70 self
::$instance = new Setup();
71 self
::$instance->model
= new \Civi\Setup\
Model();
72 self
::$instance->model
->setValues($modelValues);
73 self
::$instance->dispatcher
= new CiviEventDispatcher();
74 self
::$instance->dispatcher
->setDispatchPolicy([
75 '/^civi\.setup\./' => 'run',
76 '/^civi\.setupui\./' => 'run',
79 self
::$instance->log
= $log ?
$log : new NullLogger();
81 $pluginDir = dirname(__DIR__
) . '/plugins';
82 $pluginFiles = array();
83 foreach (['*.civi-setup.php', '*/*.civi-setup.php'] as $pattern) {
84 foreach ((array) glob("$pluginDir/$pattern") as $file) {
85 $key = substr($file, strlen($pluginDir) +
1);
86 $key = preg_replace('/\.civi-setup\.php$/', '', $key);
87 $pluginFiles[$key] = $file;
92 if ($pluginCallback) {
93 $pluginFiles = $pluginCallback($pluginFiles);
96 foreach ($pluginFiles as $pluginFile) {
97 self
::$instance->log
->debug('[Setup.php] Load plugin {file}', array(
98 'file' => $pluginFile,
103 $event = new InitEvent(self
::$instance->getModel());
104 self
::$instance->getDispatcher()->dispatch('civi.setup.init', $event);
105 // return $event; ...or... return self::$instance;
109 * Assert that this copy of civicrm-setup is compatible with the client.
111 * @param string $expectedVersion
114 public static function assertProtocolCompatibility($expectedVersion) {
115 if (version_compare(self
::PROTOCOL
, $expectedVersion, '<')) {
116 throw new InitException(sprintf("civicrm-setup is running protocol v%s. This application expects civicrm-setup to support protocol v%s.", self
::PROTOCOL
, $expectedVersion));
118 list ($actualFirst) = explode('.', self
::PROTOCOL
);
119 list ($expectedFirst) = explode('.', $expectedVersion);
120 if ($actualFirst > $expectedFirst) {
121 throw new InitException(sprintf("civicrm-setup is running protocol v%s. This application expects civicrm-setup to support protocol v%s.", self
::PROTOCOL
, $expectedVersion));
126 * Assert that the "Setup" subsystem is running.
128 * This function is mostly just a placeholder -- in practice, if
129 * someone makes a failed call to `assertRunning()`, it will probably
130 * manifest as an unknown class/function. But this gives us a pretty,
131 * one-line, syntactically-valid way to make the assertion.
133 public static function assertRunning() {
134 if (!defined('CIVI_SETUP')) {
135 exit("Installation plugins must only be loaded by the installer.\n");
142 public static function instance() {
143 if (self
::$instance === NULL) {
144 throw new InitException('\Civi\Setup has not been initialized.');
146 return self
::$instance;
150 * @return \Psr\Log\LoggerInterface
152 public static function log() {
153 return self
::instance()->getLog();
157 * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
159 public static function dispatcher() {
160 return self
::instance()->getDispatcher();
166 * Determine whether the current CMS user is authorized to perform
169 * @return \Civi\Setup\Event\CheckAuthorizedEvent
171 public function checkAuthorized() {
172 $event = new CheckAuthorizedEvent($this->getModel());
173 return $this->getDispatcher()->dispatch('civi.setup.checkAuthorized', $event);
177 * Determine whether the local environment meets system requirements.
179 * @return \Civi\Setup\Event\CheckRequirementsEvent
181 public function checkRequirements() {
182 $event = new CheckRequirementsEvent($this->getModel());
183 return $this->getDispatcher()->dispatch('civi.setup.checkRequirements', $event);
187 * Determine whether the setting and/or schema are already installed.
189 * @return \Civi\Setup\Event\CheckInstalledEvent
191 public function checkInstalled() {
192 $event = new CheckInstalledEvent($this->getModel());
193 return $this->getDispatcher()->dispatch('civi.setup.checkInstalled', $event);
197 * Create the settings file.
199 * @return \Civi\Setup\Event\InstallFilesEvent
201 public function installFiles() {
202 if ($this->pendingAction
!== NULL) {
203 throw new InitException(sprintf("Cannot begin action %s. Already executing %s.", __FUNCTION__
, $this->pendingAction
));
205 $this->pendingAction
= __FUNCTION__
;
208 $event = new InstallFilesEvent($this->getModel());
209 return $this->getDispatcher()->dispatch('civi.setup.installFiles', $event);
212 $this->pendingAction
= NULL;
217 * Create the database schema.
219 * @return \Civi\Setup\Event\InstallDatabaseEvent
221 public function installDatabase() {
222 if ($this->pendingAction
!== NULL) {
223 throw new InitException(sprintf("Cannot begin action %s. Already executing %s.", __FUNCTION__
, $this->pendingAction
));
225 $this->pendingAction
= __FUNCTION__
;
228 $event = new InstallDatabaseEvent($this->getModel());
229 return $this->getDispatcher()->dispatch('civi.setup.installDatabase', $event);
232 $this->pendingAction
= NULL;
237 * Remove the settings file.
239 * @return \Civi\Setup\Event\UninstallFilesEvent
241 public function uninstallFiles() {
242 if ($this->pendingAction
!== NULL) {
243 throw new InitException(sprintf("Cannot begin action %s. Already executing %s.", __FUNCTION__
, $this->pendingAction
));
245 $this->pendingAction
= __FUNCTION__
;
248 $event = new UninstallFilesEvent($this->getModel());
249 return $this->getDispatcher()->dispatch('civi.setup.uninstallFiles', $event);
252 $this->pendingAction
= NULL;
257 * Remove the database schema.
259 * @return \Civi\Setup\Event\UninstallDatabaseEvent
261 public function uninstallDatabase() {
262 if ($this->pendingAction
!== NULL) {
263 throw new InitException(sprintf("Cannot begin action %s. Already executing %s.", __FUNCTION__
, $this->pendingAction
));
265 $this->pendingAction
= __FUNCTION__
;
268 $event = new UninstallDatabaseEvent($this->getModel());
269 return $this->getDispatcher()->dispatch('civi.setup.uninstallDatabase', $event);
272 $this->pendingAction
= NULL;
277 * Create a page-controller for a web-based installation form.
279 * @return \Civi\Setup\UI\Event\UIConstructEvent
281 public function createController() {
282 $event = new UIConstructEvent($this->getModel());
283 return $this->getDispatcher()->dispatch('civi.setupui.construct', $event);
286 // ----- Accessors -----
289 * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
291 public function getDispatcher() {
292 return $this->dispatcher
;
296 * @return \Civi\Setup\Model
298 public function getModel() {
303 * @return \Psr\Log\LoggerInterface
305 public function getLog() {
310 * @return NULL|string
311 * The name of a pending installation action, or NULL if none are active.
312 * Ex: 'installDatabase', 'uninstallFiles'
314 public function getPendingAction() {
315 return $this->pendingAction
;