Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.86 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Method not implemented.
  5.  */
  6. class NotImplementedException extends BadMethodCallException {}
  7.  
  8. /**
  9.  * Resource was not found on the filesystem.
  10.  */
  11. class ResourceNotFoundException extends Exception {}
  12.  
  13. /**
  14.  * Resource interface. Vsi resoursi (JS, CSS) ga morajo implementirati.
  15.  * (in ga, posredno preko BaseResource razreda).
  16.  */
  17. interface Resource {
  18.     static function printResources();
  19. }
  20.  
  21. /**
  22.  * BaseResource class. Opravlja delo, skupno vsem resource classom.
  23.  * Recimo, dodajanje resoursa preko URL, dodajanje resoursa preko aliasa,
  24.  * kompresija, etc.
  25.  * BaseResource sam po sebi ni uporaben, torej je nesmiselno klicati
  26.  * recimo BaseResource::add() itd., ker se bo BaseResource::print() sesu.
  27.  */
  28. class BaseResource implements Resource {
  29.  
  30.     /**
  31.      * @var $RESOURCE_DIR Root direktorij z resoursi.
  32.      */
  33.     protected static $RESOURCE_DIR = "/keje/keje";
  34.    
  35.     /**
  36.      * @var $resources Seznam resoursov.
  37.      */
  38.     protected static $resources = array();
  39.  
  40.     /**
  41.      * Dodajanje resoursa.
  42.      *
  43.      * @param string $url  URL resoursa ali njegov veljavni alias.
  44.      * @return void
  45.      */
  46.     public static function add($url) {
  47.         if (isset(static::$aliases[$url])) {
  48.             $url = static::$aliases[$url];
  49.         }
  50.  
  51.         static::$resources[] = $url;
  52.     }
  53.  
  54.     /**
  55.      * Racunanje poti datoteke, ki vsebuje trenutne resurse.
  56.      *
  57.      * @return string
  58.      */
  59.     protected static function computeHashFileName() {
  60.         $sortedResources = sort(static::$resources, SORT_STRING);
  61.         $hash = md5(join(",", $sortedResources));
  62.         $fileName = sprintf("%s/%s/%s.%s", static::$RESOURCE_DIR, static::$extension, $hash, static::$extension);
  63.         return $fileName;
  64.     }
  65.  
  66.     /**
  67.      * Zdruzevanje vseh resursov (ki niso http) v eno datoteko.
  68.      *
  69.      * @throws ResourceNotFoundException Ce resurs ne obstaja.
  70.      * @return string
  71.      */
  72.     public static function compress() {
  73.         $fileName = static::computeHashFileName();
  74.         $contents = "";
  75.         foreach (static::$resources as $res) {
  76.             if (substr($res, 0, 8) != "http://") {
  77.                 $resourceFileName = sprintf("%s/%s/%s", static::$RESOURCE_DIR, static::$extension, $res);
  78.                 if (!file_exists($resourceFileName))
  79.                     throw new ResourceNotFoundException(sprintf("Resource file for <%s> not found (looking for <%s>).",  $res, $resourceFileName));
  80.                 $content .= sprintf("%s\n", file_get_contents($resourceFileName));
  81.             }
  82.         }      
  83.  
  84.         file_put_contents($fileName, $content);
  85.     }
  86.  
  87.     /**
  88.      * Generiranje HTML includov za kompresirane resurse (ena datoteka).
  89.      * Ce datoteka ne obstaja, jo zgeneriramo (lahko casovno potratno).
  90.      *
  91.      * @throws ResourceNotFoundException Ce kateri izmed resursov med kompresiranjem ne obstaja.
  92.      * @return string
  93.      */
  94.     public static function printCompressed() {
  95.         $hashFileName = static::computeHashFileName(); 
  96.         if (!file_exists($hashFileName)) {
  97.             static::compress();    
  98.         }
  99.  
  100.         $tmp = static::$resources;
  101.         static::$resources = array($hashFileName);
  102.         $out = static::printResources();
  103.         static::$resources = $tmp;
  104.         return $out;
  105.     }
  106.    
  107.     /**
  108.      * Izpise vse HTML include za resurse.
  109.      *
  110.      * @return string
  111.      */
  112.     public static function print() {
  113.         static::printResources();
  114.     }
  115.  
  116.     /**
  117.      * Metoda, ki jo podrazred mora povoziti. Izpise include tage za vse resurse.
  118.      * @throws NotImplementedException
  119.      */
  120.     static function printResources() {
  121.         throw new NotImplementedException("Calling: you're doing it wrong.");
  122.     }
  123. }
  124.  
  125. /**
  126.  * Resource class za vse JavaScript datoteke.
  127.  */
  128. class JS extends BaseResource {
  129.  
  130.     /**
  131.      * @var $extension
  132.      */
  133.     protected static $extension = "js";
  134.  
  135.     /**
  136.      * @var $aliases Aliasi JavaScriptov.
  137.      */
  138.     protected static $aliases = [
  139.         "jquery" => "http://jquerasldsDSADASDAS.COM",
  140.         "jquery-ui-custom" => "/scripts/jquery.dscadsa"
  141.     ];
  142.  
  143.     /**
  144.      * Izpise <script> tage za vse JavaScripte.
  145.      *
  146.      * @return string
  147.      */
  148.     public static function printResources() {
  149.         $out = "";
  150.         foreach (static::$resources as $script) {
  151.             $out .= sprintf("<script type=\"text/javascript\" src=\"%s\"></script>\n", $script);
  152.         }
  153.         return $out;
  154.     }
  155. }
  156.  
  157. /**
  158.  * Resource class za vse CSS datoteke.
  159.  */
  160. class CSS extends BaseResource {
  161.  
  162.     /**
  163.      * @var $extension
  164.      */
  165.     protected static $extension = "css";
  166.  
  167.     /**
  168.      * @var $aliases Aliasi CSS-jev.
  169.      */
  170.     protected static $aliases = [
  171.         "dashboard-main" => "http://jquerasldsDSADASDAS.COM",
  172.         "jquery-ui-custom" => "/scripts/jquery.dscadsa"
  173.     ];
  174.  
  175.     /**
  176.      * Izpise <link> tage za vse CSS-je.
  177.      *
  178.      * @return string
  179.      */
  180.     public static function printResources() {
  181.         $out = "";
  182.         foreach (static::$resources as $script) {
  183.             $out .= sprintf("<link rel=\"stylesheet\" href=\"%s\"></script>\n", $script);
  184.         }
  185.         return $out;
  186.     }
  187. }
  188.  
  189.  
  190.  
  191. // Primer uporabe.
  192.  
  193. JS::add('jquery');
  194. JS::add('jquery-ui');
  195.  
  196. echo JS::print();
  197.  
  198. try {
  199.     echo JS::printCompressed();
  200. } catch (ResourceNotFoundException $e) {
  201.     // $logger->report(.......); ... pac, logaj.
  202.     echo JS::print();
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement