1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the IrishDan\ResponsiveImageBundle package. |
4
|
|
|
* |
5
|
|
|
* (c) Daniel Byrne <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE file that was distributed with this source |
8
|
|
|
* code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace IrishDan\ResponsiveImageBundle; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\HttpKernel\Config\FileLocator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ImageEntityClassLocator |
17
|
|
|
* |
18
|
|
|
* The aim is to find entity class which implements ResponsiveImageInterface |
19
|
|
|
* |
20
|
|
|
* @package IrishDan\ResponsiveImageBundle |
21
|
|
|
*/ |
22
|
|
|
class ImageEntityClassLocator |
23
|
|
|
{ |
24
|
|
|
const RESPONSIVE_IMAGE_INTERFACE = 'IrishDan\ResponsiveImageBundle\ResponsiveImageInterface'; |
25
|
|
|
/** |
26
|
|
|
* @var null|string |
27
|
|
|
*/ |
28
|
|
|
protected $className = null; |
29
|
|
|
/** |
30
|
|
|
* @var FileLocator |
31
|
|
|
*/ |
32
|
|
|
protected $fileLocator; |
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $entityDirectory = 'Entity'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* ImageEntityClassLocator constructor. |
40
|
|
|
* |
41
|
|
|
* @param $bundles |
42
|
|
|
* @param FileLocator $fileLocator |
43
|
|
|
*/ |
44
|
|
|
public function __construct($bundles = [], FileLocator $fileLocator) |
45
|
|
|
{ |
46
|
|
|
$this->bundles = $bundles; |
|
|
|
|
47
|
|
|
$this->fileLocator = $fileLocator; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return mixed |
52
|
|
|
*/ |
53
|
|
|
public function getClassName() |
54
|
|
|
{ |
55
|
|
|
// Scan Bundle directories for entity directory |
56
|
|
|
if (empty($this->className)) { |
57
|
|
|
foreach ($this->bundles as $key => $namespace) { |
58
|
|
|
|
59
|
|
|
$path = $this->fileLocator->locate('@' . $key); |
60
|
|
|
$path .= $this->entityDirectory; |
61
|
|
|
|
62
|
|
|
if (file_exists($path)) { |
63
|
|
|
// Remove the final part of the namespace |
64
|
|
|
$namespaceParts = explode('\\', $namespace); |
65
|
|
|
array_pop($namespaceParts); |
66
|
|
|
$namespaceParts[] = $this->entityDirectory; |
67
|
|
|
$namespace = implode('\\', $namespaceParts); |
68
|
|
|
|
69
|
|
|
// switch any /'s into \'s for namespaces |
70
|
|
|
$namespace = str_replace('/', '\\', $namespace); |
71
|
|
|
|
72
|
|
|
$allFiles = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)); |
73
|
|
|
$phpFiles = new \RegexIterator($allFiles, '/\.php$/'); |
74
|
|
|
foreach ($phpFiles as $phpFile) { |
75
|
|
|
$fileName = $phpFile->getFileName(); |
76
|
|
|
$className = substr($fileName, 0, -4); |
77
|
|
|
$FQCN = $namespace . '\\' . $className; |
78
|
|
|
|
79
|
|
|
// Try to load the file as a class and determine if it implements the interface |
80
|
|
|
try { |
81
|
|
|
$reflect = new \ReflectionClass($FQCN); |
82
|
|
|
if ($reflect->implementsInterface(self::RESPONSIVE_IMAGE_INTERFACE)) { |
83
|
|
|
$this->className = $FQCN; |
84
|
|
|
break; |
85
|
|
|
} |
86
|
|
|
} catch (\ReflectionException $e) { |
87
|
|
|
// No action required |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->className; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $entityDirectory |
99
|
|
|
*/ |
100
|
|
|
public function setEntityDirectory($entityDirectory) |
101
|
|
|
{ |
102
|
|
|
$this->entityDirectory = $entityDirectory; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: