|
1
|
|
|
<?php |
|
2
|
|
|
namespace WebStream\Util; |
|
3
|
|
|
|
|
4
|
|
|
use WebStream\Exception\SystemException; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* ApplicationUtils |
|
8
|
|
|
* アプリケーション依存のUtility |
|
9
|
|
|
* @author Ryuichi Tanaka |
|
10
|
|
|
* @since 2015/12/26 |
|
11
|
|
|
* @version 0.7 |
|
12
|
|
|
*/ |
|
13
|
|
|
trait ApplicationUtils |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* プロジェクトディレクトリの絶対パスを返す |
|
17
|
|
|
* @return string プロジェクトディレクトリの絶対パス |
|
18
|
|
|
*/ |
|
19
|
|
|
public function getApplicationRoot() |
|
20
|
|
|
{ |
|
21
|
|
|
// 上位階層を辿り、.projectrootファイルを見つける |
|
22
|
|
|
$targetPath = realpath(dirname(__FILE__)); |
|
23
|
|
|
$isProjectRoot = false; |
|
24
|
|
|
|
|
25
|
|
|
while (!$isProjectRoot) { |
|
26
|
|
|
if (file_exists($targetPath . DIRECTORY_SEPARATOR . $this->getProjectFileName())) { |
|
27
|
|
|
$isProjectRoot = true; |
|
28
|
|
|
} else { |
|
29
|
|
|
if (preg_match("/(.*)\//", $targetPath, $matches)) { |
|
30
|
|
|
$targetPath = $matches[1]; |
|
31
|
|
|
if (!is_dir($targetPath)) { |
|
32
|
|
|
break; |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if (!$isProjectRoot) { |
|
|
|
|
|
|
39
|
|
|
throw new SystemException("'.projectroot' file must be put in directly under the project directory."); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $targetPath; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* 指定したファイルの名前空間を取得する |
|
47
|
|
|
* @param string ファイルパス |
|
|
|
|
|
|
48
|
|
|
* @param string 起点ディレクトリパス |
|
49
|
|
|
* @return string 名前空間 |
|
50
|
|
|
*/ |
|
51
|
|
|
public function getNamespace($filepath, $baseDir = null) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
if (file_exists($filepath)) { |
|
54
|
|
|
$resource = fopen($filepath, "r"); |
|
55
|
|
|
while (false !== ($line = fgets($resource))) { |
|
|
|
|
|
|
56
|
|
|
if (preg_match("/^namespace\s(.*);$/", $line, $matches)) { |
|
57
|
|
|
$namespace = $matches[1]; |
|
58
|
|
|
if (substr($namespace, 0) !== '\\') { |
|
59
|
|
|
$namespace = '\\' . $namespace; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $namespace; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
fclose($resource); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Viewで有効なModel変数名を返却する |
|
73
|
|
|
* @return string Model変数名 |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getModelVariableName() |
|
76
|
|
|
{ |
|
77
|
|
|
return "model"; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Viewで有効なHelper変数名を返却する |
|
82
|
|
|
* @return string Helper変数名 |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getHelperVariableName() |
|
85
|
|
|
{ |
|
86
|
|
|
return "helper"; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* プロジェクトルートファイル名を返却する |
|
91
|
|
|
* @return string プロジェクトルートファイル名 |
|
92
|
|
|
*/ |
|
93
|
|
|
private function getProjectFileName() |
|
94
|
|
|
{ |
|
95
|
|
|
return ".projectroot"; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* CoreHelper#asyncで使用するIDを返却する |
|
100
|
|
|
* @return string DOMID |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getAsyncDomId() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->getRandomstring(32); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* CoreHelper#asyncで使用するコードを返却する |
|
109
|
|
|
* @param string URL |
|
110
|
|
|
* @param string CSSクラス名 |
|
|
|
|
|
|
111
|
|
|
* @return string コード |
|
112
|
|
|
*/ |
|
113
|
|
|
public function asyncHelperCode($url, $id) |
|
114
|
|
|
{ |
|
115
|
|
|
return <<< JSCODE |
|
116
|
|
|
(function (c,b) {var a;a=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject("Microsoft.XMLHTTP");a.onreadystatechange=function () {4==a.readyState&&200==a.status&&(document.getElementById(b).outerHTML=a.responseText)};a.open("GET",c,!0);a.send()})("$url","$id"); |
|
117
|
|
|
JSCODE; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|