1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Symfony\Bundle\AsseticBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class GzipProperties for wrapping variables to file_put_contents |
9
|
|
|
* |
10
|
|
|
* @author KonstantinKuklin <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class GzipProperties |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Is compression enabled |
16
|
|
|
* |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
public static $use = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Level of compression |
23
|
|
|
* |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
public static $level = 9; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Output flow interface |
30
|
|
|
* |
31
|
|
|
* @var OutputInterface |
32
|
|
|
*/ |
33
|
|
|
public static $output = null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Override standard function file_put_contents for |
38
|
|
|
* Symfony\Bundle\AsseticBundle\Command namespace |
39
|
|
|
* |
40
|
|
|
* @param string $filePath Assetic file path |
41
|
|
|
* @param string $content Assetic file content |
42
|
|
|
*/ |
43
|
|
|
function file_put_contents($filePath, $content) |
44
|
|
|
{ |
45
|
|
|
// save assetic file |
46
|
|
|
@\file_put_contents($filePath, $content); |
|
|
|
|
47
|
|
|
|
48
|
|
|
// check gzipping |
49
|
|
|
if (GzipProperties::$use) { |
50
|
|
|
|
51
|
|
|
// check gzip extension |
52
|
|
|
if (!function_exists("gzencode")) { |
53
|
|
|
throw new \RuntimeException('Unable to find Zlib library'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$filePath .= '.gz'; |
57
|
|
|
GzipProperties::$output->writeln( |
58
|
|
|
sprintf( |
59
|
|
|
'<comment>%s</comment> <info>[gzipped file+]</info> %s', |
60
|
|
|
date('H:i:s'), |
61
|
|
|
$filePath |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
if (false === @\file_put_contents($filePath, gzencode($content, GzipProperties::$level))) { |
66
|
|
|
throw new \RuntimeException('Unable to write file ' . $filePath); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
namespace KonstantinKuklin\AsseticStaticGzipBundle\Command; |
72
|
|
|
|
73
|
|
|
use Symfony\Bundle\AsseticBundle\Command\DumpCommand as BaseCommand; |
|
|
|
|
74
|
|
|
use Symfony\Bundle\AsseticBundle\Command\GzipProperties; |
|
|
|
|
75
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
|
|
|
76
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Override assetic bundle command for gzip purposes. |
80
|
|
|
* |
81
|
|
|
* @author Konstantin Kuklin <[email protected]> |
82
|
|
|
*/ |
83
|
|
|
class DumpCommand extends BaseCommand |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
89
|
|
|
{ |
90
|
|
|
// save properties |
91
|
|
|
GzipProperties::$use = $this->getContainer()->getParameter('assetic_static_gzip.use'); |
92
|
|
|
GzipProperties::$level = $this->getContainer()->getParameter('assetic_static_gzip.level'); |
93
|
|
|
GzipProperties::$output = $output; |
94
|
|
|
|
95
|
|
|
parent::initialize($input, $output); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: