1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Yawik project. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Yawik\Composer; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
13
|
|
|
use Yawik\Composer\Event\ConfigureEvent; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class PermissionsFixer |
17
|
|
|
* @package Yawik\Composer |
18
|
|
|
* @author Anthonius Munthi <http://itstoni.com> |
19
|
|
|
* @since 0.32.0 |
20
|
|
|
*/ |
21
|
|
|
class PermissionsFixer |
22
|
|
|
{ |
23
|
|
|
use LogTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Filesystem |
27
|
|
|
*/ |
28
|
|
|
protected $filesystem; |
29
|
|
|
|
30
|
7 |
|
public function __construct() |
31
|
|
|
{ |
32
|
7 |
|
$this->filesystem = new Filesystem(); |
33
|
7 |
|
} |
34
|
|
|
|
35
|
1 |
|
public function onConfigureEvent(ConfigureEvent $event) |
36
|
|
|
{ |
37
|
1 |
|
$modules = $event->getModules(); |
38
|
1 |
|
$options = $event->getOptions(); |
39
|
1 |
|
$files = []; |
40
|
1 |
|
$directories = []; |
41
|
|
|
|
42
|
1 |
|
foreach ($modules as $module) { |
43
|
1 |
|
if ($module instanceof RequireFilePermissionInterface) { |
44
|
1 |
|
$modFileLists = $module->getRequiredFileLists($options); |
45
|
1 |
|
if (!is_null($modFileLists) && !is_array($modFileLists)) { |
46
|
1 |
|
$this->logError(sprintf( |
47
|
1 |
|
'<comment>%s::getRequiredFileList()</comment> should return an array.', |
48
|
1 |
|
get_class($module) |
49
|
|
|
)); |
50
|
|
|
} else { |
51
|
1 |
|
$files = array_merge($files, $modFileLists); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
if ($module instanceof RequireDirectoryPermissionInterface) { |
56
|
1 |
|
$modDirLists = $module->getRequiredDirectoryLists($options); |
57
|
1 |
|
if (!is_null($modDirLists) && !is_array($modDirLists)) { |
58
|
1 |
|
$this->logError(sprintf( |
59
|
1 |
|
'<comment>%s::getRequiredDirectoryList()</comment> should return an array.', |
60
|
1 |
|
get_class($module) |
61
|
|
|
)); |
62
|
|
|
} else { |
63
|
1 |
|
$directories = array_merge($directories, $modDirLists); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
foreach ($directories as $directory) { |
69
|
1 |
|
if (!is_dir($directory)) { |
70
|
1 |
|
$this->mkdir($directory); |
71
|
|
|
} |
72
|
1 |
|
$this->chmod($directory); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
foreach ($files as $file) { |
76
|
1 |
|
if (!is_file($file)) { |
77
|
1 |
|
$this->touch($file); |
78
|
|
|
} |
79
|
1 |
|
$this->chmod($file, 0666); |
80
|
|
|
} |
81
|
1 |
|
} |
82
|
|
|
|
83
|
2 |
|
public function touch($file) |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
2 |
|
$this->filesystem->touch($file); |
87
|
1 |
|
$this->log('created <info>'.$file.'</info>', 'touch'); |
88
|
1 |
|
} catch (\Exception $exception) { |
89
|
1 |
|
$this->logError($exception->getMessage(), 'touch'); |
90
|
|
|
} |
91
|
2 |
|
} |
92
|
|
|
|
93
|
2 |
|
public function chmod($dir, $mode = 0777) |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
2 |
|
$this->filesystem->chmod($dir, $mode); |
97
|
1 |
|
$fileperms = decoct(@fileperms($dir) & 0777); |
98
|
1 |
|
$message = sprintf('<comment>%s</comment> with %s', $dir, $fileperms); |
99
|
1 |
|
$this->log($message, 'chmod'); |
100
|
1 |
|
} catch (\Exception $exception) { |
101
|
1 |
|
$this->logError($exception->getMessage(), 'chmod'); |
102
|
|
|
} |
103
|
2 |
|
} |
104
|
|
|
|
105
|
2 |
|
public function mkdir($dir, $mode = 0777) |
106
|
|
|
{ |
107
|
|
|
try { |
108
|
2 |
|
$this->filesystem->mkdir($dir, $mode); |
109
|
1 |
|
$this->log(sprintf('<comment>%s</comment>', $dir), 'mkdir'); |
110
|
1 |
|
} catch (\Exception $e) { |
111
|
1 |
|
$this->logError($e->getMessage(), 'mkdir'); |
112
|
|
|
} |
113
|
2 |
|
} |
114
|
|
|
} |
115
|
|
|
|