/
FiveSeven.php
74 lines (67 loc) · 2.5 KB
/
FiveSeven.php
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
70
71
72
73
74
<?php
namespace Concrete\Core\Package\Offline\Parser;
use Concrete\Core\Package\Offline\Exception;
use Concrete\Core\Package\Offline\Parser;
/**
* The token parser for concrete5 5.7+ packages.
*/
class FiveSeven extends Parser
{
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Offline\Parser::canParseTokens()
*/
public function canParseTokens(array $tokens)
{
// 5.7+ packages use PHP namespaces
$namespaceIndex = $this->findTypedToken($tokens, [T_NAMESPACE]);
if ($namespaceIndex === null) {
return false;
}
// Multiple namespaces are not supported
return $this->findTypedToken($tokens, [T_NAMESPACE], $namespaceIndex + 1) === null;
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Offline\Parser::getControllerClassNameRegularExpression()
*/
protected function getControllerClassNameRegularExpression()
{
return '/^Controller$/';
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Offline\Parser::getPackageHandleFromClassName()
*/
protected function getPackageHandleFromClassName(array $tokens, $className, $classStart)
{
$namespaceIndex = $this->findTypedToken($tokens, [T_NAMESPACE], 0, $classStart - 1);
$namespaceNameIndex = $this->findTypedToken($tokens, [T_STRING, T_NS_SEPARATOR], $namespaceIndex + 1, $classStart - 1);
if ($namespaceNameIndex === null) {
throw Exception::create(Exception::ERRORCODE_MISSING_NAMESPACENAME, t('Unable to find the namespace name'));
}
$namespaceName = '';
while (is_array($tokens[$namespaceNameIndex]) && in_array($tokens[$namespaceNameIndex][0], [T_STRING, T_NS_SEPARATOR])) {
$namespaceName .= $tokens[$namespaceNameIndex][1];
++$namespaceNameIndex;
}
$namespaceName = trim($namespaceName, '\\');
$matches = null;
if (!preg_match('/^Concrete\\\\Package\\\\(\w+)$/', $namespaceName, $matches)) {
throw Exception::create(Exception::ERRORCODE_INVALID_NAMESPACENAME, t('The namespace "%s" is not valid', $namespaceName));
}
return uncamelcase($matches[1]);
}
/**
* {@inheritdoc}
*
* @see \Concrete\Core\Package\Offline\Parser::getDefaultPackageMinimumCodeVersion()
*/
protected function getDefaultPackageMinimumCodeVersion()
{
return '5.7.0'; // https://github.com/concrete5/concrete5/blob/8.5.1/concrete/src/Package/Package.php#L183
}
}