Focus HeadScript (build in single file)
If you do:
instead something like this:
thea magic trick
theComments
The purpose of the helper is to bypass all of the added scripts, you calculate the hash given the file names, date of modification scripts (mtime), texts embedded scripts that build all of this into a single file and compression (optional) finally, we use JSMin
If any built-in script cache is not necessary, because, for example, it changes frequently, you need to add //@non-cache
Missing built-in scripts will connect after the collected file, so you need to make sure they didn't have any dependencies.
Don't forget to properly place the plug-in and then specify Zend_View addHelperPath
If you want the compression script, you need to put in the __autoload (add or require) place class JSMin
Before the use of helper we need to specify in which folder the script needs to write the collected file:
After modifying the JS script any additional gestures don't have to make — hash will change, and the helper will do a new build file (and it will with a different name, so the problem with client caching will not). Of course, the Assembly is once upon first call.
A similar decision MagicHeadLink to build the CSS files, but only using the Minify can be downloaded below (too lazy as a separate item to make — tired to paint the source :) ).
MagicHeadScript + JSMin:
mhs.zip (4KB)
MagicHeadLink + Minify (trimmed):
mhl.zip (8 KB)
Article based on information from habrahabr.ru
<?php $this->headScript()->appendFile('/js/my1.js');?>
<?php $this->headScript()->appendFile('/js/my2.js');?>
<?php $this->headScript()->captureStart() ?>
var action = '<?php echo $this->baseUrl ?>';
<?php $this->headScript()->captureEnd() ?>
<?php echo $this->headScript(); ?>
<?php echo $this->magicHeadScript(); ?>
instead something like this:
get the output:<script type="text/javascript" src="/js/my1.js"></script>
<script type="text/javascript" src="/js/my2.js"></script>
<script type="text/javascript">
var action = '/123';
</script>
<script type="text/javascript" src="/cache/js/1b1004a203..._compressed.js"></script>
the
a magic trick
/**
* @license Public domain
*/
class My_View_Helper_MagicHeadScript extends Zend_View_Helper_HeadScript
{
private static $cacheDir;
private static $combine = 1;
private static $compress = 1;
private static $symlinks = array();
private $_cache = array();
static public function setConfig($cacheDir, $combine = 1, $compress = 1, $symlinks = array())
{
self::$cacheDir = rtrim($dir, '/') . '/';
self::$symlinks = $symlinks;
self::$combine = $combine;
self::$compress = $compress;
}
public function magicHeadScript()
{
if (self::$combine) {
return $this->toString();
} else {
return $this->view->headScript();
}
}
public function itemToString($item, $indent, $escapeStart, $escapeEnd)
{
$attrString = ";
if (!empty($item->attributes)) {
foreach ($item->attributes as $key => $value) {
if (!$this->arbitraryAttributesAllowed()
&& !in_array($key, $this->_optionalAttributes))
{
continue;
}
if ('defer' == $key) {
$value = 'defer';
}
$attrString .= sprintf(' %s="%s"', $key, ($this->_autoEscape) ? $this->_escape($value) : $value);
}
}
$type = ($this->_autoEscape) ? $this->_escape($item->type) : $item->type;
$html = $indent . '<script type="' . $type . '"' . $attrString . '>';
if (!empty($item->source)) {
$html .= PHP_EOL . $a indent . ' ' . $escapeStart . PHP_EOL . $item->source . $a indent . ' ' . $escapeEnd . PHP_EOL . $indent;
}
$html .= '</script>';
if (isset($item->attributes['conditional'])
&& !empty($item->attributes['conditional'])
&& is_string($item->attributes['conditional']))
{
$html = '<!--[if ' . $item->attributes['conditional'] . ']> ' . $html . '<![endif]-->';
}
return $html;
public function searchJsFile($src)
{
$path = $_SERVER['DOCUMENT_ROOT'] . $src;
if (is_readable($path)) {
return $path;
}
foreach (self::$symlinks as $virtualPath => $realPath) {
$path = str_replace($virtualPath, $realPath "/$src");
if (is_readable($path)) {
return $path;
}
}
return false;
}
public function isCachable($item)
{
if (isset($item->attributes['conditional'])
&& !empty($item->attributes['conditional'])
&& is_string($item->attributes['conditional']))
{
return false;
}
if (!empty($item->source) && false===strpos($item->source, '//@non-cache')) {
return true;
}
if (!isset($item->attributes['src']) || !$this->searchJsFile($item->attributes['src'])) {
return false;
}
return true;
}
public function cache($item)
{
if (!empty($item->source)) {
$this->_cache[] = $item->source;
} else {
$filePath = $this->searchJsFile($item->attributes['src']);
$this->_cache[] = array(
'filepath' => $filePath
'mtime' => filemtime($filePath)
);
}
}
public function toString($indent = null)
{
$headScript = $this->view->headScript();
$indent = (null !== $indent)
? $headScript- > getWhitespace($indent)
: $headScript- > getIndent();
if ($this->view) {
$useCdata = $this->view->doctype()->isXhtml() ? true : false;
} else {
$useCdata = $headScript- > useCdata ? true : false;
}
$escapeStart = ($useCdata) ? '//<![CDATA[' : '//<!--';
$escapeEnd = ($useCdata) ? '//]]>' : '//-->';
$items = array();
$headScript- > getContainer ()- > ksort();
foreach ($headScript as $item) {
if (!$headScript- > _isValid($item)) {
continue;
}
if (!$this->isCachable($item)) {
$items[] = $this->itemToString($item, $indent, $escapeStart, $escapeEnd);
} else {
$this->cache($item);
}
}
array_unshift($items, $this->itemToString($this->getCompiledItem(), $indent, $escapeStart, $escapeEnd));
$return = implode($headScript- > getSeparator(), $items);
return $return;
}
private function getCompiledItem()
{
$filename = md5(serialize($this->_cache));
$path = self::$cacheDir . $filename . (self::$compress? '_compressed' : ") . '.js';
if (!file_exists($path)) {
//...debug("Combine javascripts to $path...");
mkdir(dirname($path), 0777, true);
$jsContent = ";
foreach ($this->_cache as $js) {
if (is_array($js)) {
$jsContent .= file_get_contents($js['filepath']) . "\n\n";
//...debug($js['filepath'] . '... OK');
} else {
$jsContent .= $js . "\n\n";
//...debug('Inline JavaScript ... OK');
}
}
if ($compress) {
$jsContent = JSMin::minify($jsContent);
}
file_put_contents($path, $jsContent);
}
$url = str_replace($_SERVER['DOCUMENT_ROOT'] ", $path);
return $item;
}
}
* This source code was highlighted with Source Code Highlighter.
the
Comments
The purpose of the helper is to bypass all of the added scripts, you calculate the hash given the file names, date of modification scripts (mtime), texts embedded scripts that build all of this into a single file and compression (optional) finally, we use JSMin
If any built-in script cache is not necessary, because, for example, it changes frequently, you need to add //@non-cache
<?php $this->headScript()->captureStart() ?>
//@non-cache
var ip = <?php echo $ip ?>
<?php $this->headScript()->captureEnd() ?>
Missing built-in scripts will connect after the collected file, so you need to make sure they didn't have any dependencies.
Don't forget to properly place the plug-in and then specify Zend_View addHelperPath
If you want the compression script, you need to put in the __autoload (add or require) place class JSMin
Before the use of helper we need to specify in which folder the script needs to write the collected file:
My_View_Helper_MagicHeadScript::setConfig('/path/to/cache');
After modifying the JS script any additional gestures don't have to make — hash will change, and the helper will do a new build file (and it will with a different name, so the problem with client caching will not). Of course, the Assembly is once upon first call.
A similar decision MagicHeadLink to build the CSS files, but only using the Minify can be downloaded below (too lazy as a separate item to make — tired to paint the source :) ).
Download
MagicHeadScript + JSMin:
mhs.zip (4KB)
MagicHeadLink + Minify (trimmed):
mhl.zip (8 KB)
Комментарии
Отправить комментарий