forked from liip/LiipImagineBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractDoctrineLoader.php
More file actions
64 lines (51 loc) · 1.79 KB
/
AbstractDoctrineLoader.php
File metadata and controls
64 lines (51 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Liip\ImagineBundle\Binary\Loader;
use Doctrine\Persistence\ObjectManager;
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
abstract class AbstractDoctrineLoader implements LoaderInterface
{
protected ObjectManager $manager;
protected string $modelClass;
public function __construct(ObjectManager $manager, string $modelClass)
{
$this->manager = $manager;
$this->modelClass = $modelClass;
}
/**
* {@inheritdoc}
*/
public function find($path)
{
$image = $this->manager->find($this->modelClass, $this->mapPathToId($path));
if (!$image) {
// try to find the image without extension
$info = pathinfo($path);
$name = $info['dirname'].'/'.$info['filename'];
$image = $this->manager->find($this->modelClass, $this->mapPathToId($name));
}
if (!$image) {
throw new NotLoadableException(sprintf('Source image was not found with id "%s"', $path));
}
return stream_get_contents($this->getStreamFromImage($image));
}
/**
* Map the requested path (ie. subpath in the URL) to an id that can be used to lookup the image in the Doctrine store.
*
* @return string|int
*/
abstract protected function mapPathToId(string $path);
/**
* Return a stream resource from the Doctrine entity/document with the image content.
*
* @return resource
*/
abstract protected function getStreamFromImage(object $image);
}