/** * Activates the given plugin. * * @param \CMS\Core\Package\PluginPackage $plugin The plugin to enable * @return bool True on success */ protected function _enable(PluginPackage $plugin) { $checker = new RuleChecker((array) $plugin->composer['require']); if (!$checker->check()) { $this->err(__d('installer', 'Plugin "{0}" cannot be enabled as some dependencies are disabled or not installed: {1}', $plugin->humanName, $checker->fail(true))); return false; } // MENTAL NOTE: As plugin is disabled its listeners are not attached to the // system, so we need to manually attach them in order to trigger callbacks. if (!$this->params['no-callbacks']) { $this->_attachListeners($plugin->name, "{$plugin->path}/"); $trigger = $this->_triggerBeforeEvents($plugin); if (!$trigger) { return false; } } return $this->_finish($plugin); }
/** * Validates the content of working directory. * * @return bool True on success */ protected function _validateContent() { if (!$this->_workingDir) { return false; } $errors = []; if (!is_readable("{$this->_workingDir}src") || !is_dir("{$this->_workingDir}src")) { $errors[] = __d('installer', 'Invalid package, missing "src" directory.'); } if (!is_readable("{$this->_workingDir}composer.json")) { $errors[] = __d('installer', 'Invalid package, missing "composer.json" file.'); } else { $jsonErrors = Plugin::validateJson("{$this->_workingDir}composer.json", true); if (!empty($jsonErrors)) { $errors[] = __d('installer', 'Invalid "composer.json".'); $errors = array_merge($errors, (array) $jsonErrors); } else { $json = (new File("{$this->_workingDir}composer.json"))->read(); $json = json_decode($json, true); list(, $pluginName) = packageSplit($json['name'], true); if ($this->params['theme'] && !str_ends_with($pluginName, 'Theme')) { $this->err(__d('installer', 'The given package is not a valid theme.')); return false; } elseif (!$this->params['theme'] && str_ends_with($pluginName, 'Theme')) { $this->err(__d('installer', 'The given package is not a valid plugin.')); return false; } $this->_plugin = ['name' => $pluginName, 'packageName' => $json['name'], 'type' => str_ends_with($pluginName, 'Theme') ? 'theme' : 'plugin', 'composer' => $json]; if (Plugin::exists($this->_plugin['name'])) { $exists = plugin($this->_plugin['name']); if ($exists->status) { $errors[] = __d('installer', '{0} "{1}" is already installed.', [$this->_plugin['type'] == 'plugin' ? __d('installer', 'The plugin') : __d('installer', 'The theme'), $this->_plugin['name']]); } else { $errors[] = __d('installer', '{0} "{1}" is already installed but disabled, maybe you want try to enable it?.', [$this->_plugin['type'] == 'plugin' ? __d('installer', 'The plugin') : __d('installer', 'The theme'), $this->_plugin['name']]); } } if ($this->_plugin['type'] == 'theme' && !is_readable("{$this->_workingDir}webroot/screenshot.png")) { $errors[] = __d('installer', 'Missing "screenshot.png" file.'); } if (isset($json['require'])) { $checker = new RuleChecker($json['require']); if (!$checker->check()) { $errors[] = __d('installer', '{0} "{1}" depends on other packages, plugins or libraries that were not found: {2}', [$this->_plugin['type'] == 'plugin' ? __d('installer', 'The plugin') : __d('installer', 'The theme'), $this->_plugin['name'], $checker->fail(true)]); } } } } if (!file_exists(ROOT . '/plugins') || !is_dir(ROOT . '/plugins') || !is_writable(ROOT . '/plugins')) { $errors[] = __d('installer', 'Write permissions required for directory: {0}.', [ROOT . '/plugins/']); } foreach ($errors as $message) { $this->err($message); } return empty($errors); }