From e789ffe8d271477bb46d7a77ddf7a87c3dbc2ca2 Mon Sep 17 00:00:00 2001
From: Claudiu Cristea <clau.cristea@gmail.com>
Date: Tue, 31 Dec 2013 17:05:58 +0100
Subject: [PATCH] Issue #1069140 by claudiu.cristea | parasox: Requirements
 should be provided by image toolkit.

---
 .../Drupal/Core/ImageToolkit/ImageToolkitBase.php  | 21 +++++++++
 .../Core/ImageToolkit/ImageToolkitInterface.php    | 14 ++++++
 core/modules/image/image.install                   | 52 +++++++++++++---------
 .../system/Plugin/ImageToolkit/GDToolkit.php       | 26 +++++++++--
 .../image_test/Plugin/ImageToolkit/TestToolkit.php |  5 +--
 5 files changed, 90 insertions(+), 28 deletions(-)
 create mode 100644 core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php

diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php
new file mode 100644
index 0000000..4f28a6e
--- /dev/null
+++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php
@@ -0,0 +1,21 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\ImageToolkit\ImageToolkitBase.
+ */
+
+namespace Drupal\Core\ImageToolkit;
+
+use Drupal\Core\Plugin\PluginBase;
+
+abstract class ImageToolkitBase extends PluginBase implements ImageToolkitInterface {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getRequirements() {
+    return array();
+  }
+
+}
diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
index 351d6d6..a5f96f3 100644
--- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
+++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php
@@ -217,6 +217,20 @@ public function scaleAndCrop(ImageInterface $image, $width, $height);
   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 getRequirements();
+
+  /**
    * Verifies Image Toolkit is set up correctly.
    *
    * @return bool
diff --git a/core/modules/image/image.install b/core/modules/image/image.install
index c48a834..8afc37c 100644
--- a/core/modules/image/image.install
+++ b/core/modules/image/image.install
@@ -29,30 +29,38 @@ 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'],
-      );
+  if ($phase != 'runtime') {
+    return array();
+  }
 
-      // 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'),
+  $toolkit = \Drupal::service('image.toolkit.manager')->getDefaultToolkit();
+  if ($toolkit) {
+    $plugin_definition = $toolkit->getPluginDefinition();
+    $requirements = array(
+      'image.toolkit' => array(
+        'title' => t('Image toolkit'),
+        'value' => $toolkit->getPluginId(),
+        'description' => $plugin_definition['title'],
+      ),
+    );
+  }
+  else {
+    $requirements = array(
+      'image.toolkit' => array(
+        'title' => t('Image toolkit'),
+        'value' => t('None'),
+        'description' => t("No image toolkit is configured on the site. Check PHP installed extensions or add a contributed toolkit that doesn't require a PHP extension. Make sure that at least one valid image toolkit is enabled."),
         '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 there's no valid active toolkit exit here.
+    return $requirements;
+  }
+
+  foreach ($toolkit->getRequirements() 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 5499c94..e5c5528 100644
--- a/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
+++ b/core/modules/system/lib/Drupal/system/Plugin/ImageToolkit/GDToolkit.php
@@ -7,9 +7,8 @@
 
 namespace Drupal\system\Plugin\ImageToolkit;
 
-use Drupal\Core\Plugin\PluginBase;
 use Drupal\Core\Image\ImageInterface;
-use Drupal\Core\ImageToolkit\ImageToolkitInterface;
+use Drupal\Core\ImageToolkit\ImageToolkitBase;
 use Drupal\Component\Utility\Image as ImageUtility;
 
 /**
@@ -20,7 +19,7 @@
  *   title = @Translation("GD2 image manipulation toolkit")
  * )
  */
-class GDToolkit extends PluginBase implements ImageToolkitInterface {
+class GDToolkit extends ImageToolkitBase {
 
   /**
    * {@inheritdoc}
@@ -325,6 +324,27 @@ public function createTmp(ImageInterface $image, $width, $height) {
   /**
    * {@inheritdoc}
    */
+  public function getRequirements() {
+    $requirements = array();
+
+    $info = gd_info();
+    $requirements['version'] = array(
+      'title' => t('GD library'),
+      'value' => $info['GD Version'],
+    );
+
+    // Check for filter and rotate support.
+    if (!function_exists('imagefilter') || !function_exists('imagerotate')) {
+      $requirements['version']['severity'] = REQUIREMENT_WARNING;
+      $requirements['version']['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() {
     // GD2 support is available.
     return function_exists('imagegd2');
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 15040a8..d677d13 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
@@ -7,9 +7,8 @@
 
 namespace Drupal\image_test\Plugin\ImageToolkit;
 
-use Drupal\Core\Plugin\PluginBase;
 use Drupal\Core\Image\ImageInterface;
-use Drupal\Core\ImageToolkit\ImageToolkitInterface;
+use Drupal\Core\ImageToolkit\ImageToolkitBase;
 
 /**
  * Defines a Test toolkit for image manipulation within Drupal.
@@ -19,7 +18,7 @@
  *   title = @Translation("A dummy toolkit that works")
  * )
  */
-class TestToolkit extends PluginBase implements ImageToolkitInterface {
+class TestToolkit extends ImageToolkitBase {
 
   /**
    * {@inheritdoc}
-- 
1.8.5.2

