Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache image config #2125

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
cache image config
  • Loading branch information
henzigo committed Nov 16, 2020
commit 9a1c8164d75e2279cc60b3d98d7705060c34bd9c
65 changes: 51 additions & 14 deletions packages/framework/src/Component/Image/Config/ImageConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Parser;
use Symfony\Contracts\Cache\CacheInterface;

class ImageConfigLoader
{
Expand All @@ -39,14 +40,21 @@ class ImageConfigLoader
*/
protected $entityNameResolver;

/**
* @var \Symfony\Contracts\Cache\CacheInterface
*/
protected $appCache;

/**
* @param \Symfony\Component\Filesystem\Filesystem $filesystem
* @param \Shopsys\FrameworkBundle\Component\EntityExtension\EntityNameResolver $entityNameResolver
* @param \Shopsys\FrameworkBundle\Component\EntityExtension\EntityNameResolver|null $entityNameResolver
* @param \Symfony\Contracts\Cache\CacheInterface|null $appCache
*/
public function __construct(Filesystem $filesystem, ?EntityNameResolver $entityNameResolver = null)
public function __construct(Filesystem $filesystem, ?EntityNameResolver $entityNameResolver = null, ?CacheInterface $appCache = null)
{
$this->filesystem = $filesystem;
$this->entityNameResolver = $entityNameResolver;
$this->appCache = $appCache;
}

/**
Expand Down Expand Up @@ -76,29 +84,58 @@ public function setEntityNameResolver(EntityNameResolver $entityNameResolver): v
$this->entityNameResolver = $entityNameResolver;
}

/**
* @required
* @internal This function will be replaced by constructor injection in next major
* @param \Symfony\Contracts\Cache\CacheInterface $appCache
*/
public function setCacheInterface(CacheInterface $appCache): void
{
if ($this->appCache !== null && $this->appCache !== $appCache) {
throw new BadMethodCallException(sprintf(
'Method "%s" has been already called and cannot be called multiple times.',
__METHOD__
));
}
if ($this->appCache !== null) {
return;
}

@trigger_error(
sprintf(
'The %s() method is deprecated and will be removed in the next major. Use the constructor injection instead.',
__METHOD__
),
E_USER_DEPRECATED
);
$this->appCache = $appCache;
}

/**
* @param string $filename
* @return \Shopsys\FrameworkBundle\Component\Image\Config\ImageConfig
*/
public function loadFromYaml($filename)
{
$yamlParser = new Parser();
$cachedImageConfig = $this->appCache->get('imageConfig', function () use ($filename) {
$yamlParser = new Parser();

if (!$this->filesystem->exists($filename)) {
throw new FileNotFoundException(
'File ' . $filename . ' does not exist'
);
}
if (!$this->filesystem->exists($filename)) {
throw new FileNotFoundException(
'File ' . $filename . ' does not exist'
);
}

$imageConfigDefinition = new ImageConfigDefinition();
$processor = new Processor();
$imageConfigDefinition = new ImageConfigDefinition();
$processor = new Processor();

$inputConfig = $yamlParser->parse(file_get_contents($filename));
$outputConfig = $processor->processConfiguration($imageConfigDefinition, [$inputConfig]);
$inputConfig = $yamlParser->parse(file_get_contents($filename));
$outputConfig = $processor->processConfiguration($imageConfigDefinition, [$inputConfig]);

$preparedConfig = $this->loadFromArray($outputConfig);
return $this->loadFromArray($outputConfig);
});

return new ImageConfig($preparedConfig, $this->entityNameResolver);
return new ImageConfig($cachedImageConfig, $this->entityNameResolver);
}

/**
Expand Down