From c4712cbc23fa186e2aceea1a70af65c4853fc328 Mon Sep 17 00:00:00 2001 From: Parziphal Date: Wed, 2 Oct 2013 21:03:33 -0500 Subject: [PATCH] various fixes --- lib/Rails/ActionView/ViewHelpers.php | 32 ++++++--- lib/Rails/Assets/Assets.php | 34 ++++++++-- lib/Rails/Assets/Parser/PHParser - Copy.php | 72 --------------------- lib/Rails/Config/default_config.php | 8 +-- 4 files changed, 58 insertions(+), 88 deletions(-) delete mode 100755 lib/Rails/Assets/Parser/PHParser - Copy.php diff --git a/lib/Rails/ActionView/ViewHelpers.php b/lib/Rails/ActionView/ViewHelpers.php index f94a579..37cbe71 100755 --- a/lib/Rails/ActionView/ViewHelpers.php +++ b/lib/Rails/ActionView/ViewHelpers.php @@ -21,6 +21,8 @@ class ViewHelpers */ static protected $queue = []; + static protected $appQueue = []; + /** * Searches for the helper that owns $method. * @@ -70,10 +72,11 @@ class ViewHelpers /** * For application helpers. Class names passed will be appended with "Helper". + * This should only be called by Rails. */ 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 (($router = Rails::application()->dispatcher()->router()) && ($route = $router->route())) { $controllerHelper = Rails::services()->get('inflector')->camelize($route->controller()) . 'Helper'; - array_unshift(self::$queue, $controllerHelper); + array_unshift(self::$appQueue, $controllerHelper); } $appHelper = 'ApplicationHelper'; - array_unshift(self::$queue, $appHelper); + array_unshift(self::$appQueue, $appHelper); + foreach (array_unique(self::$appQueue) as $name) { + self::loadHelper($name); + } foreach (array_unique(self::$queue) as $name) { - try { - Rails::loader()->loadClass($name); - self::$helpers[$name] = new $name(); - } catch (Rails\Loader\Exception\ExceptionInterface $e) { - } + self::loadHelper($name, true); } # Add base helper @@ -106,4 +108,18 @@ class ViewHelpers 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) + ); + } + } + } } diff --git a/lib/Rails/Assets/Assets.php b/lib/Rails/Assets/Assets.php index a77d297..f9619df 100755 --- a/lib/Rails/Assets/Assets.php +++ b/lib/Rails/Assets/Assets.php @@ -206,7 +206,7 @@ class Assets protected function compileOtherFiles() { $exts = $this->filePatterns; - $pattern = '*.{' . implode(',', $exts) .'}'; + $pattern = '{' . implode(',', $exts) .'}'; $foundFiles = []; foreach ($this->paths as $assetsRoot) { @@ -221,9 +221,35 @@ class Assets $contents = file_get_contents($foundFile); $this->createCompiledFile($assetsPath . '/' . $file->relative_path(), $contents, false); - $md5 = md5_file($foundFile); - $md5File = $file->relative_file_root_path() . '-' . $md5 . '.' . $file->type(); - $this->createCompiledFile($assetsPath . '/' . $md5File, $contents, false); + + if ($this->config()->digest) { + $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(); + $this->createCompiledFile($assetsPath . '/' . $md5File, $contents, false); + } } } diff --git a/lib/Rails/Assets/Parser/PHParser - Copy.php b/lib/Rails/Assets/Parser/PHParser - Copy.php deleted file mode 100755 index 240c374..0000000 --- a/lib/Rails/Assets/Parser/PHParser - Copy.php +++ /dev/null @@ -1,72 +0,0 @@ -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); - } -} diff --git a/lib/Rails/Config/default_config.php b/lib/Rails/Config/default_config.php index 06981e4..8d3dad8 100755 --- a/lib/Rails/Config/default_config.php +++ b/lib/Rails/Config/default_config.php @@ -222,10 +222,10 @@ $config->assets = [ * when compiling. These values will be passed to glob() with GLOB_BRACE. */ 'patterns' => [ - '.gif', - '.png', - '.jpg', - '.jpeg', + '*.gif', + '*.png', + '*.jpg', + '*.jpeg', ], /**