From d33b0397165f724d3c94ce37891cbe5a9a2bdceb Mon Sep 17 00:00:00 2001
From: Claudiu Cristea <clau.cristea@gmail.com>
Date: Mon, 21 Oct 2013 22:33:10 +0300
Subject: [PATCH] Issue #1069140 by claudiu.cristea | parasox: Requirements
 should be provided by image toolkit.

---
 .../Core/ImageToolkit/ImageToolkitInterface.php    | 16 +++++++++--
 .../Core/ImageToolkit/ImageToolkitManager.php      |  2 +-
 core/modules/image/image.install                   | 33 +++++++---------------
 .../system/Plugin/ImageToolkit/GDToolkit.php       | 32 +++++++++++++++++----
 .../Plugin/ImageToolkit/BrokenToolkit.php          |  2 +-
 .../image_test/Plugin/ImageToolkit/TestToolkit.php |  2 +-
 6 files changed, 53 insertions(+), 34 deletions(-)

diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
index 2b86aef..0ac928b 100644
--- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
+++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
@@ -174,12 +174,22 @@ public function save(ImageInterface $image, $destination);
   public function getInfo(ImageInterface $image);
 
   /**
-   * Verifies Image Toolkit is set up correctly.
+   * Verifies if the image toolkit is set up correctly.
+   *
+   * @param array $requirements
+   *   An associative requirements array passed by reference in the same format
+   *   as is returned by hook_requirements(). If the toolkit claims no
+   *   requirements to the system, no items will be added to this array. The
+   *   method can add new items with arbitrary keys. Those keys do not have to
+   *   be prefixed by e.g. the module name or toolkit ID, as the system will
+   *   make the keys globally unique.
    *
    * @return bool
-   *   True if the GD toolkit is available on this machine.
+   *   TRUE if the toolkit is available on this machine.
+   *
+   * @see hook_requirements()
    */
-  public static function isAvailable();
+  public static function availability(array &$requirements = array());
 
   /**
    * Returns a list of image types supported by the toolkit.
diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php
index 43686ff..3c53344 100644
--- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php
+++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitManager.php
@@ -84,7 +84,7 @@ public function getAvailableToolkits() {
     $output = array();
     foreach ($toolkits as $id => $definition) {
       // Only allow modules that aren't marked as unavailable.
-      if (call_user_func($definition['class'] . '::isAvailable')) {
+      if (call_user_func($definition['class'] . '::availability')) {
         $output[$id] = $definition;
       }
     }
diff --git a/core/modules/image/image.install b/core/modules/image/image.install
index 9a14749..4dba3b9 100644
--- a/core/modules/image/image.install
+++ b/core/modules/image/image.install
@@ -29,30 +29,17 @@ function image_uninstall() {
  * @param $phase
  */
 function image_requirements($phase) {
-  $requirements = array();
+  if ($phase != 'runtime') {
+    return 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');
+  $requirements = array();
+  $toolkit = \Drupal::service('image.toolkit.manager')->getDefaultToolkit();
+  $toolkit->availability($requirements);
+  foreach ($requirements as $key => $requirement) {
+    $namespaced_key = 'image.toolkit.' . $toolkit->getPluginId() . '.' . $key;
+    $requirements[$namespaced_key] = $requirement;
+    unset($requirements[$key]);
   }
 
   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..22bda91 100644
--- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
+++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
@@ -275,14 +275,36 @@ public function createTmp(ImageInterface $image, $width, $height) {
   /**
    * {@inheritdoc}
    */
-  public static function isAvailable() {
+  public static function availability(array &$requirements = array()) {
     if ($check = get_extension_funcs('gd')) {
-      if (in_array('imagegd2', $check)) {
-        // GD2 support is available.
-        return TRUE;
+      $available = in_array('imagegd2', $check);
+    }
+    else {
+      $available = FALSE;
+    }
+
+    if ($available) {
+      $info = gd_info();
+      $requirements['rotate_desaturate'] = array(
+        'value' => $info['GD Version'],
+      );
+
+      // Check for filter and rotate support.
+      if (!function_exists('imagefilter') || !function_exists('imagerotate')) {
+        $requirements['rotate_desaturate']['severity'] = REQUIREMENT_WARNING;
+        $requirements['rotate_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 FALSE;
+    else {
+      $requirements['rotate_desaturate'] = 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['rotate_desaturate']['title'] = t('GD library rotate and desaturate effects');
+
+    return $available;
   }
 
   /**
diff --git a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/BrokenToolkit.php b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/BrokenToolkit.php
index e5ead27..ae6d64d 100644
--- a/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/BrokenToolkit.php
+++ b/core/modules/system/tests/modules/image_test/lib/Drupal/image_test/Plugin/ImageToolkit/BrokenToolkit.php
@@ -20,7 +20,7 @@ class BrokenToolkit extends TestToolkit {
   /**
    * {@inheritdoc}
    */
-  public static function isAvailable() {
+  public static function availability(array &$requirements = array()) {
     return FALSE;
   }
 }
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..88c1731 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,7 +126,7 @@ protected function logCall($op, $args) {
   /**
    * {@inheritdoc}
    */
-  public static function isAvailable() {
+  public static function availability(array &$requirements = array()) {
     return TRUE;
   }
 
-- 
1.8.3.1

