|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of slick/web_stack package |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Slick\WebStack\Renderer; |
|
11
|
|
|
|
|
12
|
|
|
use Aura\Router\Route; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* View Inflector |
|
16
|
|
|
* |
|
17
|
|
|
* @package Slick\WebStack\Renderer |
|
18
|
|
|
*/ |
|
19
|
|
|
class ViewInflector implements ViewInflectorInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $extension; |
|
25
|
|
|
|
|
26
|
|
|
private static $hasPcreUnicodeSupport; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Creates a view inflector |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $extension |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($extension) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->extension = $extension; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns the template name for current request |
|
40
|
|
|
* |
|
41
|
|
|
* @param Route $route |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function inflect(Route $route) |
|
46
|
|
|
{ |
|
47
|
|
|
$file = $route->attributes['controller'].'/'; |
|
48
|
|
|
$file .= $route->attributes['action']; |
|
49
|
|
|
$filtered = self::camelCaseToSeparator($file, '_'); |
|
50
|
|
|
$filtered = str_replace('_', '-', strtolower($filtered)); |
|
51
|
|
|
return "{$filtered}.{$this->extension}"; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Converts camel case strings to words separated by provided string |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $text The text to evaluate |
|
58
|
|
|
* @param string $sep The separator (or glue) for the words |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function camelCaseToSeparator($text, $sep = " ") |
|
63
|
|
|
{ |
|
64
|
|
|
if (!is_scalar($text) && !is_array($text)) { |
|
65
|
|
|
return $text; |
|
66
|
|
|
} |
|
67
|
|
|
if (self::hasPcreUnicodeSupport()) { |
|
68
|
|
|
$pattern = array( |
|
69
|
|
|
'#(?<=(?:\p{Lu}))(\p{Lu}\p{Ll})#', |
|
70
|
|
|
'#(?<=(?:\p{Ll}|\p{Nd}))(\p{Lu})#' |
|
71
|
|
|
); |
|
72
|
|
|
$replacement = array($sep.'\1', $sep.'\1'); |
|
73
|
|
|
} else { |
|
74
|
|
|
$pattern = array( |
|
75
|
|
|
'#(?<=(?:[A-Z]))([A-Z]+)([A-Z][a-z])#', |
|
76
|
|
|
'#(?<=(?:[a-z0-9]))([A-Z])#'); |
|
77
|
|
|
$replacement = array('\1'.$sep.'\2', $sep.'\1'); |
|
78
|
|
|
} |
|
79
|
|
|
return preg_replace($pattern, $replacement, $text); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Is PCRE compiled with Unicode support? |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function hasPcreUnicodeSupport() |
|
88
|
|
|
{ |
|
89
|
|
|
if (self::$hasPcreUnicodeSupport === null) { |
|
90
|
|
|
self::$hasPcreUnicodeSupport = |
|
91
|
|
|
defined('PREG_BAD_UTF8_OFFSET_ERROR') && |
|
92
|
|
|
@preg_match('/\pL/u', 'a') == 1; |
|
93
|
|
|
} |
|
94
|
|
|
return self::$hasPcreUnicodeSupport; |
|
95
|
|
|
} |
|
96
|
|
|
} |