|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <https://github.com/baleen/migrations>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Baleen\Migrations\Migration\Repository\Mapper; |
|
21
|
|
|
|
|
22
|
|
|
use Baleen\Migrations\Exception\InvalidArgumentException; |
|
23
|
|
|
use Baleen\Migrations\Migration\Factory\FactoryInterface; |
|
24
|
|
|
use Baleen\Migrations\Migration\Factory\SimpleFactory; |
|
25
|
|
|
use Baleen\Migrations\Migration\MigrationInterface; |
|
26
|
|
|
use Zend\Code\Scanner\DerivedClassScanner; |
|
27
|
|
|
use Zend\Code\Scanner\DirectoryScanner; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class DirectoryRepositoryMapper |
|
31
|
|
|
* |
|
32
|
|
|
* @author Gabriel Somoza <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
final class DirectoryMapper implements MigrationMapperInterface |
|
35
|
|
|
{ |
|
36
|
|
|
const PATTERN_DEFAULT = '/v([0-9]+).*/'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var DirectoryScanner |
|
40
|
|
|
*/ |
|
41
|
|
|
private $scanner; |
|
42
|
|
|
|
|
43
|
|
|
/** @var string */ |
|
44
|
|
|
private $pattern = self::PATTERN_DEFAULT; |
|
45
|
|
|
|
|
46
|
|
|
/** @var FactoryInterface */ |
|
47
|
|
|
private $factory; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $path Full path to the repository's directory |
|
51
|
|
|
* @param FactoryInterface $factory A factory that can instantiate migrations. |
|
|
|
|
|
|
52
|
|
|
* @param string $pattern Regex pattern to extract the version ID from a migration's class name. If null it will |
|
|
|
|
|
|
53
|
|
|
* default to DirectoryRepositoryMapper::PATTERN_DEFAULT |
|
54
|
|
|
* @throws InvalidArgumentException |
|
55
|
|
|
*/ |
|
56
|
16 |
|
public function __construct( |
|
57
|
|
|
$path, |
|
58
|
|
|
FactoryInterface $factory = null, |
|
59
|
|
|
$pattern = null |
|
60
|
|
|
) { |
|
61
|
16 |
|
$path = (string) $path; |
|
62
|
16 |
|
if (empty($path) || !is_dir($path)) { |
|
63
|
6 |
|
throw new InvalidArgumentException('Argument "path" is empty or directory does not exist.'); |
|
64
|
|
|
} |
|
65
|
10 |
|
$this->scanner = new DirectoryScanner($path); |
|
66
|
|
|
|
|
67
|
10 |
|
$pattern = null === $pattern ? self::PATTERN_DEFAULT : (string) $pattern; |
|
68
|
10 |
|
if (empty($pattern)) { |
|
69
|
1 |
|
throw new InvalidArgumentException('Argument "pattern" cannot be empty.'); |
|
70
|
|
|
} |
|
71
|
9 |
|
$this->pattern = $pattern; |
|
72
|
|
|
|
|
73
|
9 |
|
if (null === $factory) { |
|
74
|
6 |
|
$factory = new SimpleFactory(); |
|
75
|
|
|
} |
|
76
|
9 |
|
$this->factory = $factory; |
|
77
|
9 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritdoc |
|
81
|
|
|
*/ |
|
82
|
6 |
|
public function fetchAll() |
|
83
|
|
|
{ |
|
84
|
6 |
|
$classes = $this->scanner->getClasses(true); |
|
85
|
6 |
|
$definitions = []; |
|
86
|
6 |
|
foreach ($classes as $class) { |
|
87
|
|
|
/* @var DerivedClassScanner $class */ |
|
88
|
6 |
|
$className = $class->getName(); |
|
89
|
6 |
|
$matches = []; |
|
90
|
6 |
|
if (preg_match($this->pattern, $className, $matches) |
|
91
|
6 |
|
&& isset($matches[1]) |
|
92
|
6 |
|
&& $class->isInstantiable() |
|
93
|
|
|
) { |
|
94
|
6 |
|
$migration = $this->factory->create($className); |
|
95
|
6 |
|
if ($migration instanceof MigrationInterface) { |
|
96
|
6 |
|
$definitions[] = new Definition($migration); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
6 |
|
return $definitions; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.