diff --git a/example/index.php b/example/index.php index 69f3ad8..113b415 100644 --- a/example/index.php +++ b/example/index.php @@ -15,14 +15,13 @@ ob_end_clean(); // add() can be used in any sub-template before main layout rendering -LESS::connect('/lessnichy-app')->add( - [ +LESS::connect('/lessnichy-app', false)->add( + array( '/less/foo.less' - ] + ) ); ?> - @@ -31,18 +30,20 @@ '/js/less-1.7.0.min.js', + array( + // LESS::JS => '/js/less-1.7.0.min.js', // customize less.js lib LESS::DEBUG => true, LESS::WATCH_INTERVAL => 5000, - ] + ) ); ?> +
.foo > .bar text
- \ No newline at end of file + + \ No newline at end of file diff --git a/example/lessnichy-app/index.php b/example/lessnichy-app/index.php index 9261eca..def8e15 100644 --- a/example/lessnichy-app/index.php +++ b/example/lessnichy-app/index.php @@ -1,8 +1,8 @@ baseUrl = $baseUrl; + //todo ability to sitch less mode on-the-fly from browser + if ($lessMode instanceof Closure) { + $this->lessModeTrigger = $lessMode; + } else { + $this->enableLessMode = (bool) $lessMode; + } } - public function add(array $lessStylesheets = []) + /** + * @param string[] $lessStylesheets full urls to .less files + */ + public function add(array $lessStylesheets = array()) { foreach ($lessStylesheets as $less) { $this->stylesheets[] = $less; @@ -28,54 +64,92 @@ public function add(array $lessStylesheets = []) register_shutdown_function( function () use ($client, $baseurl) { // when Lessnichy used, register client watching libs and resources - if ($client->isHeadPrinted()) { + if ($client->isHeadPrinted() && $client->inLessMode()) { + //todo link to gzipped glued js + print "\n"; print ""; } } ); } - public function head(array $options = [] /* todo optional stream handler besides stdout*/) + /** + * @param array $extraOptions use LESS::* constants + */ + public function head(array $extraOptions = array() /* todo optional stream handler besides stdout*/) { if (empty($this->stylesheets)) { throw new \LogicException("Add some stylesheets first"); } - if (isset($options[Lessnichy::JS])) { - $lessJsUrl = $options[Lessnichy::JS]; - unset($options[Lessnichy::JS]); + if ($this->inLessMode()) { + $this->printLessStylesheets($extraOptions); } else { - $lessJsUrl = false; + $this->printCssStylesheets($extraOptions); } - $jsonDefaults = [Lessnichy::WATCH_INTERVAL => 2500, Lessnichy::DEBUG => true]; - $optionsMerged = array_merge($jsonDefaults, $options); - $json = json_encode($optionsMerged); + $this->headPrinted = true; + } + + /** + * @return boolean + */ + public function isHeadPrinted() + { + return $this->headPrinted; + } + + /** + * @param array $extraOptions + */ + private function printLessStylesheets(array $extraOptions) + { + if (isset($extraOptions[ Lessnichy::JS ])) { + $lessJsUrl = $extraOptions[ Lessnichy::JS ]; + unset($extraOptions[ Lessnichy::JS ]); + } else { + $lessJsUrl = $this->baseUrl . '/js/less-1.7.0.min.js'; + } - $optionsMerged['lessnichy'] = [ - 'url' => $this->baseUrl - ]; + $lessJsOptions = array(Lessnichy::WATCH_INTERVAL => 2500, Lessnichy::DEBUG => true); + $lessJsOptions = array_merge($lessJsOptions, $extraOptions); + $lessJsOptions['lessnichy'] = array( + 'url' => $this->baseUrl . '/css' + ); // setcookie('Lessnichy.') - print "\n"; + print "\n"; - foreach ($this->stylesheets as $url) { - print "\n"; + foreach ($this->stylesheets as $lessStylesheetUrl) { + print "\n"; } // if no url provided, assume that less.js connected manually if ($lessJsUrl) { print "\n"; } + } - $this->headPrinted = true; + /** + * @see $enableLessMode + * @return bool + */ + private function inLessMode() + { + if (($trigger = $this->lessModeTrigger) instanceof Closure) { + return $trigger(); + } + return $this->enableLessMode; } /** - * @return boolean + * Outputs tags to compiled css + * @param array $extraOptions */ - public function isHeadPrinted() + private function printCssStylesheets(array $extraOptions = array()) { - return $this->headPrinted; + foreach ($this->stylesheets as $LessStylesheetUrl) { + print ""; + } } } \ No newline at end of file diff --git a/lib/Lessnichy.php b/lib/Lessnichy.php index f105fcc..9a6e32e 100644 --- a/lib/Lessnichy.php +++ b/lib/Lessnichy.php @@ -1,5 +1,7 @@ add($lessStylesheets); @@ -50,14 +53,14 @@ public static function add($lessStylesheets) /** * @see LessnichyClient::head() */ - public static function head(array $options = [] /* todo optional stream handler besides stdout*/) + public static function head(array $options = array() /* todo optional stream handler besides stdout*/) { self::ensureClient(); return self::$client->head($options); } /** - * + * Check that API client is started up */ private static function ensureClient() { @@ -67,10 +70,12 @@ private static function ensureClient() } /** + * Catch http requests in current folder * @return Server */ public static function listen() { + //todo auto-bootstrap .htaccess if (is_null(self::$server)) { self::$server = new Server(); } diff --git a/lib/app.php b/lib/app.php index b8c9129..21ec20e 100644 --- a/lib/app.php +++ b/lib/app.php @@ -1,6 +1,8 @@ put( - '/css/:file', - function ($file) use ($app) { +$app->post( + '/css/', + function () use ($app) { + $sheets = $app->request->post('sheets', array()); + $results = array(); + foreach ($sheets as $lessStylesheetUrl => $cssContent) { + $parts = parse_url($lessStylesheetUrl); + + $cssStylesheetFilename = $_SERVER['DOCUMENT_ROOT'] . $parts['path'] . '.css'; + file_put_contents($cssStylesheetFilename, $cssContent); + $results[$lessStylesheetUrl] = $cssStylesheetFilename; + } + //todo minify - file_put_contents($file, $app->request->put('contents')); + print json_encode($results); } ); $app->get( '/js/:file', function ($file) { + //todo glue and gzip lessnichy.js, clean-css.js, less***.js print file_get_contents(__DIR__ . '/js/' . $file); } ); diff --git a/lib/js/lessnichy.js b/lib/js/lessnichy.js index 24854cc..dd5220a 100644 --- a/lib/js/lessnichy.js +++ b/lib/js/lessnichy.js @@ -1,5 +1,5 @@ -// startup compiler polling -(function(){ +// startup compiler polling. Assume that jQuery included +(function($){ function extractId(href) { return href.replace(/^[a-z-]+:\/+?[^\/]+/, '' ) // Remove protocol & domain .replace(/^\//, '' ) // Remove root / @@ -7,24 +7,6 @@ .replace(/[^\.\w-]+/g, '-') // Replace illegal characters .replace(/\./g, ':'); // Replace dots with colons(for valid id) } - function fireEvent(element, event) { - var evt; - var isString = function(it) { - return typeof it == "string" || it instanceof String; - } - element = (isString(element)) ? document.getElementById(element) : element; - if (document.createEventObject) { - // dispatch for IE - evt = document.createEventObject(); - return element.fireEvent('on' + event, evt) - } - else { - // dispatch for firefox + others - evt = document.createEvent("HTMLEvents"); - evt.initEvent(event, true, true); // event type,bubbling,cancelable - return !element.dispatchEvent(evt); - } - } var lessmap = []; var pollTimer; @@ -41,7 +23,7 @@ if(cssSheet){ var sheets = {}; config.writing = true; - sheets[config.href] = cssSheet.textContent; + sheets[config.href] = CleanCSS.process(cssSheet.textContent); //todo send csrf token $.ajax(less.lessnichy.url,{ @@ -75,6 +57,6 @@ setTimeout(function(){ pollTimer = setInterval(poll, 500); },less.poll+1000); -})(); +})(jQuery); //todo popup notifiers \ No newline at end of file