-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackagerHelper.php
More file actions
94 lines (75 loc) · 2.91 KB
/
PackagerHelper.php
File metadata and controls
94 lines (75 loc) · 2.91 KB
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
<?php
require_once __DIR__ . '/../../vendor/packager/Source.php';
require_once __DIR__ . '/../../vendor/jsmin-php/jsmin.php';
class PackagerHelper
{
static $scripts = array();
static $runtime_code = array();
static function compile($source)
{
$code = Packager::strip_blocks($source->build(), '1.2compat');
if (sfConfig::get('app_sf_packager_plugin_use_compression')) $code = JSMin::minify($code);
return $code;
}
static function shouldCompile($file)
{
if (sfConfig::get('app_sf_packager_plugin_compile')) return true;
if (!file_exists($file)) return true;
if (sfConfig::get('app_sf_packager_plugin_check_dates') && (time() - filemtime($file)) > 86400) return true;
}
}
function js($name = '', $requires = array(), $provides = null)
{
PackagerHelper::$scripts[] = $script = new Source(sfConfig::get('sf_app'));
if ($name) $script->set_name($name);
if ($requires) $script->requires($requires);
if ($provides) $script->provides($provides);
ob_start();
}
# note(ibolmo): CDATA is kept.
function end_js()
{
preg_match('/<script[^>]*>([\s\S]*?)<\/script>/i', ob_get_clean(), $matches);
$source = end(PackagerHelper::$scripts);
$source->set_code($matches[1]);
$source->parse();
}
function js_tag()
{
ob_start();
}
function end_js_tag()
{
preg_match('/<script[^>]*>([\s\S]*?)<\/script>/i', ob_get_clean(), $matches);
PackagerHelper::$runtime_code[] = $matches[1];
}
function include_js()
{
if (empty(PackagerHelper::$scripts)) return;
$env = sfContext::getInstance()->getConfiguration()->getEnvironment() ?: 'prod';
$key = sha1(implode('', PackagerHelper::$scripts)) . '-' . $env;
$cache_dir = sfConfig::get('sf_web_dir') . '/cache/js';
$file = $cache_dir . "/$key.js";
if (PackagerHelper::shouldCompile($file) || !file_exists($file)){
$packager = Packager::get_instance();
$files = sfFinder::type('any')->name('*package.yml')->name('*package.json')->in(sfConfig::get('sf_lib_dir') . '/js/');
foreach ($files as $package) $packager->add_package($package);
$source = new Source(sfConfig::get('sf_app'));
$source->requires(PackagerHelper::$scripts);
if (!is_writable($cache_dir) && !mkdir($cache_dir, 0777, true)) throw new sfException('Could not write cache js dir.');
$code = PackagerHelper::compile($source);
if (sfConfig::get('app_sf_packager_plugin_user_compression')) $code = JSMin::minify($code);
file_put_contents($file, $code);
}
echo content_tag('script', '', array('type' => 'text/javascript', 'src' => "/cache/js/$key.js"));
foreach (PackagerHelper::$runtime_code as $code){
if (sfConfig::get('app_sf_packager_plugin_use_compression')) $code = JSMin::minify($code);
echo content_tag('script', $code);
}
}
function require_js($module)
{
$files = sfFinder::type('any')->name($module)->in(sfConfig::get('sf_lib_dir').'/js/');
if (empty($files)) throw new sfException("Could not find '$module'.");
foreach ($files as $file) PackagerHelper::$scripts[] = new Source(sfConfig::get('sf_app'), $file);
}