2011-01-06 5 views
7

क्या ज़ेन फ्रेमवर्क के साथ Minify एकीकरण का कोई अच्छा कार्यान्वयन है? मैं उदाहरणों की तलाश में हूं।कोई भी अच्छा ज़ेंड फ्रेमवर्क + कार्यान्वयन को कम करें?

मुझे एक प्लगइन होना पसंद है जो $-> headLink() को ओवरराइड करता है और सही न्यूनतम यूआरएल/सामग्री को थूकता है।

संपादित करें:

यह सबसे उदाहरण मैं पूरी तरह से एक या फैशन में अनुकूलित नहीं कर रहे हैं लगता है। मैं एक समाधान की तलाश कर रहा हूं जो निम्नलिखित आवश्यकताओं को पूरा करता है:

एक अनुरोध में एकाधिक लिंक और स्क्रिप्ट टैग को कम करता है (लिंक के लिए एक और स्क्रिप्ट के लिए एक) मैंने जो नज़दीकी देखा है वह एक अनुरोध पथ है जो अल्पविराम से गुज़रता है -delimited स्ट्रिंग/मिनट के लिए/इसलिए की तरह:

<script src="/min?f=file1.js,file2,js,file3.js" type="text/javascript"></script> 

नहीं क्यों कुछ है कि मक्खी पर डिस्क पर एक फ़ाइल में सभी स्क्रिप्ट को जोड़ती है और फिर इसे कैश ताकि आप हर अनुरोध पर minification नहीं कर रहे हैं?

<script src="/js/app.js?someRandomStringHere" type="text/javascript"></script> 

संयोजन पहलू आदेश (संदर्भ में, पहले जोड़ें संलग्न, आदि के लिए)

मैं सही भेजने के बारे में इतना परवाह नहीं है जबकि बनाए रखना चाहिए हेडर समाप्त हो रहा है, क्योंकि मैं gzipping, etags मजबूर, और हेडर की समय सीमा समाप्त सर्वर-साइड पर, वह वैकल्पिक होने से अन्य उपयोगकर्ताओं के लिए फायदेमंद होगा।

आखिरकार, एक निर्मित स्क्रिप्ट जो खनिज संपत्ति उत्पन्न करती है, वह आवश्यक नहीं है - जब तक यह करना आसान हो और प्रत्येक निर्माण के बाद कोड परिवर्तन की आवश्यकता न हो।

+0

मैं, अब इस पर काम कर रहा हूँ इस fav'd और आप –

+0

किसी भी प्रगति यहाँ @jakenoble तैनात रखेंगे: उनमें से एक के लिए त्वरित अवलोकन? – doremi

+0

उत्तर अब पोस्ट किया गया –

उत्तर

0

mmm, माफ करना, मैं उदाहरण नहीं है, लेकिन मैं तुम्हें बताया गया हो कि मदद कर सकते हैं,

मेरी सरल कार्यप्रवाह इस तरह होगा:

1- अपने कस्टम पुस्तकालय में एक दृश्य के सहायक के रूप में फ़ोल्डर, एक वर्ग है कि my_minify::minify() के स्थिर समारोह फैली बनाने यदि आप एक viewhelper कि दोनों headLink() और minfy वर्ग

2 की कार्यक्षमता को ओवरराइड एक प्लगइन के रूप में बना सकते हैं : आप पूरे परिणामस्वरूप दृश्य को कम करने के लिए postdispatch चला सकते हैं, explanation

2

मैं अभी भी वही काम करने की कोशिश कर रहा हूं। मैं ज़ेंड फ्रेमवर्क के आधार पर एनसी स्टेट यूनिवर्सिटी के ओटी फ्रेमवर्क को देख रहा हूं। इसे एक दृश्य सहायक के रूप में लागू किया गया है।

http://ot.ncsu.edu/2010/03/03/getting-started-with-ot-framework/

Headscripts:

<?php 

/** 
* Minifies the javascript files added via the minifyHeadScript helper using 
* minify (http://code.google.com/p/minify/) 
* 
*/ 
class Ot_View_Helper_MinifyHeadScript extends Zend_View_Helper_HeadScript 
{ 

    protected $_regKey = 'Ot_View_Helper_MinifyHeadScript'; 

    public function minifyHeadScript($mode = Zend_View_Helper_HeadScript::FILE, $spec = null, $placement = 'APPEND', array $attrs = array(), $type = 'text/javascript') 
    { 
     return parent::headScript($mode, $spec, $placement, $attrs, $type); 
    } 

    public function toString() 
    { 
     $items = array(); 
     $scripts = array(); 
     $baseUrl = $this->getBaseUrl(); 

     // we can only support files 
     foreach ($this as $item) { 
      if (isset($item->attributes['src']) && !empty($item->attributes['src'])) { 
       $scripts[] = str_replace($baseUrl, '', $item->attributes['src']); 
      } 
     } 

     //remove the slash at the beginning if there is one 
     if (substr($baseUrl, 0, 1) == '/') { 
      $baseUrl = substr($baseUrl, 1); 
     } 

     $item = new stdClass(); 
     $item->type = 'text/javascript'; 
     $item->attributes['src'] = $this->getMinUrl() . '?b=' . $baseUrl . '&f=' . implode(',', $scripts); 
     $scriptTag = $this->itemToString($item, '', '', ''); 

     return $scriptTag; 
    } 

    public function getMinUrl() { 
     return $this->getBaseUrl() . '/min/'; 
    } 

    public function getBaseUrl(){ 
     return Zend_Controller_Front::getInstance()->getBaseUrl(); 
    } 
} 

और यहाँ headlinks के लिए कोड है:

<?php 

/** 
* Minifies the stylesheets added via the minifyHeadLink helper using 
* minify (http://code.google.com/p/minify/) 
* 
*/ 
class Ot_View_Helper_MinifyHeadLink extends Zend_View_Helper_HeadLink 
{ 

    protected $_regKey = 'Ot_View_Helper_MinifyHeadLink'; 

    public function minifyHeadLink(array $attributes = null, $placement = Zend_View_Helper_Placeholder_Container_Abstract::APPEND) 
    { 
     return parent::headlink($attributes, $placement); 
    } 

    public function toString() 
     { 
     $items = array(); 
     $stylesheets = array(); 
     $baseUrl = $this->getBaseUrl(); 

     foreach ($this as $item) { 
      if ($item->type == 'text/css' && $item->conditionalStylesheet === false) { 
       $stylesheets[$item->media][] = str_replace($baseUrl, '', $item->href); 
      } else { 
       $items[] = $this->itemToString($item); 
      } 
     } 

     //remove the slash at the beginning if there is one 
     if (substr($baseUrl, 0, 1) == '/') { 
      $baseUrl = substr($baseUrl, 1); 
     } 

     foreach ($stylesheets as $media=>$styles) { 
      $item = new stdClass(); 
      $item->rel = 'stylesheet'; 
      $item->type = 'text/css'; 
      $item->href = $this->getMinUrl() . '?b=' . $baseUrl . '&f=' . implode(',', $styles); 
      $item->media = $media; 
      $item->conditionalStylesheet = false; 
      $items[] = $this->itemToString($item); 
     } 

     $link = implode($this->_escape($this->getSeparator()), $items); 

     return $link; 
    } 

    public function getMinUrl() { 
     return $this->getBaseUrl() . '/min/'; 
    } 

    public function getBaseUrl(){ 
     return Zend_Controller_Front::getInstance()->getBaseUrl(); 
    } 
} 
3
यह कम करें के माध्यम से सभी headscripts और headlinks कम करें करने के लिए Google Code पर एक अच्छा वर्ग है

मैं यही उपयोग करता हूं, कक्षा का उपयोग मामलों के बाद दिखाया जाता है।मैं इसे जल्दी से टिप्पणी की है, इसके बारे में कुछ अपने पथ से मेल करने को बदलने की आवश्यकता हो सकती है या define()PUBLICPATH

class View_Helper_Minify extends Zend_View_Helper_Abstract 
{ 
    public function minify($files, $ext, $folderName) 
    { 
     // The folder of the files your about to minify 
     // PUBLICPATH should be the path to your public ZF folder 
     $folder = PUBLICPATH . $folderName . "/"; 

     // Set update needed flag to false 
     $update_needed = false; 

     // This is the file ext of the cached files 
     $cacheFileExt = "." . $ext; 

     // The list of files sent is exploded into an array 
     $filesExploded = explode(',', $files); 

     // The full cached file path is an md5 of the files string 
     $cacheFilePath = $folder . md5($files) . $cacheFileExt; 

     // The filename of the cached file 
     $cacheFileName = preg_replace("#[^a-zA-Z0-9\.]#", "", end(explode("/", $cacheFilePath))); 

     // Obtains the modified time of the cache file 
     $cacheFileDate = is_file($cacheFilePath) ? filemtime($cacheFilePath) : 0; 

     // Create new array for storing the list of valid files 
     $fileList = array(); 

     // For each file 
     foreach($filesExploded as $f) 
     { 
      // determine full path of the full and append extension 
      $f = $folder . $f . '.' . $ext; 

      // If the determined path is a file 
      if(is_file($f)) 
      { 
       // If the file's modified time is after the cached file's modified time 
       // Then an update of the cached file is needed 
       if(filemtime($f) > $cacheFileDate) 
        $update_needed = true; 

       // File is valid add to list 
       $fileList[] = $f; 
      } 
     } 

     // If the cache folder's modified time is after the cached file's modified time 
     // Then an update is needed 
     if(filemtime($folder) > $cacheFileDate) 
      $update_needed = true; 

     // If an update is needed then optmise the valid files 
     if($update_needed) 
      $this->optmiseFiles($fileList, $cacheFilePath, $ext); 

     // Finally check if the cached file path is valid and return the absolute URL 
     // for te cached file 
     if(is_file($cacheFilePath)) 
      return "/" . $folderName . "/" . $cacheFileName; 

     // Throw Exception 
     throw new Exception("No minified file cached");    
    } 

    private function optimise($code, $ext) 
    { 
     // Do not optmise JS files 
     // I had problems getting JS files optmised and still function 
     if($ext == "js") 
      return $code; 

     // Remove comments from CSS 
     while(($i = strpos($code, '/*')) !== false) 
     { 
      $i2 = strpos($code, '*/',$i); 

      if($i2 === false) 
       break; 

      $code = substr($code, 0, $i).substr($code, $i2 + 2); 
     } 

     // Remove other elements from CSS 
     $code = str_replace('/*','',$code); 
     $code = str_replace("\n",' ',$code); 
     $code = str_replace("\r",' ',$code); 
     $code = str_replace("\t",' ',$code); 
     $code = @ereg_replace('[ ]+',' ',$code); 
     $code = str_replace(': ',':', $code); 
     $code = str_replace('; ',';', $code); 
     $code = str_replace(', ',',', $code); 
     $code = str_replace(' :',':', $code); 
     $code = str_replace(' ;',';', $code); 
     $code = str_replace(' ,',',', $code); 

     // Return optimised code 
     return $code; 
    } 

    // Optmise the list of files 
    private function optmiseFiles($fileList, $cacheFilePath, $ext) 
    { 
     // Empty String to start with 
     $code = ''; 

     // Check files list in case just one file was passed 
     if(is_array($fileList)) 
     { 
      // Foreach of the valid files optmise the code if the file is valid 
      foreach($fileList as $f) 
       $code .= is_file($f) ? $this->optimise(implode('', file($f)), $ext) : ''; 
     } 
     // Else if a valid file is passed optmise the code 
     else 
      $code = is_file($fileList) ? $this->optimise(implode('', file($fileList)), $ext) : ''; 

     // Open the cache file 
     $f = @fopen($cacheFilePath, 'w'); 

     // If open successful 
     if(is_resource($f)) 
     { 
      // Write code to the cache file 
      fwrite($f, $code); 

      // close cache file 
      fclose($f); 
     } 
    } 
} 

आप अपने दृश्य

// Define an array of files, note you do not define the ext of those files 
// The ext is defined as a param for the helper as this effects the optmisation 
$files = array("jquery-ui-1.8.7.custom", 
      "jquery.pnotify.default", 
      "jquery.pnotify.default.icons", 
      "tipTip", 
      "prettyPhoto", 
      "custom"); 

// Get the absolute URL of the files which are imploded also pass the directory 'css' and ext 'css' 
$cssString = $this->minify(implode("," , $files), "css", "css"); 

