From 282092c49c7a5b45455f071c3d45bcacd33ae1f9 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    | 15 +++++++++++
 core/modules/image/image.install                   | 31 ++++++----------------
 .../system/Plugin/ImageToolkit/GDToolkit.php       | 30 +++++++++++++++++++++
 .../image_test/Plugin/ImageToolkit/TestToolkit.php |  7 +++++
 4 files changed, 60 insertions(+), 23 deletions(-)

diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
index 2b86aef..0e161f8 100644
--- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
+++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
@@ -174,6 +174,21 @@ 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 must have arbitrary and unique keys. Keys shouldn't be
+   *   prefixed by name and toolkit, the system automatically adds the module
+   *   name and toolkit id (e.g. 'image.toolkit.gd.' should not be added as
+   *   prefix to the array key).
+   *
+   * @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 fa8fe41..f4390fc 100644
--- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
+++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
@@ -277,6 +277,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 726384d..e8b2e62 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
@@ -128,6 +128,13 @@ protected function logCall($op, $args) {
   /**
    * {@inheritdoc}
    */
+  public function requirements() {
+    return array();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
   public static function isAvailable() {
     return TRUE;
   }
-- 
1.8.3.1

