1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Application\Tool; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Borut Balažek <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class Storage |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Prepare all the folders and files for storage. |
14
|
|
|
*/ |
15
|
|
|
public static function prepare() |
16
|
|
|
{ |
17
|
|
|
self::prepareFolders(array( |
18
|
|
|
'var', |
19
|
|
|
'var/backups', |
20
|
|
|
'var/backups/database', |
21
|
|
|
'var/cache', |
22
|
|
|
'var/cache/assetic', |
23
|
|
|
'var/cache/file', |
24
|
|
|
'var/cache/http', |
25
|
|
|
'var/cache/profiler', |
26
|
|
|
'var/cache/proxy', |
27
|
|
|
'var/cache/template', |
28
|
|
|
'var/cache/security', |
29
|
|
|
'var/database', |
30
|
|
|
'var/logs', |
31
|
|
|
'var/sessions', |
32
|
|
|
'var/mailer', |
33
|
|
|
'var/mailer/spool', |
34
|
|
|
)); |
35
|
|
|
|
36
|
|
|
self::prepareLogFiles(array( |
37
|
|
|
'var/logs/development.log', |
38
|
|
|
'var/logs/testing.log', |
39
|
|
|
'var/logs/staging.log', |
40
|
|
|
'var/logs/production.log', |
41
|
|
|
)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Prepare the folders for storage. |
46
|
|
|
* |
47
|
|
|
* @param array $paths |
48
|
|
|
* @param bool $removeIfExists |
49
|
|
|
*/ |
50
|
|
|
public static function prepareFolders(array $paths = array(), $removeIfExists = false) |
51
|
|
|
{ |
52
|
|
|
if (empty($paths)) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$fs = new Filesystem(); |
57
|
|
|
|
58
|
|
View Code Duplication |
foreach ($paths as $path) { |
|
|
|
|
59
|
|
|
if ( |
60
|
|
|
$removeIfExists && |
61
|
|
|
$fs->exists($path) |
62
|
|
|
) { |
63
|
|
|
$fs->remove($path); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$fs->mkdir($paths); |
67
|
|
|
$fs->chmod($path, 0777); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Prepare the uploads folder (so images can be uploaded). |
73
|
|
|
* |
74
|
|
|
* @param string $uploadsDirectory |
75
|
|
|
*/ |
76
|
|
|
public static function prepareUploadsFolder($uploadsDirectory) |
77
|
|
|
{ |
78
|
|
|
if (!$uploadsDirectory) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$fs = new Filesystem(); |
83
|
|
|
|
84
|
|
|
$uploadsDirectory = 'web/assets/uploads'; |
85
|
|
|
|
86
|
|
|
if (!$fs->exists($uploadsDirectory)) { |
87
|
|
|
$fs->mkdir($uploadsDirectory, 0755); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$user = PHP_OS == 'Darwin' // Fix for OSX |
91
|
|
|
? get_current_user() |
92
|
|
|
: 'www-data' |
93
|
|
|
; |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
$fs->chown($uploadsDirectory, $user); |
97
|
|
|
$fs->chmod($uploadsDirectory, 0755); |
98
|
|
|
} catch (\Exception $e) { |
99
|
|
|
// Could not chown and / or chmod the uploads directory |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Prepare the log files. |
105
|
|
|
* |
106
|
|
|
* @param array $paths |
107
|
|
|
* @param bool $removeIfExists |
108
|
|
|
*/ |
109
|
|
|
public static function prepareLogFiles(array $paths, $removeIfExists = false) |
110
|
|
|
{ |
111
|
|
|
$fs = new Filesystem(); |
112
|
|
|
|
113
|
|
View Code Duplication |
foreach ($paths as $path) { |
|
|
|
|
114
|
|
|
if ( |
115
|
|
|
$removeIfExists && |
116
|
|
|
$fs->exists($path) |
117
|
|
|
) { |
118
|
|
|
$fs->remove($path); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$fs->touch($path); |
122
|
|
|
$fs->chmod($path, 0777); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.