Skip to content

Commit

Permalink
upd: rework less compilation, introduce minified styles;
Browse files Browse the repository at this point in the history
  • Loading branch information
kulbakin committed Apr 18, 2015
1 parent a97a310 commit 55b9250
Show file tree
Hide file tree
Showing 16 changed files with 8,300 additions and 3,470 deletions.
2 changes: 2 additions & 0 deletions composer.json
Expand Up @@ -7,6 +7,8 @@
"bin": ["bin/anahita"],
"require": {
"php": ">=5.3",
"oyejorge/less.php": "dev-master",
"matthiasmullie/minify": "dev-master",
"symfony/console": "2.1.*",
"dg/mysql-dump": "1.1.0",
"easybook/geshi": "1.0.8.11"
Expand Down
29 changes: 24 additions & 5 deletions src/libraries/anahita/file/pathfinder.php
Expand Up @@ -4,7 +4,7 @@
*
* <code>
* $finder = KService::get('anahita:file.pathfinder');
* $finder->addSearchDirs(array($path1,$path2));
* $finder->addSearchDirs(array($path1, $path2));
* $finder->getPath($relative_path);
* </code>
*
Expand Down Expand Up @@ -53,7 +53,6 @@ protected function _initialize(KConfig $config)
* Adds an array of search dirs to search for a path
*
* @param $paths string|array Adds a base path
*
* @return AnFilePathfinder
*/
public function addSearchDirs($dirs)
Expand Down Expand Up @@ -120,8 +119,8 @@ protected function _findPath($path)
}
}

return $file;
}
return $this->_absolutePath($file);
}

/**
* Unify directory separator to one used by environemnt OS
Expand All @@ -131,6 +130,26 @@ protected function _findPath($path)
*/
protected function _unifyPath($path)
{
return preg_replace('%[/\\\\]%', DS, $path);
return preg_replace('%[/\\\\]%', DS, $path);
}

/**
* Works like realpath() but without resolving symbolic links
*
* @param string $path
* @return string
*/
protected function _absolutePath($path) {
$parts = explode(DS, $path);
$absolutes = array();
foreach ($parts as $i => $part) {
if ('' == $part && $i > 0 || '.' == $part) continue;
if ('..' == $part) {
array_pop($absolutes);
} else {
$absolutes[] = $part;
}
}
return implode(DS, $absolutes);
}
}
35 changes: 10 additions & 25 deletions src/libraries/anahita/helper/file.php
@@ -1,27 +1,13 @@
<?php

/**
* LICENSE: ##LICENSE##
*
* @category Anahita
* @package Anahita_Helper
* @author Arash Sanieyan <ash@anahitapolis.com>
* @author Rastin Mehr <rastin@anahitapolis.com>
* @copyright 2008 - 2010 rmdStudio Inc./Peerglobe Technology Inc
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
* @version SVN: $Id$
* @link http://www.anahitapolis.com
*/

/**
* File, Path Helpers
*
*
* @category Anahita
* @package Anahita_Helper
* @author Arash Sanieyan <ash@anahitapolis.com>
* @author Rastin Mehr <rastin@anahitapolis.com>
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
* @link http://www.anahitapolis.com
* @copyright 2008 - 2010 rmdStudio Inc./Peerglobe Technology Inc
*/
class AnHelperFile extends KObject
{
Expand All @@ -35,30 +21,29 @@ class AnHelperFile extends KObject
*/
static public function getTravelPath($from, $to)
{
$from = explode(DS, $from);
$to = explode(DS, $to);
$from = explode(DS, str_replace(array('/', '\\'), DS, $from));
$to = explode(DS, str_replace(array('/', '\\'), DS, $to));

$relPath = $to;

foreach($from as $depth => $dir)
{
foreach($from as $depth => $dir) {
// find first non-matching dir
if($dir === $to[$depth]) {
// ignore this directory
array_shift($relPath);
} else {
// get number of remaining dirs to $from
$remaining = count($from) - ($depth - 1);
$remaining = count($from) - ($depth - 1);
if($remaining > 1) {
// add traversals up to first matching dir
$padLength = (count($relPath) + $remaining - 1) * -1;
$relPath = array_pad($relPath, $padLength, '..');
break;
} else {
$relPath[0] = './' . $relPath[0];
$relPath[0] = './'.$relPath[0];
}
}
}
return implode('/', $relPath);
}
}
}
}
1 change: 0 additions & 1 deletion src/libraries/default/application/dispatcher.php
Expand Up @@ -67,7 +67,6 @@ protected function _actionRoute(KCommandContext $context)
// trigger the onAfterRoute events
$this->_application->triggerEvent('onAfterRoute');
$url->query = KRequest::get('get','raw');

//globally set ItemId
global $Itemid;

Expand Down
26 changes: 9 additions & 17 deletions ...application/template/helper/less/less.php → ...ault/application/template/helper/less.php 100755 → 100644
@@ -1,6 +1,4 @@
<?php
require_once 'compiler.php';

/**
* Less Compiler Template Helper
*
Expand All @@ -26,20 +24,14 @@ public function compile($config = array())

$config->append(array(
'parse_urls' => true,
'compress' => true,
'import' => array(),
'force' => false,
'output' => null, //required
'input' => null, //required
));

$less = new lessc();
$less->setPreserveComments( ! $config->compress);
if ($config->compress) {
$less->setFormatter("compressed");
}
$config['import'] = $config['import'];
$less->setImportDir($config['import']);
$less = new lessc();
$less->setImportDir($config->import->toArray());
$cache_file = JPATH_CACHE.'/less-'.md5($config->input);

if (file_exists($cache_file)) {
Expand All @@ -49,13 +41,12 @@ public function compile($config = array())
}
$force = $config->force;

//if output doesn't exsit then force compile
// if output doesn't exsit then force compile
if ( ! is_readable($config->output)) {
$force = true;
}

//check if any of the import folder have changed or
//if yes then re-compile
// check if any of the import folder have changed and recompile if necessary
if (is_array($cache)) {
foreach ($config['import'] as $path) {
if (is_readable($path) && filemtime($path) > $cache['updated']) {
Expand All @@ -72,17 +63,18 @@ public function compile($config = array())
}
if ( ! is_array($cache) || $new_cache['updated'] > $cache['updated']) {
if ($config->parse_urls) {
$new_cache['compiled'] = $this->_parseUrls($new_cache['compiled'], $config->import);
$new_cache['compiled'] = $this->_parseUrls($new_cache['compiled'], array_merge(array(dirname($config->output)), $config->import->toArray()));
}

//store the cache
file_put_contents($cache_file, serialize($new_cache));
//store the compiled file
//create a directory if
if ( ! file_exists(dirname($config->output))) {
if ( ! file_exists(dirname($config->output))) { // create a directory if necessary
mkdir(dirname($config->output), 0755);
}
file_put_contents($config->output, $new_cache['compiled']);
$minifier = new \MatthiasMullie\Minify\CSS($new_cache['compiled']);
file_put_contents(preg_replace('%\.css$%', '.min.css', $config->output), $minifier->minify());
}
}

Expand All @@ -108,7 +100,7 @@ protected function _parseUrls($text, array $paths)

if (($path = $finder->getPath($match))) {
$path = AnHelperFile::getTravelPath($starting_path, $path);
$path = str_replace(DS,'/',$path);
$path = str_replace(DS, '/', $path);
$replaces[$match] = $path;
}
}
Expand Down

0 comments on commit 55b9250

Please sign in to comment.