1 | <?php |
||
31 | class Checkpoint { |
||
32 | |||
33 | const CORE_DIR = 'core'; |
||
34 | const APP_DIR = 'apps'; |
||
35 | |||
36 | /** |
||
37 | * @var Locator $locator |
||
38 | */ |
||
39 | protected $locator; |
||
40 | |||
41 | /** |
||
42 | * @var Filesystemhelper $fsHelper |
||
43 | */ |
||
44 | protected $fsHelper; |
||
45 | |||
46 | /** |
||
47 | * |
||
48 | * @param Locator $locator |
||
49 | * @param FilesystemHelper $fsHelper |
||
50 | */ |
||
51 | 2 | public function __construct(Locator $locator, FilesystemHelper $fsHelper){ |
|
55 | |||
56 | /** |
||
57 | * Creates a checkpoint |
||
58 | * @return string |
||
59 | * @throws \Exception if base checkpoint directory is not writable |
||
60 | */ |
||
61 | public function create(){ |
||
62 | $checkpointId = $this->createCheckpointId(); |
||
63 | $checkpointPath = $this->getCheckpointPath($checkpointId); |
||
64 | try{ |
||
65 | if (!$this->fsHelper->isWritable($this->locator->getCheckpointDir())){ |
||
66 | throw new \Exception($this->locator->getCheckpointDir() . ' is not writable.'); |
||
67 | } |
||
68 | $this->fsHelper->mkdir($checkpointPath); |
||
69 | |||
70 | $checkpointCorePath = $checkpointPath . '/' . self::CORE_DIR; |
||
71 | $this->fsHelper->mkdir($checkpointCorePath); |
||
72 | $core = $this->locator->getRootDirItems(); |
||
73 | foreach ($core as $coreItem){ |
||
74 | $cpItemPath = $checkpointCorePath . '/' . basename($coreItem); |
||
75 | $this->fsHelper->copyr($coreItem, $cpItemPath, true); |
||
76 | } |
||
77 | //copy config.php |
||
78 | $configDirSrc = $this->locator->getOwnCloudRootPath() . '/config'; |
||
79 | $configDirDst = $checkpointCorePath . '/config'; |
||
80 | $this->fsHelper->copyr($configDirSrc, $configDirDst, true); |
||
81 | |||
82 | $checkpointAppPath = $checkpointPath . '/' . self::APP_DIR; |
||
83 | $this->fsHelper->mkdir($checkpointAppPath); |
||
84 | $appManager = Application::$container['utils.appmanager']; |
||
85 | $apps = $appManager->getAllApps(); |
||
86 | foreach ($apps as $appId){ |
||
87 | $appPath = $appManager->getAppPath($appId); |
||
88 | if ($appPath){ |
||
89 | $this->fsHelper->copyr($appPath, $checkpointAppPath . '/' . $appId, true); |
||
90 | } |
||
91 | } |
||
92 | |||
93 | } catch (\Exception $e){ |
||
94 | $application = Application::$container['application']; |
||
95 | $application->getLogger()->error($e->getMessage()); |
||
96 | $this->fsHelper->removeIfExists($checkpointPath); |
||
97 | throw $e; |
||
98 | } |
||
99 | return $checkpointId; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Restore a checkpoint by id |
||
104 | * @param string $checkpointId id of checkpoint |
||
105 | * @return array |
||
106 | * @throws \UnexpectedValueException if there is no checkpoint with this id |
||
107 | */ |
||
108 | public function restore($checkpointId){ |
||
109 | $this->assertCheckpointExists($checkpointId); |
||
110 | $checkpointDir = $this->locator->getCheckpointDir() . '/' . $checkpointId; |
||
111 | $ocRoot = $this->locator->getOwnCloudRootPath(); |
||
112 | $this->fsHelper->copyr($checkpointDir . '/' . self::CORE_DIR, $ocRoot, false); |
||
113 | $this->fsHelper->copyr($checkpointDir . '/' . self::APP_DIR, $ocRoot . '/' . self::APP_DIR, false); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Remove a checkpoint by id |
||
118 | * @param string $checkpointId id of checkpoint |
||
119 | * @return array |
||
120 | * @throws \UnexpectedValueException if there is no checkpoint with this id |
||
121 | */ |
||
122 | public function remove($checkpointId){ |
||
127 | |||
128 | /** |
||
129 | * Return all checkpoints as an array of items [ 'title', 'date' ] |
||
130 | * @return array |
||
131 | */ |
||
132 | 2 | public function getAll(){ |
|
147 | |||
148 | /** |
||
149 | * Check if there is a checkpoint with a given id |
||
150 | * @param string $checkpointId id of checkpoint |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function checkpointExists($checkpointId){ |
||
154 | return in_array($checkpointId, $this->getAllCheckpointIds()); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Get the most recent checkpoint Id |
||
159 | * @return string|bool |
||
160 | */ |
||
161 | public function getLastCheckpointId(){ |
||
165 | |||
166 | /** |
||
167 | * Return array of all checkpoint ids |
||
168 | * @return array |
||
169 | */ |
||
170 | 2 | public function getAllCheckpointIds(){ |
|
171 | 2 | $checkpointDir = $this->locator->getCheckpointDir(); |
|
172 | 2 | $content = $this->fsHelper->isDir($checkpointDir) ? $this->fsHelper->scandir($checkpointDir) : []; |
|
173 | 2 | $checkpoints = array_filter( |
|
174 | 2 | $content, |
|
175 | function($dir){ |
||
176 | 1 | $checkpointPath = $this->getCheckpointPath($dir); |
|
177 | 1 | return !in_array($dir, ['.', '..']) && $this->fsHelper->isDir($checkpointPath); |
|
178 | 2 | } |
|
179 | ); |
||
180 | 2 | return $checkpoints; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Create an unique checkpoint id |
||
185 | * @return string |
||
186 | */ |
||
187 | protected function createCheckpointId(){ |
||
188 | $versionString = implode('.', $this->locator->getInstalledVersion()); |
||
189 | return uniqid($versionString . '-'); |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Get an absolute path to the checkpoint directory by checkpoint Id |
||
194 | * @param string $checkpointId id of checkpoint |
||
195 | * @return string |
||
196 | */ |
||
197 | 1 | public function getCheckpointPath($checkpointId){ |
|
200 | |||
201 | /** |
||
202 | * Produce an error on non-existing checkpoints |
||
203 | * @param string $checkpointId id of checkpoint |
||
204 | * @throws \UnexpectedValueException if there is no checkpoint with this id |
||
205 | */ |
||
206 | private function assertCheckpointExists($checkpointId){ |
||
212 | } |
||
213 |