-
Notifications
You must be signed in to change notification settings - Fork 0
/
symfony-post-deploy.php
203 lines (174 loc) · 5.83 KB
/
symfony-post-deploy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
require_once 'symfony-shell.php';
/**
* Recursive copy a file or directory
*
* @param string $src
* The source file|folder
* @param string $dst
* The destination file|folder
* @param octal $mode
* The destination file|folder permission (octal mode)
* @return bool Returns true on success, false otherwise
*/
function _copy($src, $dst, $mode = 0755) {
$success = true;
if (is_dir ( $src )) {
if ($success &= is_dir ( $dst ) || mkdir ( $dst, $mode, true )) {
$files = scandir ( $src );
foreach ( $files as $file ) {
if ($file != "." && $file != "..")
$success &= _copy ( "$src/$file", "$dst/$file" );
}
}
} else if (file_exists ( $src ))
$success &= copy ( $src, $dst );
return $success;
}
/**
* Installs the Composer required components
*
* @return bool Returns true on success, false otherwise
*/
function composer_install() {
$output = SymfonyShell\run_composer ( 'install', array (
'optimize-autoloader' => null,
'no-interaction' => null
) );
SymfonyShell\echoTerminaCmd ( $output );
return ! $output [4]; // returns the cmd exec exit code
}
/**
* Installs the Composer required components
*
* @return bool Returns true on success, false otherwise
*/
function composer_update() {
$output = SymfonyShell\run_composer ( 'update', array (
'no-interaction' => null
) );
SymfonyShell\echoTerminaCmd ( $output );
return ! $output [4]; // returns the cmd exec exit code
}
/**
* Helper function that will diagnose the composer.json file
*
* @return boolean Returns true on success, false otherwise
*/
function composer_diagnose() {
$output = SymfonyShell\run_composer ( 'diagnose', array (
'no-interaction' => null
) );
SymfonyShell\echoTerminaCmd ( $output );
return ! $output [4]; // returns the cmd exec exit code
}
/**
* Helper function that will validate the composer.json file
*
* @return boolean Returns true on success, false otherwise
*/
function composer_validate() {
$output = SymfonyShell\run_composer ( 'validate', array (
'no-interaction' => null
) );
SymfonyShell\echoTerminaCmd ( $output );
return ! $output [4]; // returns the cmd exec exit code
}
/**
* Dumps the Symfony assets
*
* @param string $environment
* The Symfony environments (eg. dev, prod, etc)
* @return bool Returns true on success, false otherwise
*/
function symfony_dump_assets($environment = 'prod') {
$output = SymfonyShell\run_symfony_console ( 'assetic:dump', array (
'env' => $environment,
'no-debug' => null
) );
SymfonyShell\echoTerminaCmd ( $output );
return ! $output [4]; // returns the cmd exec exit code
}
/**
* Clears the Symfony cache directory
*
* @param string $environment
* The Symfony environment (eg. prod,dev,tests)
* @return bool Returns true on success, false otherwise
*/
function symfony_cache_clear($environment = 'prod') {
$output = SymfonyShell\run_symfony_console ( 'cache:clear', array (
'env' => $environment,
'no-debug' => null
) );
SymfonyShell\echoTerminaCmd ( $output );
return ! $output [4]; // returns the cmd exec exit code
}
/**
* Install the bundle assets to the public (eg.
* web) directory
*
* @param string $environment
* The Symfony environments (eg. dev, prod, etc)
* @param string $symlink
* When true symlinks the assets otherwise copy them
* @param string $relative
* When true make relative symlinks
* @return bool Returns true on success, false otherwise
*/
function symfony_assets_install($environment = 'prod', $symlink = false, $relative = false) {
$args = array (
'env' => $environment,
'no-debug' => null
);
$symlink && $args ['symlink'] = null;
$relative && $args ['relative'] = null;
$output = SymfonyShell\run_symfony_console ( 'assets:install', $args );
SymfonyShell\echoTerminaCmd ( $output );
return ! $output [4]; // returns the cmd exec exit code
}
/**
* This is a custom hook that has nothing to do with Composer/Symfony
*
* @param string $src
* @param string $dst
* @return bool Returns true on success, false otherwise
*/
function copy_vendor_assets($src, $dst) {
$result = false;
$start = microtime ( true );
$echo = function ($string, $is_error = false) use (&$src, &$dst, &$start) {
SymfonyShell\echoTerminaCmd ( array (
sprintf ( 'cp %s %s', $src, $dst ),
array (
$is_error ? '' : $string
),
array (
$is_error ? $string : ''
),
microtime ( true ) - $start,
0
) );
};
if (is_dir ( __DIR__ . $src )) {
if ($result = _copy ( __DIR__ . $src, __DIR__ . $dst ))
$echo ( sprintf ( 'Folder %s copied successfully to %s', $src, $dst ) );
else {
$sys_err = error_get_last ();
$echo ( sprintf ( '%s (%s)', $sys_err ['message'], $sys_err ['type'] ) );
}
} else
$echo ( sprintf ( 'Directory %s does not exist', $src ) );
return $result;
}
// register our custom hooks
SymfonyShell\register_hook ( 'composer_install' ); // install the required dependencies as per composer.json
SymfonyShell\register_hook ( 'copy_vendor_assets', '/vendor/thomaspark', '/web/bundles/vendor/thomaspark' ); // should be registered before `symfony_dump_assets` hook
SymfonyShell\register_hook ( 'copy_vendor_assets', '/vendor/fortawesome', '/web/bundles/vendor/fortawesome' ); // should be registered before `symfony_dump_assets` hook
SymfonyShell\register_hook ( 'symfony_assets_install', 'prod', true ); // install the bundle assets to the default dir (default "web")
SymfonyShell\register_hook ( 'symfony_dump_assets' ); // dump the assets to the public folder (eg. web)
SymfonyShell\register_hook ( 'symfony_cache_clear' ); // clear the default (production) cache
SymfonyShell\register_hook ( 'symfony_cache_clear', 'dev' ); // explicitely clear the development cache
// run the registered hook functions
SymfonyShell\run ();
?>