Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented image alias generator, using legacy kernel
- Loading branch information
1 parent
9297314
commit c7c0aa0
Showing
9 changed files
with
352 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
eZ/Publish/API/Repository/Exceptions/InvalidVariantException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
/** | ||
* File containing the InvalidVariantException class. | ||
* | ||
* @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. | ||
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 | ||
* @version //autogentag// | ||
*/ | ||
|
||
namespace eZ\Publish\API\Repository\Exceptions; | ||
|
||
class InvalidVariantException extends InvalidArgumentException | ||
{ | ||
public function __construct( $variantName, $variantType, $code = 0, Exception $previous = null ) | ||
{ | ||
parent::__construct( "Invalid variant '$variantName' for $variantType", $code, $previous ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/** | ||
* File containing the ImageVariant class. | ||
* | ||
* @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. | ||
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 | ||
* @version //autogentag// | ||
*/ | ||
|
||
namespace eZ\Publish\API\Repository\Values\File; | ||
|
||
class ImageVariant extends Variant | ||
{ | ||
/** | ||
* The width as number of pixels (for example "320"). | ||
* | ||
* @var int | ||
*/ | ||
protected $width; | ||
|
||
/** | ||
* The height as number of pixels (for example "256"). | ||
* | ||
* @var int | ||
*/ | ||
protected $height; | ||
|
||
/** | ||
* The name of the image alias (for example "original"). | ||
* | ||
* @var string | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* Contains extra information about the image, depending on the image type. | ||
* It will typically contain EXIF information from digital cameras or information about animated GIFs. | ||
* If there is no information, the info will be a boolean FALSE. | ||
* | ||
* @var mixed | ||
*/ | ||
protected $info; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* File containing the file Variant class. | ||
* | ||
* @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. | ||
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 | ||
* @version //autogentag// | ||
*/ | ||
|
||
namespace eZ\Publish\API\Repository\Values\File; | ||
|
||
use eZ\Publish\API\Repository\Values\ValueObject; | ||
|
||
/** | ||
* Base class for file variations (i.e. image aliases) | ||
*/ | ||
class Variant extends ValueObject | ||
{ | ||
/** | ||
* Number of bytes for current variant | ||
* | ||
* @var int | ||
*/ | ||
protected $fileSize; | ||
|
||
/** | ||
* The MIME type (for example "image/png"). | ||
* | ||
* @var string | ||
*/ | ||
protected $mimeType; | ||
|
||
/** | ||
* The name of the file (for example "my_image.png"). | ||
* | ||
* @var string | ||
*/ | ||
protected $fileName; | ||
|
||
/** | ||
* The path to the file (for example "var/storage/images/test/199-2-eng-GB"). | ||
* | ||
* @var string | ||
*/ | ||
protected $dirPath; | ||
|
||
/** | ||
* Complete path + name of image file (for example "var/storage/images/test/199-2-eng-GB/apple.png"). | ||
* | ||
* @var string | ||
*/ | ||
protected $uri; | ||
|
||
/** | ||
* When the variant was last modified. | ||
* | ||
* @var \DateTime | ||
*/ | ||
protected $lastModified; | ||
} |
119 changes: 119 additions & 0 deletions
119
eZ/Publish/Core/MVC/Legacy/FieldType/Image/AliasGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
/** | ||
* File containing the image AliasGenerator class. | ||
* | ||
* @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved. | ||
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2 | ||
* @version //autogentag// | ||
*/ | ||
|
||
namespace eZ\Publish\Core\MVC\Legacy\FieldType\Image; | ||
|
||
use eZ\Publish\Core\MVC\ConfigResolverInterface, | ||
eZ\Publish\SPI\VariantService, | ||
eZ\Publish\API\Repository\Values\Content\Field, | ||
eZ\Publish\API\Repository\Values\Content\VersionInfo, | ||
eZ\Publish\API\Repository\Values\File\ImageVariant, | ||
eZ\Publish\API\Repository\Exceptions\InvalidVariantException, | ||
eZContentObjectAttribute, | ||
eZImageAliasHandler; | ||
|
||
class AliasGenerator implements VariantService | ||
{ | ||
/** | ||
* @var \Closure | ||
*/ | ||
private $kernelClosure; | ||
|
||
/** | ||
* @var \eZ\Publish\Core\MVC\ConfigResolverInterface | ||
*/ | ||
private $configResolver; | ||
|
||
/** | ||
* @var \eZImageAliasHandler[] | ||
*/ | ||
private $aliasHandlers; | ||
|
||
/** | ||
* Image variant objects, indexed by <fieldId>-<versionNo>-<variantName>. | ||
* Storing them avoids to run the legacy kernel each time if there are similar images variations required. | ||
* | ||
* @var \eZ\Publish\API\Repository\Values\File\ImageVariant[] | ||
*/ | ||
private $variants; | ||
|
||
public function __construct( \Closure $legacyKernelClosure, ConfigResolverInterface $configResolver ) | ||
{ | ||
$this->kernelClosure = $legacyKernelClosure; | ||
$this->configResolver = $configResolver; | ||
} | ||
|
||
/** | ||
* @return \eZ\Publish\Core\MVC\Legacy\Kernel | ||
*/ | ||
protected function getLegacyKernel() | ||
{ | ||
$kernelClosure = $this->kernelClosure; | ||
return $kernelClosure(); | ||
} | ||
|
||
/** | ||
* Returns an image variant object. | ||
* Variant creation will be done through the legacy eZImageAliasHandler, using the legacy kernel. | ||
* | ||
* @param \eZ\Publish\API\Repository\Values\Content\Field $field | ||
* @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo | ||
* @param string $variantName | ||
* @return \eZ\Publish\API\Repository\Values\File\ImageVariant | ||
* | ||
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidVariantException | ||
*/ | ||
public function getVariant( Field $field, VersionInfo $versionInfo, $variantName ) | ||
{ | ||
$variantIdentifier = "$field->id-$versionInfo->versionNo-$variantName"; | ||
if ( isset( $this->variants[$variantIdentifier] ) ) | ||
return $this->variants[$variantIdentifier]; | ||
|
||
$configResolver = $this->configResolver; | ||
// Assigning by reference to be able to modify those arrays within the closure (due to PHP 5.3 limitation with access to $this) | ||
$allAliasHandlers = &$this->aliasHandlers; | ||
$allVariants = &$this->variants; | ||
|
||
return $this->getLegacyKernel()->runCallback( | ||
function () use ( $field, $versionInfo, $variantName, $configResolver, $allAliasHandlers, $allVariants, $variantIdentifier ) | ||
{ | ||
$aliasHandlerIdentifier = "$field->id-$versionInfo->versionNo"; | ||
if ( !isset( $allAliasHandlers[$aliasHandlerIdentifier] ) ) | ||
{ | ||
$allAliasHandlers[$aliasHandlerIdentifier] = new eZImageAliasHandler( | ||
eZContentObjectAttribute::fetch( $field->id, $versionInfo->versionNo ) | ||
); | ||
} | ||
|
||
/** @var $imageAliasHandler \eZImageAliasHandler */ | ||
$imageAliasHandler = $allAliasHandlers[$aliasHandlerIdentifier]; | ||
$aliasArray = $imageAliasHandler->imageAlias( $variantName ); | ||
if ( $aliasArray === null ) | ||
throw new InvalidVariantException( $variantName, 'image' ); | ||
|
||
$allVariants[$variantIdentifier] = new ImageVariant( | ||
array( | ||
'name' => $variantName, | ||
'fileName' => $aliasArray['filename'], | ||
'dirPath' => $aliasArray['dirpath'], | ||
'fileSize' => $aliasArray['filesize'], | ||
'mimeType' => $aliasArray['mime_type'], | ||
'lastModified' => new \DateTime( '@' . $aliasArray['timestamp'] ), | ||
'uri' => $aliasArray['url'], | ||
'width' => $aliasArray['width'], | ||
'height' => $aliasArray['height'], | ||
) | ||
); | ||
|
||
return $allVariants[$variantIdentifier]; | ||
}, | ||
false | ||
); | ||
} | ||
} |
Oops, something went wrong.