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
44 // ----- Static initialization -----
47 * The initialization process loads any `*.civi-setup.php` files and
48 * fires the `civi.setup.init` event.
50 * @param array $modelValues
51 * List of default configuration options.
52 * Recommended fields: 'srcPath', 'cms'
53 * @param callable $pluginCallback
54 * Function which manipulates the list of plugin files.
55 * Use this to add, remove, or re-order callbacks.
56 * function(array $files) => array
57 * Ex: ['hello' => '/var/www/plugins/hello.civi-setup.php']
58 * @param \Psr\Log\LoggerInterface $log
60 public static function init($modelValues = array(), $pluginCallback = NULL, $log = NULL) {
61 if (!defined('CIVI_SETUP')) {
62 define('CIVI_SETUP', 1);
65 self
::$instance = new Setup();
66 self
::$instance->model
= new \Civi\Setup\
Model();
67 self
::$instance->model
->setValues($modelValues);
68 self
::$instance->dispatcher
= new EventDispatcher();
69 self
::$instance->log
= $log ?
$log : new NullLogger();
71 $pluginDir = dirname(__DIR__
) . '/plugins';
72 $pluginFiles = array();
73 foreach (['*.civi-setup.php', '*/*.civi-setup.php'] as $pattern) {
74 foreach ((array) glob("$pluginDir/$pattern") as $file) {
75 $key = substr($file, strlen($pluginDir) +
1);
76 $key = preg_replace('/\.civi-setup\.php$/', '', $key);
77 $pluginFiles[$key] = $file;
82 if ($pluginCallback) {
83 $pluginFiles = $pluginCallback($pluginFiles);
86 foreach ($pluginFiles as $pluginFile) {
87 self
::$instance->log
->debug('[Setup.php] Load plugin {file}', array(
88 'file' => $pluginFile,
93 $event = new InitEvent(self
::$instance->getModel());
94 self
::$instance->getDispatcher()->dispatch('civi.setup.init', $event);
95 // return $event; ...or... return self::$instance;
99 * Assert that this copy of civicrm-setup is compatible with the client.
101 * @param string $expectedVersion
104 public static function assertProtocolCompatibility($expectedVersion) {
105 if (version_compare(self
::PROTOCOL
, $expectedVersion, '<')) {
106 throw new InitException(sprintf("civicrm-setup is running protocol v%s. This application expects civicrm-setup to support protocol v%s.", self
::PROTOCOL
, $expectedVersion));
108 list ($actualFirst) = explode('.', self
::PROTOCOL
);
109 list ($expectedFirst) = explode('.', $expectedVersion);
110 if ($actualFirst > $expectedFirst) {
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));
116 * Assert that the "Setup" subsystem is running.
118 * This function is mostly just a placeholder -- in practice, if
119 * someone makes a failed call to `assertRunning()`, it will probably
120 * manifest as an unknown class/function. But this gives us a pretty,
121 * one-line, syntactically-valid way to make the assertion.
123 public static function assertRunning() {
124 if (!defined('CIVI_SETUP')) {
125 exit("Installation plugins must only be loaded by the installer.\n");
132 public static function instance() {
133 if (self
::$instance === NULL) {
134 throw new InitException('\Civi\Setup has not been initialized.');
136 return self
::$instance;
140 * @return \Psr\Log\LoggerInterface
142 public static function log() {
143 return self
::instance()->getLog();
147 * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
149 public static function dispatcher() {
150 return self
::instance()->getDispatcher();
156 * Determine whether the current CMS user is authorized to perform
159 * @return \Civi\Setup\Event\CheckAuthorizedEvent
161 public function checkAuthorized() {
162 $event = new CheckAuthorizedEvent($this->getModel());
163 return $this->getDispatcher()->dispatch('civi.setup.checkAuthorized', $event);
167 * Determine whether the local environment meets system requirements.
169 * @return \Civi\Setup\Event\CheckRequirementsEvent
171 public function checkRequirements() {
172 $event = new CheckRequirementsEvent($this->getModel());
173 return $this->getDispatcher()->dispatch('civi.setup.checkRequirements', $event);
177 * Determine whether the setting and/or schema are already installed.
179 * @return \Civi\Setup\Event\CheckInstalledEvent
181 public function checkInstalled() {
182 $event = new CheckInstalledEvent($this->getModel());
183 return $this->getDispatcher()->dispatch('civi.setup.checkInstalled', $event);
187 * Create the settings file.
189 * @return \Civi\Setup\Event\InstallFilesEvent
191 public function installFiles() {
192 $event = new InstallFilesEvent($this->getModel());
193 return $this->getDispatcher()->dispatch('civi.setup.installFiles', $event);
197 * Create the database schema.
199 * @return \Civi\Setup\Event\InstallDatabaseEvent
201 public function installDatabase() {
202 $event = new InstallDatabaseEvent($this->getModel());
203 return $this->getDispatcher()->dispatch('civi.setup.installDatabase', $event);
207 * Remove the settings file.
209 * @return \Civi\Setup\Event\UninstallFilesEvent
211 public function uninstallFiles() {
212 $event = new UninstallFilesEvent($this->getModel());
213 return $this->getDispatcher()->dispatch('civi.setup.uninstallFiles', $event);
217 * Remove the database schema.
219 * @return \Civi\Setup\Event\UninstallDatabaseEvent
221 public function uninstallDatabase() {
222 $event = new UninstallDatabaseEvent($this->getModel());
223 return $this->getDispatcher()->dispatch('civi.setup.uninstallDatabase', $event);
227 * Create a page-controller for a web-based installation form.
229 * @return \Civi\Setup\UI\Event\UIConstructEvent
231 public function createController() {
232 $event = new UIConstructEvent($this->getModel());
233 return $this->getDispatcher()->dispatch('civi.setupui.construct', $event);
236 // ----- Accessors -----
239 * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
241 public function getDispatcher() {
242 return $this->dispatcher
;
246 * @return \Civi\Setup\Model
248 public function getModel() {
253 * @return \Psr\Log\LoggerInterface
255 public function getLog() {