1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saxulum\RouteController\Manager; |
4
|
|
|
|
5
|
|
|
use PhpParser\PrettyPrinter\Standard; |
6
|
|
|
use Saxulum\AnnotationManager\Manager\AnnotationManager; |
7
|
|
|
use Silex\Application; |
8
|
|
|
|
9
|
|
|
class RouteControllerManager |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $paths; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $cacheFile; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
array $paths = array(), |
23
|
|
|
$cacheDirectory, |
24
|
|
|
$cacheFileName |
25
|
|
|
) { |
26
|
|
|
if (!is_null($cacheDirectory)) { |
27
|
|
|
if (substr($cacheDirectory, -1) == '/') { |
28
|
|
|
$cacheDirectory = substr($cacheDirectory, 0, -1); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (!is_dir($cacheDirectory)) { |
32
|
|
|
mkdir($cacheDirectory, 0777, true); |
33
|
|
|
} |
34
|
|
|
} else { |
35
|
|
|
$cacheDirectory = sys_get_temp_dir(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->paths = $paths; |
39
|
|
|
$this->cacheFile = $cacheDirectory . '/' . $cacheFileName; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param boolean $debug |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function isCacheValid($debug) |
47
|
|
|
{ |
48
|
|
|
if (!$debug && file_exists($this->cacheFile)) { |
49
|
|
|
return true; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param AnnotationManager $annotationManager |
57
|
|
|
* @param ServiceManager $serviceManager |
58
|
|
|
* @param RouteManager $routeManager |
59
|
|
|
* @param Standard $prettyPrinter |
60
|
|
|
*/ |
61
|
|
|
public function updateCache( |
62
|
|
|
AnnotationManager $annotationManager, |
63
|
|
|
ServiceManager $serviceManager, |
64
|
|
|
RouteManager $routeManager, |
65
|
|
|
Standard $prettyPrinter |
66
|
|
|
) { |
67
|
|
|
$classInfos = $annotationManager |
68
|
|
|
->buildClassInfosBasedOnPaths($this->paths) |
69
|
|
|
; |
70
|
|
|
|
71
|
|
|
$code = "<?php\n\n"; |
72
|
|
|
|
73
|
|
|
foreach ($classInfos as $classInfo) { |
74
|
|
|
$code .= $prettyPrinter->prettyPrint($serviceManager->generateCode($classInfo)); |
75
|
|
|
$code .= "\n\n"; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
foreach ($classInfos as $classInfo) { |
79
|
|
|
$code .= $prettyPrinter->prettyPrint($routeManager->generateCode($classInfo)); |
80
|
|
|
$code .= "\n\n"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
file_put_contents($this->cacheFile, $code); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Application $app |
88
|
|
|
* @throws \LogicException |
89
|
|
|
*/ |
90
|
|
|
public function loadCache(Application $app) |
91
|
|
|
{ |
92
|
|
|
if (!file_exists($this->cacheFile)) { |
93
|
|
|
throw new \LogicException("Can't load cache, if its not exists"); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
require $this->cacheFile; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|