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 Symfony\Component\EventDispatcher\EventDispatcher
;
19 const PROTOCOL
= '1.0';
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 EventDispatcher();
74 self
::$instance->log
= $log ?
$log : new NullLogger();
76 $pluginDir = dirname(__DIR__
) . '/plugins';
77 $pluginFiles = array();
78 foreach (['*.civi-setup.php', '*/*.civi-setup.php'] as $pattern) {
79 foreach ((array) glob("$pluginDir/$pattern") as $file) {
80 $key = substr($file, strlen($pluginDir) +
1);
81 $key = preg_replace('/\.civi-setup\.php$/', '', $key);
82 $pluginFiles[$key] = $file;
87 if ($pluginCallback) {
88 $pluginFiles = $pluginCallback($pluginFiles);
91 foreach ($pluginFiles as $pluginFile) {
92 self
::$instance->log
->debug('[Setup.php] Load plugin {file}', array(
93 'file' => $pluginFile,
98 $event = new InitEvent(self
::$instance->getModel());
99 self
::$instance->getDispatcher()->dispatch('civi.setup.init', $event);
100 // return $event; ...or... return self::$instance;
104 * Assert that this copy of civicrm-setup is compatible with the client.
106 * @param string $expectedVersion
109 public static function assertProtocolCompatibility($expectedVersion) {
110 if (version_compare(self
::PROTOCOL
, $expectedVersion, '<')) {
111 throw new InitException(sprintf("civicrm-setup is running protocol v%s. This application expects civicrm-setup to support protocol v%s.", self
::PROTOCOL
, $expectedVersion));
113 list ($actualFirst) = explode('.', self
::PROTOCOL
);
114 list ($expectedFirst) = explode('.', $expectedVersion);
115 if ($actualFirst > $expectedFirst) {
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));
121 * Assert that the "Setup" subsystem is running.
123 * This function is mostly just a placeholder -- in practice, if
124 * someone makes a failed call to `assertRunning()`, it will probably
125 * manifest as an unknown class/function. But this gives us a pretty,
126 * one-line, syntactically-valid way to make the assertion.
128 public static function assertRunning() {
129 if (!defined('CIVI_SETUP')) {
130 exit("Installation plugins must only be loaded by the installer.\n");
137 public static function instance() {
138 if (self
::$instance === NULL) {
139 throw new InitException('\Civi\Setup has not been initialized.');
141 return self
::$instance;
145 * @return \Psr\Log\LoggerInterface
147 public static function log() {
148 return self
::instance()->getLog();
152 * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
154 public static function dispatcher() {
155 return self
::instance()->getDispatcher();
161 * Determine whether the current CMS user is authorized to perform
164 * @return \Civi\Setup\Event\CheckAuthorizedEvent
166 public function checkAuthorized() {
167 $event = new CheckAuthorizedEvent($this->getModel());
168 return $this->getDispatcher()->dispatch('civi.setup.checkAuthorized', $event);
172 * Determine whether the local environment meets system requirements.
174 * @return \Civi\Setup\Event\CheckRequirementsEvent
176 public function checkRequirements() {
177 $event = new CheckRequirementsEvent($this->getModel());
178 return $this->getDispatcher()->dispatch('civi.setup.checkRequirements', $event);
182 * Determine whether the setting and/or schema are already installed.
184 * @return \Civi\Setup\Event\CheckInstalledEvent
186 public function checkInstalled() {
187 $event = new CheckInstalledEvent($this->getModel());
188 return $this->getDispatcher()->dispatch('civi.setup.checkInstalled', $event);
192 * Create the settings file.
194 * @return \Civi\Setup\Event\InstallFilesEvent
196 public function installFiles() {
197 if ($this->pendingAction
!== NULL) {
198 throw new InitException(sprintf("Cannot begin action %s. Already executing %s.", __FUNCTION__
, $this->pendingAction
));
200 $this->pendingAction
= __FUNCTION__
;
203 $event = new InstallFilesEvent($this->getModel());
204 return $this->getDispatcher()->dispatch('civi.setup.installFiles', $event);
207 $this->pendingAction
= NULL;
212 * Create the database schema.
214 * @return \Civi\Setup\Event\InstallDatabaseEvent
216 public function installDatabase() {
217 if ($this->pendingAction
!== NULL) {
218 throw new InitException(sprintf("Cannot begin action %s. Already executing %s.", __FUNCTION__
, $this->pendingAction
));
220 $this->pendingAction
= __FUNCTION__
;
223 $event = new InstallDatabaseEvent($this->getModel());
224 return $this->getDispatcher()->dispatch('civi.setup.installDatabase', $event);
227 $this->pendingAction
= NULL;
232 * Remove the settings file.
234 * @return \Civi\Setup\Event\UninstallFilesEvent
236 public function uninstallFiles() {
237 if ($this->pendingAction
!== NULL) {
238 throw new InitException(sprintf("Cannot begin action %s. Already executing %s.", __FUNCTION__
, $this->pendingAction
));
240 $this->pendingAction
= __FUNCTION__
;
243 $event = new UninstallFilesEvent($this->getModel());
244 return $this->getDispatcher()->dispatch('civi.setup.uninstallFiles', $event);
247 $this->pendingAction
= NULL;
252 * Remove the database schema.
254 * @return \Civi\Setup\Event\UninstallDatabaseEvent
256 public function uninstallDatabase() {
257 if ($this->pendingAction
!== NULL) {
258 throw new InitException(sprintf("Cannot begin action %s. Already executing %s.", __FUNCTION__
, $this->pendingAction
));
260 $this->pendingAction
= __FUNCTION__
;
263 $event = new UninstallDatabaseEvent($this->getModel());
264 return $this->getDispatcher()->dispatch('civi.setup.uninstallDatabase', $event);
267 $this->pendingAction
= NULL;
272 * Create a page-controller for a web-based installation form.
274 * @return \Civi\Setup\UI\Event\UIConstructEvent
276 public function createController() {
277 $event = new UIConstructEvent($this->getModel());
278 return $this->getDispatcher()->dispatch('civi.setupui.construct', $event);
281 // ----- Accessors -----
284 * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
286 public function getDispatcher() {
287 return $this->dispatcher
;
291 * @return \Civi\Setup\Model
293 public function getModel() {
298 * @return \Psr\Log\LoggerInterface
300 public function getLog() {
305 * @return NULL|string
306 * The name of a pending installation action, or NULL if none are active.
307 * Ex: 'installDatabase', 'uninstallFiles'
309 public function getPendingAction() {
310 return $this->pendingAction
;