1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Imagine\Data\Loader; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
15
|
|
|
use Liip\ImagineBundle\Exception\File\Loader\NotLoadableException; |
16
|
|
|
use Liip\ImagineBundle\File\FileBlob; |
17
|
|
|
use Liip\ImagineBundle\File\FileInterface; |
18
|
|
|
|
19
|
|
|
abstract class AbstractDoctrineLoader implements LoaderInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var ObjectManager |
23
|
|
|
*/ |
24
|
|
|
protected $manager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $class; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ObjectManager $manager |
33
|
|
|
* @param string $class |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ObjectManager $manager, string $class = null) |
36
|
|
|
{ |
37
|
|
|
$this->manager = $manager; |
38
|
|
|
$this->class = $class; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function find(string $identity): FileInterface |
45
|
|
|
{ |
46
|
|
|
$image = $this->manager->find($this->class, $this->mapPathToId($identity)); |
47
|
|
|
|
48
|
|
|
if (!$image) { |
49
|
|
|
// try to find the image without extension |
50
|
|
|
$info = pathinfo($identity); |
51
|
|
|
$name = $info['dirname'].'/'.$info['filename']; |
52
|
|
|
|
53
|
|
|
$image = $this->manager->find($this->class, $this->mapPathToId($name)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!$image) { |
57
|
|
|
throw new NotLoadableException('Source image was not found with id "%s"', $identity); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return FileBlob::create(stream_get_contents($this->getStreamFromImage($image))); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Map the requested path (ie. subpath in the URL) to an id that can be used to lookup the image in the Doctrine store. |
65
|
|
|
* |
66
|
|
|
* @param string $path |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
abstract protected function mapPathToId($path); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return a stream resource from the Doctrine entity/document with the image content. |
74
|
|
|
* |
75
|
|
|
* @param object $image |
76
|
|
|
* |
77
|
|
|
* @return resource |
78
|
|
|
*/ |
79
|
|
|
abstract protected function getStreamFromImage($image); |
80
|
|
|
} |
81
|
|
|
|