Repository navigation
Expand file tree
/
Copy pathMigrationFileParser.php
More file actions
69 lines (56 loc) · 1.77 KB
/
Copy pathMigrationFileParser.php
File metadata and controls
69 lines (56 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php namespace RainLab\Builder\Classes;
/**
* Parses migrations source files.
*
* @package rainlab\builder
* @author Alexey Bobkov, Samuel Georges
*/
class MigrationFileParser
{
/**
* Returns the migration namespace and class name.
* @param string $fileContents Specifies the file contents.
* @return array|null Returns an array with keys 'class', 'namespace'.
* Returns null if the parsing fails.
*/
public function extractMigrationInfoFromSource($fileContents)
{
$stream = new PhpSourceStream($fileContents);
$result = [];
while ($stream->forward()) {
$tokenCode = $stream->getCurrentCode();
if ($tokenCode == T_NAMESPACE) {
$namespace = $this->extractNamespace($stream);
if ($namespace === null) {
return null;
}
$result['namespace'] = $namespace;
}
if ($tokenCode == T_CLASS) {
$className = $this->extractClassName($stream);
if ($className === null) {
return null;
}
$result['class'] = $className;
}
}
if (!$result) {
return null;
}
return $result;
}
protected function extractClassName($stream)
{
if ($stream->getNextExpected(T_WHITESPACE) === null) {
return null;
}
return $stream->getNextExpectedTerminated([T_STRING], [T_WHITESPACE, ';']);
}
protected function extractNamespace($stream)
{
if ($stream->getNextExpected(T_WHITESPACE) === null) {
return null;
}
return $stream->getNextExpectedTerminated([T_STRING, T_NS_SEPARATOR], [T_WHITESPACE, ';']);
}
}