// use baseURL() to output the full URL of the cached file and use it as normal with headLink() 
echo $this->headLink() 
->appendStylesheet($this->baseUrl($cssString)); 

और यहाँ में इस तरह सहायक का प्रयोग करेंगे एक जावास्क्रिप्ट है संस्करण

$files = array("jquery-1.4.4.min", 
      "jquery.pnotify.min", 
      "jquery.tipTip.minified", 
      "jquery.countdown.min", 
      "jquery.prettyPhoto", 
      "jquery.typewatch", 
      "default.functions"); 

$jsString = $this->minify(implode("," , $files), "js", "scripts"); 

echo $this->headScript()->appendFile($this->baseUrl($jsString)); 
+0

यह काम करता है, लेकिन वास्तव में भ्रमित? मुझे स्पष्टीकरण की आवश्यकता है – Nandakumar

+0

क्या समझाया जाना चाहिए - इसकी सभी टिप्पणी की गई? –

2

मैं एक ही समस्या में भाग गया और मेरे लिए इसे प्रबंधित करने के लिए दो ड्रॉप-इन सहायकों को लिखना समाप्त कर दिया। आप उन्हें http://blog.hines57.com/2011/03/13/zendframework-minify/ पर देख सकते हैं - धन्यवाद।

* * ** PREREQUISITES ** 
* This file expects that you have installed minify in ../ZendFramworkProject/Public/min 
* and that it is working. If your location has changed, modify 
* $this->$_minifyLocation to your current location. 
* 
* ** INSTALLATION ** 
* Simply drop this file into your ../ZendFramworkProject/application/views/helpers 
* directory. 
* 
* ** USAGE ** 
* In your Layout or View scripts, you can simply call minifyHeadLink 
* in the same way that you used to call headLink. Here is an example: 
* 
    echo $this->minifyHeadLink('/favicon.ico')    // Whatever was already loaded from Controller. 
    ->prependStylesheet('http://example.com/js/sample.css')// 6th 
    ->prependStylesheet('/js/jqModal.css')     // 5th 
    ->prependStylesheet('/js/jquery.alerts.css')   // 4th 
    ->prependStylesheet('/templates/main.css')    // 3rd 
    ->prependStylesheet('/css/project.css.php')   // 2nd 
    ->prependStylesheet('/css/jquery.autocomplete.css') // 1st 
    ->appendStylesheet('/css/ie6.css','screen','lt IE 7'); // APPEND to make it Last 
* 
* 
* This can be interesting because you will notice that 2nd is a php file, and we 
* have a reference to a favicon link in there as well as a reference to a css file on 
* another website. Because minify can't do anything with that php file (runtime configured 
* css file) nor with CSS on other websites, and order is important,you would notice that 
* the output in your browser will looks something like: 
* 
    <link href="/min/?f=/css/jquery.autocomplete.css" media="screen" rel="stylesheet" type="text/css" /> 
    <link href="/css/project.css.php" media="screen" rel="stylesheet" type="text/css" /> 
    <link href="/min/?f=/templates/main.css,/js/jquery.alerts.css,/js/jqModal.css" media="screen" 
       rel="stylesheet" type="text/css" /> 
    <link href="http://example.com/js/sample.css" media="screen" rel="stylesheet" type="text/css" /> 
    <link href="/favicon.ico" rel="shortcut icon" /> 
    <!--[if lt IE 7]> <link href="/css/ie6.css" media="screen" rel="stylesheet" type="text/css" /><![endif]--> 
+0

"एक अनुरोध में एकाधिक लिंक और स्क्रिप्ट टैग को कम करता है (लिंक के लिए एक और स्क्रिप्ट के लिए एक) सबसे नज़दीक मैंने देखा है एक अनुरोध पथ है जो अल्पविराम-सीमित स्ट्रिंग को/मिनट/इस तरह से पास करता है:" - वास्तव में यह आपके लिए संभालती है। यह पहले अनुरोध पर डिस्क पर फ़ाइल बनाता है और कैश करता है, और उस कॉमा सीमांकित स्ट्रिंग के बाद के सभी अनुरोध केवल कैश किए गए संस्करण की सेवा करते हैं, क्योंकि वास्तव में प्रत्येक कॉल पर इसे "पुनः" खनन करने के विरोध में किया जाता है। – bubba

संबंधित मुद्दे