Esempio n. 1
0
 public static function run($bootstrap)
 {
     ZFE_Util_Stopwatch::start('page');
     self::$bootstrap = $bootstrap;
     $methods = get_class_methods(get_called_class());
     foreach ($methods as $method) {
         if (strpos($method, "_init") === 0) {
             self::$method();
         }
     }
 }
Esempio n. 2
0
 public function testTrigger()
 {
     ZFE_Util_Stopwatch::trigger('trigger');
     $timing = ZFE_Util_Stopwatch::get('trigger');
     $this->assertArrayHasKey('start', $timing);
     $this->assertCount(1, $timing);
     ZFE_Util_Stopwatch::trigger('trigger');
     $timing = ZFE_Util_Stopwatch::get('trigger');
     $this->assertArrayHasKey('start', $timing);
     $this->assertArrayHasKey('stop', $timing);
     $this->assertArrayHasKey('duration', $timing);
     $this->assertCount(3, $timing);
     ZFE_Util_Stopwatch::trigger('trigger');
     $this->assertNull(ZFE_Util_Stopwatch::get('trigger'));
 }
Esempio n. 3
0
 public function toString($indent = null)
 {
     // Return early if no minifier has been set up
     if (false === $this->_minifier || !$this->_minifier->doBundle()) {
         return parent::toString($indent);
     }
     ZFE_Util_Stopwatch::trigger(__METHOD__);
     $container = $this->getContainer();
     $container->ksort();
     $items = $container->getArrayCopy();
     // Collect CSS files
     $compressable = array();
     foreach ($items as $key => $item) {
         if ($item->rel == 'stylesheet' && $item->type == 'text/css' && !$item->conditionalStylesheet) {
             $file = basename($item->href);
             if (in_array($file, $this->doNotBundle)) {
                 continue;
             }
             if (ZFE_Util_String::startsWith($item->href, "http://")) {
                 continue;
             }
             if (ZFE_Util_String::startsWith($item->href, "//")) {
                 continue;
             }
             if (!isset($compressable[$item->media])) {
                 $compressable[$item->media] = array();
             }
             $compressable[$item->media][] = $item;
             unset($items[$key]);
         }
     }
     $container->exchangeArray($items);
     // Collect current data of the CSS files
     $hashes = array();
     $mtimes = array();
     foreach ($compressable as $media => $items) {
         $hash = '';
         $_mtimes = array();
         foreach ($items as $item) {
             $file = $_SERVER['DOCUMENT_ROOT'] . $item->href;
             if (Zend_Loader::isReadable($file)) {
                 $hash .= ':' . $item->href;
                 $_mtimes[] = filemtime($file);
             }
         }
         $hashes[$media] = $hash;
         $mtimes[$media] = $_mtimes;
     }
     // Check if the original CSS files have been updated since the
     // last minification
     foreach ($hashes as $media => $hash) {
         $regenerate = true;
         $filename = sha1($media . $hash) . ".css";
         $cachedir = $this->_minifier->getCacheDir();
         $path = $_SERVER['DOCUMENT_ROOT'] . $cachedir;
         // check directory exists. if not, create it
         if (!file_exists($path)) {
             mkdir($path, 0775, true);
         }
         if (file_exists($path . "/" . $filename)) {
             $mtime = filemtime($path . "/" . $filename);
             $regenerate = array_reduce($mtimes[$media], function ($u, $v) use($mtime) {
                 return $u || $v > $mtime;
             }, false);
         }
         // If any CSS file in this media group has been updated since the last
         // minification, collect the content again, and store it in the cached version
         if ($regenerate) {
             $cssContent = '';
             foreach ($compressable[$media] as $item) {
                 $file = $_SERVER['DOCUMENT_ROOT'] . $item->href;
                 if (Zend_Loader::isReadable($file)) {
                     $cssContent .= file_get_contents($file) . PHP_EOL;
                 }
             }
             $cssContent = $this->_minifier->minify($cssContent);
             file_put_contents($path . "/" . $filename, $cssContent);
         }
         $this->appendStylesheet($cachedir . "/" . $filename, $media);
     }
     ZFE_Util_Stopwatch::trigger(__METHOD__);
     return parent::toString($indent);
 }
Esempio n. 4
0
 /**
  * dump
  *
  * Dumps the results on screen
  * @codeCoverageIgnore
  */
 public static function dump()
 {
     // Clears incomplete timings
     self::$timings = array_filter(self::$timings, function ($t) {
         return isset($t['duration']);
     });
     // Sort the results on duration, in descending order
     uasort(self::$timings, function ($a, $b) {
         if ($a['duration'] == $b['duration']) {
             return 0;
         }
         return $a['duration'] > $b['duration'] ? -1 : 1;
     });
     echo "<pre style=\"clear: both;\">" . print_r(self::$timings, true) . "</pre>";
     if (count(self::$cumulative)) {
         $prev = array();
         echo "<pre style=\"clear: both;\">";
         foreach (self::$cumulative as $i => $data) {
             echo "[" . $i . "] (" . $data['ts'] . ") " . $data['label'] . ": " . ($i == 0 ? 0 : $data['ts'] - $prev['ts']) . "s" . PHP_EOL;
             $prev = $data;
         }
         echo "</pre>";
     }
     echo "</pre>";
 }