1 | <?php |
||
28 | class Checkpoint { |
||
29 | |||
30 | const CORE_DIR = 'core'; |
||
31 | const APP_DIR = 'apps'; |
||
32 | |||
33 | /** |
||
34 | * @var Locator $locator |
||
35 | */ |
||
36 | protected $locator; |
||
37 | |||
38 | /** |
||
39 | * @var Filesystemhelper $fsHelper |
||
40 | */ |
||
41 | protected $fsHelper; |
||
42 | |||
43 | /** |
||
44 | * |
||
45 | * @param Locator $locator |
||
46 | * @param FilesystemHelper $fsHelper |
||
47 | */ |
||
48 | 2 | public function __construct(Locator $locator, FilesystemHelper $fsHelper){ |
|
52 | |||
53 | /** |
||
54 | * Creates a checkpoint |
||
55 | * @return string |
||
56 | * @throws Exception if base checkpoint directory is not writable |
||
57 | */ |
||
58 | public function create(){ |
||
59 | $checkpointId = $this->createCheckpointId(); |
||
60 | $checkpointPath = $this->getCheckpointPath($checkpointId); |
||
61 | try{ |
||
62 | if (!$this->fsHelper->isWritable($this->locator->getCheckpointDir())){ |
||
63 | throw new \Exception($this->locator->getCheckpointDir() . ' is not writable.'); |
||
64 | } |
||
65 | $this->fsHelper->mkdir($checkpointPath); |
||
66 | |||
67 | $checkpointCorePath = $checkpointPath . '/' . self::CORE_DIR; |
||
68 | $this->fsHelper->mkdir($checkpointCorePath); |
||
69 | $core = $this->locator->getRootDirItems(); |
||
70 | foreach ($core as $coreItem){ |
||
71 | $cpItemPath = $checkpointCorePath . '/' . basename($coreItem); |
||
72 | $this->fsHelper->copyr($coreItem, $cpItemPath, true); |
||
73 | } |
||
74 | //copy config.php |
||
75 | $configDirSrc = $this->locator->getOwnCloudRootPath() . '/config'; |
||
76 | $configDirDst = $checkpointCorePath . '/config'; |
||
77 | $this->fsHelper->copyr($configDirSrc, $configDirDst, true); |
||
78 | |||
79 | $checkpointAppPath = $checkpointPath . '/' . self::APP_DIR; |
||
80 | $this->fsHelper->mkdir($checkpointAppPath); |
||
81 | $appManager = Application::$container['utils.appmanager']; |
||
82 | $apps = $appManager->getAllApps(); |
||
83 | foreach ($apps as $appId){ |
||
84 | $appPath = $appManager->getAppPath($appId); |
||
85 | if ($appPath){ |
||
86 | $this->fsHelper->copyr($appPath, $checkpointAppPath . '/' . $appId, true); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | } catch (\Exception $e){ |
||
91 | $application = Application::$container['application']; |
||
92 | $application->getLogger()->error($e->getMessage()); |
||
93 | $this->fsHelper->removeIfExists($checkpointPath); |
||
94 | throw $e; |
||
95 | } |
||
96 | return $checkpointId; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Restore a checkpoint by id |
||
101 | * @param string $checkpointId id of checkpoint |
||
102 | * @return array |
||
103 | * @throws UnexpectedValueException if there is no checkpoint with this id |
||
104 | */ |
||
105 | public function restore($checkpointId){ |
||
112 | |||
113 | /** |
||
114 | * Remove a checkpoint by id |
||
115 | * @param string $checkpointId id of checkpoint |
||
116 | * @return array |
||
117 | * @throws UnexpectedValueException if there is no checkpoint with this id |
||
118 | */ |
||
119 | public function remove($checkpointId){ |
||
124 | |||
125 | /** |
||
126 | * Return all checkpoints as an array of items [ 'title', 'date' ] |
||
127 | * @return array |
||
128 | */ |
||
129 | 2 | public function getAll(){ |
|
130 | 2 | $checkpoints = []; |
|
131 | 2 | foreach ($this->getAllCheckpointIds() as $dir){ |
|
132 | 1 | $checkpoints[] = [ |
|
133 | 1 | 'title' => $dir, |
|
134 | 1 | 'date' => date( |
|
135 | 1 | "F d Y H:i", |
|
136 | 1 | $this->fsHelper->filemtime( |
|
137 | 1 | $this->locator->getCheckpointDir() . '/' . $dir |
|
138 | ) |
||
139 | ) |
||
140 | ]; |
||
141 | } |
||
142 | 2 | return $checkpoints; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Check if there is a checkpoint with a given id |
||
147 | * @param string $checkpointId id of checkpoint |
||
148 | * @return bool |
||
149 | */ |
||
150 | public function checkpointExists($checkpointId){ |
||
153 | |||
154 | /** |
||
155 | * Get the most recent checkpoint Id |
||
156 | * @return string|bool |
||
157 | */ |
||
158 | public function getLastCheckpointId(){ |
||
162 | |||
163 | /** |
||
164 | * Return array of all checkpoint ids |
||
165 | * @return array |
||
166 | */ |
||
167 | 2 | public function getAllCheckpointIds(){ |
|
168 | 2 | $checkpointDir = $this->locator->getCheckpointDir(); |
|
169 | 2 | $content = $this->fsHelper->isDir($checkpointDir) ? $this->fsHelper->scandir($checkpointDir) : []; |
|
170 | 2 | $checkpoints = array_filter( |
|
171 | $content, |
||
172 | 2 | function($dir){ |
|
173 | 1 | $checkpointPath = $this->getCheckpointPath($dir); |
|
174 | 1 | return !in_array($dir, ['.', '..']) && $this->fsHelper->isDir($checkpointPath); |
|
175 | 2 | } |
|
176 | ); |
||
177 | 2 | return $checkpoints; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Create an unique checkpoint id |
||
182 | * @return string |
||
183 | */ |
||
184 | protected function createCheckpointId(){ |
||
188 | |||
189 | /** |
||
190 | * Get an absolute path to the checkpoint directory by checkpoint Id |
||
191 | * @param string $checkpointId id of checkpoint |
||
192 | * @return string |
||
193 | */ |
||
194 | 1 | public function getCheckpointPath($checkpointId){ |
|
197 | |||
198 | /** |
||
199 | * Produce an error on non-existing checkpoints |
||
200 | * @param string $checkpointId id of checkpoint |
||
201 | * @throws \UnexpectedValueException if there is no checkpoint with this id |
||
202 | */ |
||
203 | private function assertCheckpointExists($checkpointId){ |
||
209 | } |
||
210 |