class PackageManagerRequirementsHooks

Requirements checks for Package Manager.

Hierarchy

Expanded class hierarchy of PackageManagerRequirementsHooks

File

core/modules/package_manager/src/Hook/PackageManagerRequirementsHooks.php, line 17

Namespace

Drupal\package_manager\Hook
View source
class PackageManagerRequirementsHooks {
    use StringTranslationTrait;
    public function __construct(ComposerInspector $composerInspector, ExecutableFinderInterface $executableFinder) {
    }
    
    /**
     * Implements hook_runtime_requirements().
     */
    public function runtime() : array {
        $requirements = [];
        $requirements = $this->checkSettings($requirements);
        $requirements = $this->checkFailure($requirements);
        // Report the Composer version in use, as well as its path.
        $title = $this->t('Composer version');
        try {
            $requirements['package_manager_composer'] = [
                'title' => $title,
                'description' => $this->t('@version (<code>@path</code>)', [
                    '@version' => $this->composerInspector
                        ->getVersion(),
                    '@path' => $this->executableFinder
                        ->find('composer'),
                ]),
                'severity' => REQUIREMENT_INFO,
            ];
        } catch (\Throwable $e) {
            // All Composer Stager exceptions are translatable.
            $message = $e instanceof ExceptionInterface ? $e->getTranslatableMessage() : $e->getMessage();
            $requirements['package_manager_composer'] = [
                'title' => $title,
                'description' => $this->t('Composer was not found. The error message was: @message', [
                    '@message' => $message,
                ]),
                'severity' => REQUIREMENT_ERROR,
            ];
        }
        return $requirements;
    }
    
    /**
     * Implements hook_update_requirements().
     */
    public function update() : array {
        $requirements = [];
        $requirements = $this->checkSettings($requirements);
        $requirements = $this->checkFailure($requirements);
        return $requirements;
    }
    
    /**
     * Check that package manager has an explicit setting to allow installation.
     *
     * @param array $requirements
     *   The requirements array that has been processed so far.
     *
     * @return array
     *   Requirements array.
     *
     * @see hook_runtime_requirements
     * @see hook_update_requirements
     */
    public function checkSettings($requirements) : array {
        if (Settings::get('testing_package_manager', FALSE) === FALSE) {
            $requirements['testing_package_manager'] = [
                'title' => 'Package Manager',
                'description' => $this->t("Package Manager is available for early testing. To install the module set the value of 'testing_package_manager' to TRUE in your settings.php file."),
                'severity' => REQUIREMENT_ERROR,
            ];
        }
        return $requirements;
    }
    
    /**
     * Check for a failed update.
     *
     * This is run during requirements to allow restoring from backup.
     *
     * @param array $requirements
     *   The requirements array that has been processed so far.
     *
     * @return array
     *   Requirements array.
     *
     * @see hook_runtime_requirements
     * @see hook_update_requirements
     */
    public function checkFailure(array $requirements) : array {
        // If we're able to check for the presence of the failure marker at all, do
        // it irrespective of the current run phase. If the failure marker is there,
        // the site is in an indeterminate state and should be restored from backup
        // ASAP.
        $service_id = FailureMarker::class;
        if (\Drupal::hasService($service_id)) {
            try {
                \Drupal::service($service_id)->assertNotExists(NULL);
            } catch (FailureMarkerExistsException $exception) {
                $requirements['package_manager_failure_marker'] = [
                    'title' => $this->t('Failed Package Manager update detected'),
                    'description' => $exception->getMessage(),
                    'severity' => REQUIREMENT_ERROR,
                ];
            }
        }
        return $requirements;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
PackageManagerRequirementsHooks::checkFailure public function Check for a failed update.
PackageManagerRequirementsHooks::checkSettings public function Check that package manager has an explicit setting to allow installation.
PackageManagerRequirementsHooks::runtime public function Implements hook_runtime_requirements().
PackageManagerRequirementsHooks::update public function Implements hook_update_requirements().
PackageManagerRequirementsHooks::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language. 1

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.