Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Method not implemented.
- */
- class NotImplementedException extends BadMethodCallException {}
- /**
- * Resource was not found on the filesystem.
- */
- class ResourceNotFoundException extends Exception {}
- /**
- * Resource interface. Vsi resoursi (JS, CSS) ga morajo implementirati.
- * (in ga, posredno preko BaseResource razreda).
- */
- interface Resource {
- static function printResources();
- }
- /**
- * BaseResource class. Opravlja delo, skupno vsem resource classom.
- * Recimo, dodajanje resoursa preko URL, dodajanje resoursa preko aliasa,
- * kompresija, etc.
- * BaseResource sam po sebi ni uporaben, torej je nesmiselno klicati
- * recimo BaseResource::add() itd., ker se bo BaseResource::print() sesu.
- */
- class BaseResource implements Resource {
- /**
- * @var $RESOURCE_DIR Root direktorij z resoursi.
- */
- protected static $RESOURCE_DIR = "/keje/keje";
- /**
- * @var $resources Seznam resoursov.
- */
- protected static $resources = array();
- /**
- * Dodajanje resoursa.
- *
- * @param string $url URL resoursa ali njegov veljavni alias.
- * @return void
- */
- public static function add($url) {
- if (isset(static::$aliases[$url])) {
- $url = static::$aliases[$url];
- }
- static::$resources[] = $url;
- }
- /**
- * Racunanje poti datoteke, ki vsebuje trenutne resurse.
- *
- * @return string
- */
- protected static function computeHashFileName() {
- $sortedResources = sort(static::$resources, SORT_STRING);
- $hash = md5(join(",", $sortedResources));
- $fileName = sprintf("%s/%s/%s.%s", static::$RESOURCE_DIR, static::$extension, $hash, static::$extension);
- return $fileName;
- }
- /**
- * Zdruzevanje vseh resursov (ki niso http) v eno datoteko.
- *
- * @throws ResourceNotFoundException Ce resurs ne obstaja.
- * @return string
- */
- public static function compress() {
- $fileName = static::computeHashFileName();
- $contents = "";
- foreach (static::$resources as $res) {
- if (substr($res, 0, 8) != "http://") {
- $resourceFileName = sprintf("%s/%s/%s", static::$RESOURCE_DIR, static::$extension, $res);
- if (!file_exists($resourceFileName))
- throw new ResourceNotFoundException(sprintf("Resource file for <%s> not found (looking for <%s>).", $res, $resourceFileName));
- $content .= sprintf("%s\n", file_get_contents($resourceFileName));
- }
- }
- file_put_contents($fileName, $content);
- }
- /**
- * Generiranje HTML includov za kompresirane resurse (ena datoteka).
- * Ce datoteka ne obstaja, jo zgeneriramo (lahko casovno potratno).
- *
- * @throws ResourceNotFoundException Ce kateri izmed resursov med kompresiranjem ne obstaja.
- * @return string
- */
- public static function printCompressed() {
- $hashFileName = static::computeHashFileName();
- if (!file_exists($hashFileName)) {
- static::compress();
- }
- $tmp = static::$resources;
- static::$resources = array($hashFileName);
- $out = static::printResources();
- static::$resources = $tmp;
- return $out;
- }
- /**
- * Izpise vse HTML include za resurse.
- *
- * @return string
- */
- public static function print() {
- static::printResources();
- }
- /**
- * Metoda, ki jo podrazred mora povoziti. Izpise include tage za vse resurse.
- * @throws NotImplementedException
- */
- static function printResources() {
- throw new NotImplementedException("Calling: you're doing it wrong.");
- }
- }
- /**
- * Resource class za vse JavaScript datoteke.
- */
- class JS extends BaseResource {
- /**
- * @var $extension
- */
- protected static $extension = "js";
- /**
- * @var $aliases Aliasi JavaScriptov.
- */
- protected static $aliases = [
- "jquery" => "http://jquerasldsDSADASDAS.COM",
- "jquery-ui-custom" => "/scripts/jquery.dscadsa"
- ];
- /**
- * Izpise <script> tage za vse JavaScripte.
- *
- * @return string
- */
- public static function printResources() {
- $out = "";
- foreach (static::$resources as $script) {
- $out .= sprintf("<script type=\"text/javascript\" src=\"%s\"></script>\n", $script);
- }
- return $out;
- }
- }
- /**
- * Resource class za vse CSS datoteke.
- */
- class CSS extends BaseResource {
- /**
- * @var $extension
- */
- protected static $extension = "css";
- /**
- * @var $aliases Aliasi CSS-jev.
- */
- protected static $aliases = [
- "dashboard-main" => "http://jquerasldsDSADASDAS.COM",
- "jquery-ui-custom" => "/scripts/jquery.dscadsa"
- ];
- /**
- * Izpise <link> tage za vse CSS-je.
- *
- * @return string
- */
- public static function printResources() {
- $out = "";
- foreach (static::$resources as $script) {
- $out .= sprintf("<link rel=\"stylesheet\" href=\"%s\"></script>\n", $script);
- }
- return $out;
- }
- }
- // Primer uporabe.
- JS::add('jquery');
- JS::add('jquery-ui');
- echo JS::print();
- try {
- echo JS::printCompressed();
- } catch (ResourceNotFoundException $e) {
- // $logger->report(.......); ... pac, logaj.
- echo JS::print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement