diff --git a/core/lib/Drupal/Core/Image/ImageFactory.php b/core/lib/Drupal/Core/Image/ImageFactory.php index 992a2e0..29c2870 100644 --- a/core/lib/Drupal/Core/Image/ImageFactory.php +++ b/core/lib/Drupal/Core/Image/ImageFactory.php @@ -89,7 +89,7 @@ public function get($source = NULL, $toolkit_id = NULL) { $toolkit_id = $toolkit_id ?: $this->toolkitId; $image = $this->toolkitManager->createInstance($toolkit_id); if ($source) { - $image->parseFile($source); + $image->createFromSource($source); } return $image; } diff --git a/core/lib/Drupal/Core/Image/ImageInterface.php b/core/lib/Drupal/Core/Image/ImageInterface.php index b630c3d..ad3c241 100644 --- a/core/lib/Drupal/Core/Image/ImageInterface.php +++ b/core/lib/Drupal/Core/Image/ImageInterface.php @@ -13,18 +13,22 @@ interface ImageInterface { /** - * Determines if a file contains a valid image. + * Gets an image from a file. * * Drupal supports GIF, JPG and PNG file formats when used with the GD * toolkit, and may support others, depending on which toolkits are * installed. * - * @param @todo + * @param string $source + * The path to an image file. + * + * @throws \BadMethodCallException + * When called twice. * * @return bool * TRUE if the file could be found and is an image, FALSE otherwise. */ - public function parseFile($source); + public function createFromSource($source); /** * Checks if the image is valid. @@ -94,7 +98,7 @@ public function getSource(); public function apply($operation, array $arguments = array()); /** - * Closes the image and saves the changes to a file. + * Saves the image to a file. * * @param string|null $destination * (optional) Destination path where the image should be saved. If it is empty @@ -102,8 +106,6 @@ public function apply($operation, array $arguments = array()); * * @return bool * TRUE on success, FALSE on failure. - * - * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface::save() */ public function save($destination = NULL); diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php index b2e944e..5de368c 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitBase.php @@ -104,16 +104,35 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form /** * {@inheritdoc} */ - public final function parseFile($source) { + public final function createFromSource($source) { + if ($this->getSource()) { + throw new \BadMethodCallException(__METHOD__ . '() may only be called once'); + } $this->source = $source; - if ($this->doParseFile()) { - $this->fileSize = filesize($this->source); + if ($this->doCreateFromSource()) { + $this->fileSize = filesize($this->getSource()); return TRUE; } return FALSE; } /** + * Performs toolkit specific operations to get an image from a file. + * + * Image toolkits must implement this method to ensure that the image + * can be manipulated through image toolkit operations once the source + * file is processed, and that + * \Drupal\Core\ImageToolkit\ImageToolkitInterface::isValid() will return + * TRUE if the image creation is successful. + * + * @return bool + * TRUE if the file could be found and is an image, FALSE otherwise. + * + * @see \Drupal\Core\ImageToolkit\ImageToolkitInterface::isValid() + */ + abstract protected function doCreateFromSource(); + + /** * {@inheritdoc} */ public final function save($destination = NULL) { @@ -138,6 +157,20 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form } /** + * Performs toolkit specific operations to save an image to a file. + * + * Image toolkits must implement this method to ensure that the image + * is saved to a destination file. + * + * @param string $destination + * Destination path where the image should be saved. + * + * @return bool + * TRUE on success, FALSE on failure. + */ + abstract protected function doSave($destination); + + /** * Provides a wrapper for drupal_chmod() to allow unit testing. * * @param string $uri diff --git a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php index 509c18b..38ec0c6 100644 --- a/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php +++ b/core/lib/Drupal/Core/ImageToolkit/ImageToolkitInterface.php @@ -9,7 +9,6 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\PluginFormInterface; -use Drupal\Core\Image\ImageInterface; use Drupal\Component\Plugin\PluginInspectionInterface; /** diff --git a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php index a2190d7..7780fad 100644 --- a/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php +++ b/core/modules/system/src/Plugin/ImageToolkit/GDToolkit.php @@ -45,7 +45,7 @@ class GDToolkit extends ImageToolkitBase { * * @var array|null * - * @see \Drupal\system\Plugin\ImageToolkit\GDToolkit::parseFile() + * @see \Drupal\system\Plugin\ImageToolkit\GDToolkit::doCreateFromSource() * @see \Drupal\system\Plugin\ImageToolkit\GDToolkit::setResource() * @see http://php.net/manual/en/function.getimagesize.php */ @@ -199,7 +199,7 @@ protected function doSave($destination) { /** * {@inheritdoc} */ - protected function doParseFile() { + protected function doCreateFromSource() { $data = @getimagesize($this->getSource()); if ($data && in_array($data[2], static::supportedTypes())) { $this->setType($data[2]); diff --git a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php index afbf105..8cd80b5 100644 --- a/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php +++ b/core/modules/system/tests/modules/image_test/src/Plugin/ImageToolkit/TestToolkit.php @@ -136,7 +136,7 @@ public function isValid() { /** * {@inheritdoc} */ - protected function doParseFile() { + protected function doCreateFromSource() { $this->logCall('parseFile', func_get_args()); $data = @getimagesize($this->getSource()); if ($data && in_array($data[2], static::supportedTypes())) { diff --git a/core/modules/system/tests/src/Unit/ImageToolkit/GDToolkitTest.php b/core/modules/system/tests/src/Unit/ImageToolkit/GDToolkitTest.php index 931ba33..a6cf787 100644 --- a/core/modules/system/tests/src/Unit/ImageToolkit/GDToolkitTest.php +++ b/core/modules/system/tests/src/Unit/ImageToolkit/GDToolkitTest.php @@ -107,7 +107,7 @@ protected function getTestImage($load_expected = TRUE, array $stubs = array()) { ->method('load'); } - $this->image->parseFile($this->source); + $this->image->createFromSource($this->source); } /** @@ -128,7 +128,7 @@ protected function getTestImageForOperation($class_name) { ->method('getToolkitOperation') ->will($this->returnValue($this->toolkitOperation)); - $this->image->parseFile($this->source); + $this->image->createFromSource($this->source); } /** @@ -232,11 +232,11 @@ public function testChmodFails() { } /** - * @covers ::parseFile + * @covers ::createFromSource */ - public function testParseFileFails() { + public function testCreateFromSourceFails() { $image = $this->getToolkitMock(); - $image->parseFile('magic-foobars.png'); + $image->createFromSource('magic-foobars.png'); $this->assertFalse($image->isValid()); $this->assertFalse($image->save());