From b9b8dc17ac6ac05b7a9ded7487f0379b394a41ed Mon Sep 17 00:00:00 2001
From: Claudiu Cristea <clau.cristea@gmail.com>
Date: Mon, 28 Oct 2013 19:25:03 +0200
Subject: [PATCH] Issue #1069140 by claudiu.cristea | parasox: Requirements
 should be provided by image toolkit.

---
 .../Core/ImageToolkit/ImageToolkitInterface.php    | 14 +++++++
 core/modules/image/image.install                   | 47 +++++++++++-----------
 .../system/Plugin/ImageToolkit/GDToolkit.php       | 21 ++++++++++
 .../image_test/Plugin/ImageToolkit/TestToolkit.php |  7 ++++
 4 files changed, 65 insertions(+), 24 deletions(-)

diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
index 2b86aef..214923f 100644
--- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
+++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
@@ -174,6 +174,20 @@ public function save(ImageInterface $image, $destination);
   public function getInfo(ImageInterface $image);
 
   /**
+   * Gets toolkit requirements in a format suitable for hook_requirements().
+   *
+   * @return array
+   *   An associative requirements array as is returned by hook_requirements().
+   *   If the toolkit claims no requirements to the system, returns an empty
+   *   array. The array can have arbitrary keys and they do not have to be
+   *   prefixed by e.g. the module name or toolkit ID, as the system will make
+   *   the keys globally unique.
+   *
+   * @see hook_requirements()
+   */
+  public function requirements();
+
+  /**
    * Verifies Image Toolkit is set up correctly.
    *
    * @return bool
diff --git a/core/modules/image/image.install b/core/modules/image/image.install
index 9a14749..043d09a 100644
--- a/core/modules/image/image.install
+++ b/core/modules/image/image.install
@@ -29,30 +29,29 @@ function image_uninstall() {
  * @param $phase
  */
 function image_requirements($phase) {
-  $requirements = array();
-
-  if ($phase == 'runtime') {
-    // Check for the PHP GD library.
-    if (function_exists('imagegd2')) {
-      $info = gd_info();
-      $requirements['image_gd'] = array(
-        'value' => $info['GD Version'],
-      );
-
-      // Check for filter and rotate support.
-      if (!function_exists('imagefilter') || !function_exists('imagerotate')) {
-        $requirements['image_gd']['severity'] = REQUIREMENT_WARNING;
-        $requirements['image_gd']['description'] = t('The GD Library for PHP is enabled, but was compiled without support for functions used by the rotate and desaturate effects. It was probably compiled using the official GD libraries from http://www.libgd.org instead of the GD library bundled with PHP. You should recompile PHP --with-gd using the bundled GD library. See <a href="@url">the PHP manual</a>.', array('@url' => 'http://www.php.net/manual/book.image.php'));
-      }
-    }
-    else {
-      $requirements['image_gd'] = array(
-        'value' => t('Not installed'),
-        'severity' => REQUIREMENT_ERROR,
-        'description' => t('The GD library for PHP is missing or outdated. Check the <a href="@url">PHP image documentation</a> for information on how to correct this.', array('@url' => 'http://www.php.net/manual/book.image.php')),
-      );
-    }
-    $requirements['image_gd']['title'] = t('GD library rotate and desaturate effects');
+  if ($phase != 'runtime') {
+    return array();
+  }
+
+  $toolkit = \Drupal::service('image.toolkit.manager')->getDefaultToolkit();
+  $plugin_definition = $toolkit ? $toolkit->getPluginDefinition() : NULL;
+  $requirements = array(
+    'image.toolkit' => array(
+      'title' => t('Image toolkit'),
+      'value' => $toolkit ? $toolkit->getPluginId() : t('None'),
+      'description' => $toolkit ? $plugin_definition['title'] : t("No image toolkit is configured on the site. Check PHP installed extensions or add other contributed toolkit that doesn't require a PHP extension and make sure that at least one valid image toolkit is enabled."),
+      'severity' => $toolkit ? REQUIREMENT_INFO : REQUIREMENT_ERROR,
+    ),
+  );
+
+  // If there no valid active toolkit exit here.
+  if (!$toolkit) {
+    return $requirements;
+  }
+
+  foreach ($toolkit->requirements() as $key => $requirement) {
+    $namespaced_key = 'image.toolkit.' . $toolkit->getPluginId() . '.' . $key;
+    $requirements[$namespaced_key] = $requirement;
   }
 
   return $requirements;
diff --git a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
index 737ad27..e78075b 100644
--- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
+++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
@@ -275,6 +275,27 @@ public function createTmp(ImageInterface $image, $width, $height) {
   /**
    * {@inheritdoc}
    */
+  public function requirements() {
+    $requirements = array();
+
+    $info = gd_info();
+    $requirements['rotate_and_desaturate'] = array(
+      'title' => t('GD library rotate and desaturate effects'),
+      'value' => $info['GD Version'],
+    );
+
+    // Check for filter and rotate support.
+    if (!function_exists('imagefilter') || !function_exists('imagerotate')) {
+      $requirements['rotate_and_desaturate']['severity'] = REQUIREMENT_WARNING;
+      $requirements['rotate_and_desaturate']['description'] = t('The GD Library for PHP is enabled, but was compiled without support for functions used by the rotate and desaturate effects. It was probably compiled using the official GD libraries from http://www.libgd.org instead of the GD library bundled with PHP. You should recompile PHP --with-gd using the bundled GD library. See <a href="@url">the PHP manual</a>.', array('@url' => 'http://www.php.net/manual/book.image.php'));
+    }
+
+    return $requirements;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public static function isAvailable() {
     if ($check = get_extension_funcs('gd')) {
       if (in_array('imagegd2', $check)) {
diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php
index 43800617..d2fcdef 100644
--- a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php
+++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/TestToolkit.php
@@ -126,6 +126,13 @@ protected function logCall($op, $args) {
   /**
    * {@inheritdoc}
    */
+  public function requirements() {
+    return array();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public static function isAvailable() {
     return TRUE;
   }
-- 
1.8.3.1

