various fixes

This commit is contained in:
Parziphal 2013-10-02 21:03:33 -05:00
parent 4196aaee20
commit c4712cbc23
4 changed files with 58 additions and 88 deletions

View File

@ -21,6 +21,8 @@ class ViewHelpers
*/ */
static protected $queue = []; static protected $queue = [];
static protected $appQueue = [];
/** /**
* Searches for the helper that owns $method. * Searches for the helper that owns $method.
* *
@ -70,10 +72,11 @@ class ViewHelpers
/** /**
* For application helpers. Class names passed will be appended with "Helper". * For application helpers. Class names passed will be appended with "Helper".
* This should only be called by Rails.
*/ */
static public function addAppHelpers(array $helpers) static public function addAppHelpers(array $helpers)
{ {
self::$queue = array_merge(self::$queue, array_map(function($c) { return $c . 'Helper'; }, $helpers)); self::$appQueue = array_merge(self::$appQueue, array_map(function($c) { return $c . 'Helper'; }, $helpers));
} }
/** /**
@ -87,17 +90,16 @@ class ViewHelpers
if (!self::$helpersLoaded) { if (!self::$helpersLoaded) {
if (($router = Rails::application()->dispatcher()->router()) && ($route = $router->route())) { if (($router = Rails::application()->dispatcher()->router()) && ($route = $router->route())) {
$controllerHelper = Rails::services()->get('inflector')->camelize($route->controller()) . 'Helper'; $controllerHelper = Rails::services()->get('inflector')->camelize($route->controller()) . 'Helper';
array_unshift(self::$queue, $controllerHelper); array_unshift(self::$appQueue, $controllerHelper);
} }
$appHelper = 'ApplicationHelper'; $appHelper = 'ApplicationHelper';
array_unshift(self::$queue, $appHelper); array_unshift(self::$appQueue, $appHelper);
foreach (array_unique(self::$queue) as $name) { foreach (array_unique(self::$appQueue) as $name) {
try { self::loadHelper($name);
Rails::loader()->loadClass($name);
self::$helpers[$name] = new $name();
} catch (Rails\Loader\Exception\ExceptionInterface $e) {
} }
foreach (array_unique(self::$queue) as $name) {
self::loadHelper($name, true);
} }
# Add base helper # Add base helper
@ -106,4 +108,18 @@ class ViewHelpers
self::$helpersLoaded = true; self::$helpersLoaded = true;
} }
} }
static protected function loadHelper($name, $throwE = false)
{
try {
Rails::loader()->loadClass($name);
self::$helpers[$name] = new $name();
} catch (Rails\Loader\Exception\ExceptionInterface $e) {
if ($throwE) {
throw new Exception\RuntimeException(
sprintf("Couldn't load file for helper %s", $name)
);
}
}
}
} }

View File

@ -206,7 +206,7 @@ class Assets
protected function compileOtherFiles() protected function compileOtherFiles()
{ {
$exts = $this->filePatterns; $exts = $this->filePatterns;
$pattern = '*.{' . implode(',', $exts) .'}'; $pattern = '{' . implode(',', $exts) .'}';
$foundFiles = []; $foundFiles = [];
foreach ($this->paths as $assetsRoot) { foreach ($this->paths as $assetsRoot) {
@ -221,11 +221,37 @@ class Assets
$contents = file_get_contents($foundFile); $contents = file_get_contents($foundFile);
$this->createCompiledFile($assetsPath . '/' . $file->relative_path(), $contents, false); $this->createCompiledFile($assetsPath . '/' . $file->relative_path(), $contents, false);
if ($this->config()->digest) {
$md5 = md5_file($foundFile); $md5 = md5_file($foundFile);
$relativeDir = $file->relative_dir();
if ($relativeDir) {
$relativeDir .= '/';
}
$relativePath = $relativeDir . $file->file_root();
$basePath = $this->compilePath() . $this->prefix();
$fileroot = $basePath . '/' . $relativeDir . $file->file_root();
# Delete previous md5 files
$pattern = $fileroot . '-*.' . $ext . '*';
if ($mfiles = glob($pattern)) {
$regexp = '/-' . $md5 . '\.' . $ext . '(\.gz)?$/';
foreach ($mfiles as $mfile) {
if (!preg_match($regexp, $mfile)) {
unlink($mfile);
}
}
}
$this->updateManifestIndex($relativePath . '.' . $ext, $relativePath . '-' . $md5 . '.' . $ext);
$md5File = $file->relative_file_root_path() . '-' . $md5 . '.' . $file->type(); $md5File = $file->relative_file_root_path() . '-' . $md5 . '.' . $file->type();
$this->createCompiledFile($assetsPath . '/' . $md5File, $contents, false); $this->createCompiledFile($assetsPath . '/' . $md5File, $contents, false);
} }
} }
}
/** /**
* Deletes everything inside the compile folder. * Deletes everything inside the compile folder.

View File

@ -1,72 +0,0 @@
<?php
namespace Rails\Assets\Parser;
use Rails\Assets\Exception;
use Rails;
class PHParser
{
static protected $instance;
protected $file;
static public function parseContents($contents, $file)
{
if (!self::$instance) {
self::$instance = new self($file);
}
return self::$instance->parse($contents);
}
public function __construct($file)
{
$this->file = $file;
}
public function parse($contents)
{
ob_start();
eval('?>' . $contents);
return ob_get_clean();
}
/**
* Returns relative paths.
*/
protected function assetPath($file, array $options = [])
{
$root = \Rails::application()->router()->rootPath();
if ($root == '/') {
$root = '';
}
$path = Rails::assets()->prefix() . '/' . $file;
return $this->getRelativePath($this->file->url(), $path);
}
# SO: /a/12236654/638668
protected function getRelativePath($from, $to)
{
$from = str_replace('\\', '/', $from);
$to = str_replace('\\', '/', $to);
$from = explode('/', $from);
$to = explode('/', $to);
$relPath = $to;
foreach($from as $depth => $dir) {
if($dir === $to[$depth]) {
array_shift($relPath);
} else {
$remaining = count($from) - $depth;
if($remaining > 1) {
$padLength = (count($relPath) + $remaining - 1) * -1;
$relPath = array_pad($relPath, $padLength, '..');
break;
} else {
$relPath[0] = './' . $relPath[0];
}
}
}
return implode('/', $relPath);
}
}

View File

@ -222,10 +222,10 @@ $config->assets = [
* when compiling. These values will be passed to glob() with GLOB_BRACE. * when compiling. These values will be passed to glob() with GLOB_BRACE.
*/ */
'patterns' => [ 'patterns' => [
'.gif', '*.gif',
'.png', '*.png',
'.jpg', '*.jpg',
'.jpeg', '*.jpeg',
], ],
/** /**