|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phinx. |
|
4
|
|
|
* |
|
5
|
|
|
* (The MIT license) |
|
6
|
|
|
* Copyright (c) 2015 Rob Morgan |
|
7
|
|
|
* |
|
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
9
|
|
|
* of this software and associated * documentation files (the "Software"), to |
|
10
|
|
|
* deal in the Software without restriction, including without limitation the |
|
11
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
|
12
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is |
|
13
|
|
|
* furnished to do so, subject to the following conditions: |
|
14
|
|
|
* |
|
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
16
|
|
|
* all copies or substantial portions of the Software. |
|
17
|
|
|
* |
|
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
23
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
|
24
|
|
|
* IN THE SOFTWARE. |
|
25
|
|
|
* |
|
26
|
|
|
* @package Phinx |
|
27
|
|
|
* @subpackage Phinx\Migration\Locator |
|
28
|
|
|
*/ |
|
29
|
|
|
namespace Phinx\Migration\Locator; |
|
30
|
|
|
|
|
31
|
|
|
use Phinx\Migration\MigrationDefinition; |
|
32
|
|
|
use Phinx\Util\Util; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @author Cas Leentfaar |
|
36
|
|
|
*/ |
|
37
|
|
|
class PsrLocator implements LocatorInterface |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function generate($targetDir, $name = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$version = Util::getCurrentTimestamp(); |
|
45
|
|
|
$className = sprintf('v%d%s', $version, $name ? '_' . $name : ''); |
|
46
|
|
|
$filePath = sprintf('%s/%s.php', $targetDir, $className); |
|
47
|
|
|
$definition = new MigrationDefinition($version, $className, $filePath, $name); |
|
48
|
|
|
|
|
49
|
|
|
return $definition; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
|
|
public function locate($filePath) |
|
56
|
|
|
{ |
|
57
|
|
|
// convert the filename to a class name |
|
58
|
|
|
$class = basename($filePath, '.php'); |
|
59
|
|
|
|
|
60
|
|
|
if (false !== $underscorePos = strpos($class, '_')) { |
|
61
|
|
|
$name = substr($class, strpos($class, "_") + 1); |
|
62
|
|
|
} else { |
|
63
|
|
|
$name = null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// extract version from class name |
|
67
|
|
|
$version = preg_replace('/[^\d]/', '', $class); |
|
68
|
|
|
|
|
69
|
|
|
// try to find namespace in class |
|
70
|
|
|
$content = file_get_contents($filePath); |
|
71
|
|
|
preg_match('#^namespace\s+(.+?);$#sm', $content, $matches); |
|
72
|
|
|
|
|
73
|
|
|
if (!isset($matches[1])) { |
|
74
|
|
|
// no namespace detected, the FQCN is expected to be the same as the filename without extension |
|
75
|
|
|
$fqcn = $class; |
|
76
|
|
|
} else { |
|
77
|
|
|
// namespace detected, the FQCN is expected to be the same as the namespace + filename |
|
78
|
|
|
$fqcn = sprintf('%s\%s', $matches[1], $class); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return new MigrationDefinition($version, $fqcn, $filePath, $name); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|