Skip to content
This repository has been archived by the owner on Jan 28, 2019. It is now read-only.

Commit

Permalink
last commit update
Browse files Browse the repository at this point in the history
  • Loading branch information
velosipedist committed Apr 30, 2014
1 parent fcf3e75 commit 926df26
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 65 deletions.
17 changes: 9 additions & 8 deletions example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
)
);

?>

<!doctype html>
<html>
<head>
Expand All @@ -31,18 +30,20 @@
<!-- Here will be LESS scripts and sources -->
<?
LESS::head(
[
LESS::JS => '/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,
]
)
);
?>
<!-- End of LESS scripts and sources -->
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
</head>
<body>
<div class="foo">
<div class="bar">.foo > .bar text</div>
</div>
</body>
</html>
</html>
<!--There other js will be appended-->
2 changes: 1 addition & 1 deletion example/lessnichy-app/index.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
//require_once __DIR__ . "/../../vendor/autoload.php"; //debug instead of phar inclusion
//shebang workaround
ob_start();
require __DIR__.'/lessnichy.phar'; // production include
ob_end_clean();
//require_once __DIR__ . "/../../vendor/autoload.php"; //debug instead of phar inclusion
// require php on dev mode or phar on production
LESS::listen();
120 changes: 97 additions & 23 deletions lib/Client.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,58 @@
<?php
namespace Lessnichy;

use Closure;

/**
* Class Client
* @package Lessnichy
*/
class Client
{
/**
* @var string API base url
*/
private $baseUrl;
/**
* @var string[] Less stylesheets urls
*/
private $stylesheets;
/**
* @var bool Does LESS head scripts printed
*/
private $headPrinted = false;
/**
* @var bool Whether to use .less sources and browser compilation. Otherwise just print compiled .css
*/
private $enableLessMode = true;
/**
* @var callable Callback to define {@link enableLessMode}
*/
private $lessModeTrigger;

function __construct($baseUrl)
/**
* @param $baseUrl
* @param bool|Closure $lessMode
*/
function __construct($baseUrl, $lessMode = true)
{
$baseUrl = rtrim($baseUrl, '/');
if(php_sapi_name() == 'cli-server'){
$baseUrl = rtrim($baseUrl, '/');
if (php_sapi_name() == 'cli-server') {
$baseUrl .= '/index.php';
}
$this->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;
Expand All @@ -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 "<script src='{$baseurl}/js/clean-css.min.js'></script>\n";
print "<script src='{$baseurl}/js/lessnichy.js'></script>";
}
}
);
}

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 "<script type='text/javascript'>var less = " . $json . ";</script>\n";
print "<script type='text/javascript'>var less = " . json_encode($lessJsOptions) . ";</script>\n";

foreach ($this->stylesheets as $url) {
print "<link rel='stylesheet/less' type='text/css' href='$url' />\n";
foreach ($this->stylesheets as $lessStylesheetUrl) {
print "<link rel='stylesheet/less' type='text/css' href='$lessStylesheetUrl' />\n";
}
// if no url provided, assume that less.js connected manually
if ($lessJsUrl) {
print "<script type='text/javascript' src='$lessJsUrl'></script>\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 <link> 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 "<link rel='stylesheet' type='text/css' href='{$LessStylesheetUrl}.css'>";
}
}
}

17 changes: 11 additions & 6 deletions lib/Lessnichy.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
namespace Lessnichy {
use Closure;

/**
* Lessnichy magic facade
*/
Expand Down Expand Up @@ -27,21 +29,22 @@ class Lessnichy
private static $server;

/**
* @param $baseUrl
* @param string $baseUrl full base url to Lessnicy API listener dir
* @param bool|Closure $lessMode
* @return Client
*/
public static function connect($baseUrl)
public static function connect($baseUrl, $lessMode = true)
{
if (is_null(self::$client)) {
self::$client = new Client($baseUrl);
self::$client = new Client($baseUrl, $lessMode);
}
return self::$client;
}

/**
* @param $lessStylesheets
*/
public static function add($lessStylesheets)
public static function add(array $lessStylesheets)
{
self::ensureClient();
return self::$client->add($lessStylesheets);
Expand All @@ -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()
{
Expand All @@ -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();
}
Expand Down
23 changes: 18 additions & 5 deletions lib/app.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
// bootstrap app
$app = new \Slim\Slim();
use Slim\Slim;

$app = new Slim();
//todo add auth middleware
//todo add common Lessnichy\Server injection

Expand All @@ -19,16 +21,27 @@ function ($file) {
}
);
// save less rendered
$app->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);
}
);
Expand Down
26 changes: 4 additions & 22 deletions lib/js/lessnichy.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
// 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 /
.replace(/\.[a-zA-Z]+$/, '' ) // Remove simple extension
.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;
Expand All @@ -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,{
Expand Down Expand Up @@ -75,6 +57,6 @@
setTimeout(function(){
pollTimer = setInterval(poll, 500);
},less.poll+1000);
})();
})(jQuery);

//todo popup notifiers

0 comments on commit 926df26

Please sign in to comment.