From b25e6f5cae2cefb3ccfc8610e7642bc3e35f1213 Mon Sep 17 00:00:00 2001
From: Claudiu Cristea <clau.cristea@gmail.com>
Date: Wed, 9 Oct 2013 12:45:05 +0300
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                   | 31 ++++++----------------
 .../system/Plugin/ImageToolkit/GDToolkit.php       | 30 +++++++++++++++++++++
 .../image_test/Plugin/ImageToolkit/TestToolkit.php |  7 +++++
 4 files changed, 59 insertions(+), 23 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..2ef40a2 100644
--- a/core/modules/image/image.install
+++ b/core/modules/image/image.install
@@ -29,30 +29,15 @@ 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');
+  $toolkit = \Drupal::service('image.toolkit.manager')->getDefaultToolkit();
+  $requirements = array();
+  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..96e9e75 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,36 @@ public function createTmp(ImageInterface $image, $width, $height) {
   /**
    * {@inheritdoc}
    */
+  public function requirements() {
+    $requirements = array();
+
+    if (function_exists('imagegd2')) {
+      $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'));
+      }
+    }
+    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 $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

