|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* This file is part of the BEAR.AppMeta package. |
|
6
|
|
|
* |
|
7
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace BEAR\AppMeta; |
|
10
|
|
|
|
|
11
|
|
|
use BEAR\AppMeta\Exception\AppNameException; |
|
12
|
|
|
use BEAR\AppMeta\Exception\NotWritableException; |
|
13
|
|
|
|
|
14
|
|
|
class AppMeta extends AbstractAppMeta |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* ClearDir flags not to delete multiple times in single request, Such as unit testing |
|
18
|
|
|
* |
|
19
|
4 |
|
* @var string[] |
|
20
|
|
|
*/ |
|
21
|
4 |
|
private static $cleanUpFlg = []; |
|
22
|
4 |
|
|
|
23
|
1 |
|
/** |
|
24
|
|
|
* @param string $name application name (Vendor\Project) |
|
25
|
4 |
|
* @param string $context application context (prod-hal-app) |
|
26
|
4 |
|
* @param string $appDir application directory |
|
27
|
4 |
|
*/ |
|
28
|
4 |
|
public function __construct(string $name, string $context = 'app', string $appDir = '') |
|
29
|
|
|
{ |
|
30
|
|
|
$appModule = $name . '\Module\AppModule'; |
|
31
|
4 |
|
if (! class_exists($appModule)) { |
|
32
|
4 |
|
throw new AppNameException($name); |
|
33
|
|
|
} |
|
34
|
|
|
$this->name = $name; |
|
35
|
4 |
|
$this->appDir = $appDir ?: dirname((new \ReflectionClass($appModule))->getFileName(), 3); |
|
36
|
4 |
|
$this->tmpDir = $this->appDir . '/var/tmp/' . $context; |
|
37
|
1 |
|
if (! file_exists($this->tmpDir) && ! @mkdir($this->tmpDir, 0777, true) && ! is_dir($this->tmpDir)) { |
|
38
|
|
|
throw new NotWritableException($this->tmpDir); |
|
39
|
4 |
|
} |
|
40
|
|
|
$this->logDir = $this->appDir . '/var/log/' . $context; |
|
41
|
|
|
if (! file_exists($this->logDir) && ! @mkdir($this->logDir, 0777, true) && ! is_dir($this->logDir)) { |
|
42
|
|
|
throw new NotWritableException($this->logDir); |
|
43
|
|
|
} |
|
44
|
1 |
|
$isClearable = strpos($context, 'prod-') === false |
|
45
|
|
|
&& strpos($context, 'stage-') === false |
|
46
|
|
|
&& ! in_array($this->tmpDir, self::$cleanUpFlg, true) |
|
47
|
|
|
&& ! file_exists($this->tmpDir . '/.do_not_clear'); |
|
48
|
|
|
if ($isClearable) { |
|
49
|
1 |
|
$this->clearTmpDirectory($this->tmpDir); |
|
50
|
|
|
self::$cleanUpFlg[] = $this->tmpDir; |
|
51
|
1 |
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
private function clearTmpDirectory(string $dir) |
|
55
|
1 |
|
{ |
|
56
|
1 |
|
$unlink = function ($path) use (&$unlink) { |
|
57
|
1 |
|
foreach (glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*') as $file) { |
|
58
|
|
|
is_dir($file) ? $unlink($file) : unlink($file); |
|
59
|
1 |
|
@rmdir($file); |
|
60
|
1 |
|
} |
|
61
|
1 |
|
}; |
|
62
|
1 |
|
$unlink($dir); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|