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(['/^civi\.setup\./' => 'run', '/./' => 'fail']);
75 self
::$instance->log
= $log ?
$log : new NullLogger();
77 $pluginDir = dirname(__DIR__
) . '/plugins';
78 $pluginFiles = array();
79 foreach (['*.civi-setup.php', '*/*.civi-setup.php'] as $pattern) {
80 foreach ((array) glob("$pluginDir/$pattern") as $file) {
81 $key = substr($file, strlen($pluginDir) +
1);
82 $key = preg_replace('/\.civi-setup\.php$/', '', $key);
83 $pluginFiles[$key] = $file;
88 if ($pluginCallback) {
89 $pluginFiles = $pluginCallback($pluginFiles);
92 foreach ($pluginFiles as $pluginFile) {
93 self
::$instance->log
->debug('[Setup.php] Load plugin {file}', array(
94 'file' => $pluginFile,
99 $event = new InitEvent(self
::$instance->getModel());
100 self
::$instance->getDispatcher()->dispatch('civi.setup.init', $event);
101 // return $event; ...or... return self::$instance;
105 * Assert that this copy of civicrm-setup is compatible with the client.
107 * @param string $expectedVersion
110 public static function assertProtocolCompatibility($expectedVersion) {
111 if (version_compare(self
::PROTOCOL
, $expectedVersion, '<')) {
112 throw new InitException(sprintf("civicrm-setup is running protocol v%s. This application expects civicrm-setup to support protocol v%s.", self
::PROTOCOL
, $expectedVersion));
114 list ($actualFirst) = explode('.', self
::PROTOCOL
);
115 list ($expectedFirst) = explode('.', $expectedVersion);
116 if ($actualFirst > $expectedFirst) {
117 throw new InitException(sprintf("civicrm-setup is running protocol v%s. This application expects civicrm-setup to support protocol v%s.", self
::PROTOCOL
, $expectedVersion));
122 * Assert that the "Setup" subsystem is running.
124 * This function is mostly just a placeholder -- in practice, if
125 * someone makes a failed call to `assertRunning()`, it will probably
126 * manifest as an unknown class/function. But this gives us a pretty,
127 * one-line, syntactically-valid way to make the assertion.
129 public static function assertRunning() {
130 if (!defined('CIVI_SETUP')) {
131 exit("Installation plugins must only be loaded by the installer.\n");
138 public static function instance() {
139 if (self
::$instance === NULL) {
140 throw new InitException('\Civi\Setup has not been initialized.');
142 return self
::$instance;
146 * @return \Psr\Log\LoggerInterface
148 public static function log() {
149 return self
::instance()->getLog();
153 * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
155 public static function dispatcher() {
156 return self
::instance()->getDispatcher();
162 * Determine whether the current CMS user is authorized to perform
165 * @return \Civi\Setup\Event\CheckAuthorizedEvent
167 public function checkAuthorized() {
168 $event = new CheckAuthorizedEvent($this->getModel());
169 return $this->getDispatcher()->dispatch('civi.setup.checkAuthorized', $event);
173 * Determine whether the local environment meets system requirements.
175 * @return \Civi\Setup\Event\CheckRequirementsEvent
177 public function checkRequirements() {
178 $event = new CheckRequirementsEvent($this->getModel());
179 return $this->getDispatcher()->dispatch('civi.setup.checkRequirements', $event);
183 * Determine whether the setting and/or schema are already installed.
185 * @return \Civi\Setup\Event\CheckInstalledEvent
187 public function checkInstalled() {
188 $event = new CheckInstalledEvent($this->getModel());
189 return $this->getDispatcher()->dispatch('civi.setup.checkInstalled', $event);
193 * Create the settings file.
195 * @return \Civi\Setup\Event\InstallFilesEvent
197 public function installFiles() {
198 if ($this->pendingAction
!== NULL) {
199 throw new InitException(sprintf("Cannot begin action %s. Already executing %s.", __FUNCTION__
, $this->pendingAction
));
201 $this->pendingAction
= __FUNCTION__
;
204 $event = new InstallFilesEvent($this->getModel());
205 return $this->getDispatcher()->dispatch('civi.setup.installFiles', $event);
208 $this->pendingAction
= NULL;
213 * Create the database schema.
215 * @return \Civi\Setup\Event\InstallDatabaseEvent
217 public function installDatabase() {
218 if ($this->pendingAction
!== NULL) {
219 throw new InitException(sprintf("Cannot begin action %s. Already executing %s.", __FUNCTION__
, $this->pendingAction
));
221 $this->pendingAction
= __FUNCTION__
;
224 $event = new InstallDatabaseEvent($this->getModel());
225 return $this->getDispatcher()->dispatch('civi.setup.installDatabase', $event);
228 $this->pendingAction
= NULL;
233 * Remove the settings file.
235 * @return \Civi\Setup\Event\UninstallFilesEvent
237 public function uninstallFiles() {
238 if ($this->pendingAction
!== NULL) {
239 throw new InitException(sprintf("Cannot begin action %s. Already executing %s.", __FUNCTION__
, $this->pendingAction
));
241 $this->pendingAction
= __FUNCTION__
;
244 $event = new UninstallFilesEvent($this->getModel());
245 return $this->getDispatcher()->dispatch('civi.setup.uninstallFiles', $event);
248 $this->pendingAction
= NULL;
253 * Remove the database schema.
255 * @return \Civi\Setup\Event\UninstallDatabaseEvent
257 public function uninstallDatabase() {
258 if ($this->pendingAction
!== NULL) {
259 throw new InitException(sprintf("Cannot begin action %s. Already executing %s.", __FUNCTION__
, $this->pendingAction
));
261 $this->pendingAction
= __FUNCTION__
;
264 $event = new UninstallDatabaseEvent($this->getModel());
265 return $this->getDispatcher()->dispatch('civi.setup.uninstallDatabase', $event);
268 $this->pendingAction
= NULL;
273 * Create a page-controller for a web-based installation form.
275 * @return \Civi\Setup\UI\Event\UIConstructEvent
277 public function createController() {
278 $event = new UIConstructEvent($this->getModel());
279 return $this->getDispatcher()->dispatch('civi.setupui.construct', $event);
282 // ----- Accessors -----
285 * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
287 public function getDispatcher() {
288 return $this->dispatcher
;
292 * @return \Civi\Setup\Model
294 public function getModel() {
299 * @return \Psr\Log\LoggerInterface
301 public function getLog() {
306 * @return NULL|string
307 * The name of a pending installation action, or NULL if none are active.
308 * Ex: 'installDatabase', 'uninstallFiles'
310 public function getPendingAction() {
311 return $this->pendingAction
;