1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Opine\Build\Service |
4
|
|
|
* |
5
|
|
|
* Copyright (c)2013, 2014 Ryan Mahoney, https://github.com/Opine-Org <[email protected]> |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
15
|
|
|
* all copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23
|
|
|
* THE SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
namespace Opine\Build; |
26
|
|
|
|
27
|
|
|
use RecursiveIteratorIterator; |
28
|
|
|
use RecursiveDirectoryIterator; |
29
|
|
|
use FilesystemIterator; |
30
|
|
|
use Exception; |
31
|
|
|
use Opine\Interfaces\Cache as CacheInterface; |
32
|
|
|
|
33
|
|
|
class Service |
34
|
|
|
{ |
35
|
|
|
private $root; |
36
|
|
|
private $cache; |
37
|
|
|
private $config; |
38
|
|
|
private $bundles; |
39
|
|
|
private $container; |
40
|
|
|
private $route; |
41
|
|
|
private $topics; |
42
|
|
|
private $cachePrefix; |
43
|
|
|
|
44
|
|
|
public function __construct($root, CacheInterface $cache, $config, $bundles, $container, $route, $topics) |
45
|
|
|
{ |
46
|
|
|
$this->root = $root; |
47
|
|
|
$this->cache = $cache; |
48
|
|
|
$this->config = $config; |
49
|
|
|
$this->bundles = $bundles; |
50
|
|
|
$this->container = $container; |
51
|
|
|
$this->route = $route; |
52
|
|
|
$this->topics = $topics; |
53
|
|
|
|
54
|
|
|
// set environment |
55
|
|
|
$environment = 'default'; |
56
|
|
|
$test = getenv('OPINE_ENV'); |
57
|
|
|
if ($test !== false) { |
58
|
|
|
$environment = $test; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// set project name |
62
|
|
|
$projectName = 'project'; |
63
|
|
|
$test = getenv('OPINE_PROJECT'); |
64
|
|
|
if ($test !== false) { |
65
|
|
|
$projectName = $test; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->cachePrefix = $projectName . $environment; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function project() |
72
|
|
|
{ |
73
|
|
|
//clear memcache and filesystem |
74
|
|
|
$this->clearCache(); |
75
|
|
|
$this->clearFileCache(); |
76
|
|
|
|
77
|
|
|
//build each type of thing |
78
|
|
|
$this->config->build(); |
79
|
|
|
$this->bundles->build(); |
80
|
|
|
$this->container->build(); |
81
|
|
|
$this->bundles->build(); |
82
|
|
|
$this->container->build(); |
83
|
|
|
$this->route->build(); |
84
|
|
|
$this->topics->build(); |
85
|
|
|
|
86
|
|
|
//put cache data into memcache |
87
|
|
|
$cache = [ |
88
|
|
|
'bundles' => $this->getFile($this->root.'/../var/cache/bundles.json'), |
89
|
|
|
'topics' => $this->getFile($this->root.'/../var/cache/topics.json'), |
90
|
|
|
'routes' => $this->getFile($this->root.'/../var/cache/routes.json'), |
91
|
|
|
'container' => $this->getFile($this->root.'/../var/cache/container.json'), |
92
|
|
|
'config' => $this->getFile($this->root.'/../var/cache/config.json') |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$this->cache->set($this->cachePrefix . '-opine', json_encode($cache)); |
|
|
|
|
96
|
|
|
return $cache; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private function getFile ($path) |
100
|
|
|
{ |
101
|
|
|
if (!file_exists($path)) { |
102
|
|
|
return []; |
103
|
|
|
} |
104
|
|
|
return json_decode(file_get_contents($path), true); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function clearCache() |
108
|
|
|
{ |
109
|
|
|
$this->cache->delete($this->cachePrefix . '-opine'); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function clearFileCache() |
113
|
|
|
{ |
114
|
|
|
$dirPath = $this->root.'/../var/cache'; |
115
|
|
|
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirPath, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST) as $path) { |
116
|
|
|
$path->isDir() && !$path->isLink() ? rmdir($path->getPathname()) : unlink($path->getPathname()); |
117
|
|
|
} |
118
|
|
|
rmdir($dirPath); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check looks for function calls that miss required arguments.