|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Loader; |
|
4
|
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\LoaderInterface; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition; |
|
7
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\MigrationDefinitionCollection; |
|
8
|
|
|
use Kaliop\eZMigrationBundle\API\ConfigResolverInterface; |
|
9
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Loads migration definitions from disk (by default from a specific dir in enabled bundles) |
|
13
|
|
|
*/ |
|
14
|
|
|
class Filesystem implements LoaderInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Name of the directory where migration versions are located |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $versionDirectory; |
|
21
|
|
|
protected $kernel; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Filesystem constructor. |
|
25
|
|
|
* @param KernelInterface $kernel |
|
26
|
|
|
* @param string $versionDirectoryParameter name of folder when $configResolver is null; name of parameter when it is not |
|
27
|
|
|
* @param ConfigResolverInterface $configResolver |
|
28
|
|
|
* @throws \Exception |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(KernelInterface $kernel, $versionDirectoryParameter = 'Migrations', ConfigResolverInterface $configResolver = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->kernel = $kernel; |
|
33
|
|
|
$this->versionDirectory = $configResolver ? $configResolver->getParameter($versionDirectoryParameter) : $versionDirectoryParameter; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $paths either dir names or file names. If empty, will look in all registered bundles subdir |
|
38
|
|
|
* @return MigrationDefinition[] migrations definitions. key: name, value: file path |
|
39
|
|
|
* @throws \Exception |
|
40
|
|
|
*/ |
|
41
|
|
|
public function listAvailableDefinitions(array $paths = array()) |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->getDefinitions($paths, true); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array $paths either dir names or file names. If empty, will look in all registered bundles subdir |
|
48
|
|
|
* @return MigrationDefinitionCollection migrations definitions. key: name, value: contents of the definition, as string |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
*/ |
|
51
|
|
|
public function loadDefinitions(array $paths = array()) |
|
52
|
|
|
{ |
|
53
|
|
|
return new MigrationDefinitionCollection($this->getDefinitions($paths, false)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param array $paths either dir names or file names |
|
58
|
|
|
* @param bool $returnFilename return either the |
|
59
|
|
|
* @return MigrationDefinition[]|string[] migrations definitions. key: name, value: contents of the definition, as string or file path |
|
60
|
|
|
* @throws \Exception |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getDefinitions(array $paths = array(), $returnFilename = false) |
|
63
|
|
|
{ |
|
64
|
|
|
// if no paths defined, we look in all bundles |
|
65
|
|
View Code Duplication |
if (empty($paths)) { |
|
|
|
|
|
|
66
|
|
|
$paths = array(); |
|
67
|
|
|
/** @var $bundle \Symfony\Component\HttpKernel\Bundle\BundleInterface */ |
|
68
|
|
|
foreach ($this->kernel->getBundles() as $bundle) |
|
69
|
|
|
{ |
|
70
|
|
|
$path = $bundle->getPath() . "/" . $this->versionDirectory; |
|
71
|
|
|
if (is_dir($path)) { |
|
72
|
|
|
$paths[] = $path; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$definitions = array(); |
|
78
|
|
|
foreach ($paths as $path) { |
|
79
|
|
|
if (is_file($path)) { |
|
80
|
|
|
$definitions[basename($path)] = $returnFilename ? $path : new MigrationDefinition( |
|
81
|
|
|
basename($path), |
|
82
|
|
|
$path, |
|
83
|
|
|
file_get_contents($path) |
|
84
|
|
|
); |
|
85
|
|
|
} elseif (is_dir($path)) { |
|
86
|
|
|
foreach (new \DirectoryIterator($path) as $file) { |
|
87
|
|
|
if ($file->isFile()) { |
|
88
|
|
|
$definitions[$file->getFilename()] = |
|
89
|
|
|
$returnFilename ? $file->getRealPath() : new MigrationDefinition( |
|
90
|
|
|
$file->getFilename(), |
|
91
|
|
|
$file->getRealPath(), |
|
92
|
|
|
file_get_contents($file->getRealPath()) |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} else { |
|
97
|
|
|
throw new \Exception("Path '$path' is neither a file nor directory"); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
ksort($definitions); |
|
101
|
|
|
|
|
102
|
|
|
return $definitions; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.