Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Command to remove all alias images - eZ Publish 2014.11
<?php
namespace Ayaline\Bundle\EzToolsBundle\Image;
use eZ\Publish\Core\FieldType\Image\AliasCleanerInterface;
use eZ\Publish\Core\IO\UrlRedecoratorInterface;
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
class AliasCleaner implements AliasCleanerInterface
{
/**
* @var AliasCleanerInterface
*/
private $innerAliasCleaner;
/**
* @var UrlRedecoratorInterface
*/
private $urlRedecorator;
/**
* @var FilterConfiguration
*/
private $filterConfiguration;
/**
* @var string
*/
private $kernelRootPath;
/**
* @param AliasCleanerInterface $innerAliasCleaner
* @param UrlRedecoratorInterface $urlRedecorator
* @param FilterConfiguration $filterConfiguration
* @param string $kernelRootPath
*/
public function __construct(
AliasCleanerInterface $innerAliasCleaner,
UrlRedecoratorInterface $urlRedecorator,
FilterConfiguration $filterConfiguration,
$kernelRootPath
)
{
$this->innerAliasCleaner = $innerAliasCleaner;
$this->urlRedecorator = $urlRedecorator;
$this->filterConfiguration = $filterConfiguration;
$this->kernelRootPath = $kernelRootPath . '/../web';
}
public function removeAliases( $originalPath )
{
$image = $this->kernelRootPath . $this->urlRedecorator->redecorateFromTarget( $originalPath );
$this->remove($image);
}
/**
* @param $imagePath
*/
public function remove( $imagePath )
{
$filters = array_keys( $this->filterConfiguration->all() );
foreach ( $filters as $filter )
{
$filteredImagePath = $this->getFilePath( $imagePath, $filter );
if (file_exists($filteredImagePath)) {
unlink($filteredImagePath);
}
}
}
/**
* Returns path for filtered image from original path.
* Pattern is <original_dir>/<filename>_<filter_name>.<extension>
*
* e.g. var/ezdemo_site/Tardis/bigger/in-the-inside/RiverSong_thumbnail.jpg
*
* @param string $path
* @param string $filter
*
* @return string
*/
public function getFilePath( $path, $filter )
{
$info = pathinfo( $path );
return sprintf(
'%s/%s_%s%s',
$info['dirname'],
$info['filename'],
$filter,
empty( $info['extension'] ) ? '' : '.' . $info['extension']
);
}
}
<?php
namespace Ayaline\Bundle\EzToolsBundle\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Ayaline\Bundle\EzToolsBundle\Image\AliasCleaner;
class ImageAliasCleanerCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('clear:image:alias')
->setDescription('Remove all images alias')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$handler = $this->getDatabaseHandler();
$connection = $handler->getConnection();
/** @var AliasCleaner $aliasCleaner */
$aliasCleaner = $this->getContainer()->get('ayaline.image_alias.cleaner');
$imagePaths = $connection->executeQuery("SELECT filepath FROM ezimagefile")->fetchAll();
foreach ($imagePaths as $path) {
$aliasCleaner->removeAliases($path["filepath"]);
}
}
/**
* Returns the database handler from the service container
*
* @return \eZ\Publish\Core\Persistence\Doctrine\ConnectionHandler
*/
protected function getDatabaseHandler()
{
return $this->getContainer()->get( 'ezpublish.api.storage_engine.legacy.dbhandler' );
}
}
parameters:
ayaline.image_alias.cleaner.class: Ayaline\Bundle\EzToolsBundle\Image\AliasCleaner
services:
ayaline.image_alias.cleaner:
class: %ayaline.image_alias.cleaner.class%
arguments:
- @ezpublish.image_alias.imagine.alias_cleaner
- @ezpublish.core.io.image_fieldtype.legacy_url_redecorator
- @liip_imagine.filter.configuration
- %kernel.root_dir%
lazy: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.