From f93a1f2c8c2df7a294c02d5c75bcc6b566754df6 Mon Sep 17 00:00:00 2001 From: Austin Pray Date: Fri, 30 Jan 2015 23:13:45 -0600 Subject: [PATCH 1/3] Memoize asset_path function Previously roots would do a file operation each time asset_path is called. Now it will only parse the json manifest one time per program execution. Extract json reading to class that can be used to read bower.json --- lib/assets.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/assets.php b/lib/assets.php index d6fedb0..30c2a35 100644 --- a/lib/assets.php +++ b/lib/assets.php @@ -17,17 +17,34 @@ namespace Roots\Sage\Assets; * - An ID has been defined in config.php * - You're not logged in as an administrator */ + +class JsonManifest { + private $manifest; + + public function __construct($manifest_path) { + if (file_exists($manifest_path)) { + $this->manifest = json_decode(file_get_contents($manifest_path), true); + } else { + $this->manifest = []; + } + } + + public function get() { + return $this->manifest; + } +} + function asset_path($filename) { $dist_path = get_template_directory_uri() . '/dist/'; $directory = dirname($filename) . '/'; $file = basename($filename); - $manifest_path = get_template_directory() . '/dist/assets.json'; + static $manifest; - if (file_exists($manifest_path)) { - $manifest = json_decode(file_get_contents($manifest_path), true); - } else { - $manifest = []; + if (empty($manifest)) { + $manifest_path = get_template_directory() . '/dist/assets.json'; + $manifest = new JsonManifest($manifest_path); + $manifest = $manifest->get(); } if (WP_ENV !== 'development' && array_key_exists($file, $manifest)) { From 979b99f3976f8d995188b6d4494c3c35545ad760 Mon Sep 17 00:00:00 2001 From: Austin Pray Date: Sat, 31 Jan 2015 00:33:07 -0600 Subject: [PATCH 2/3] reconcile bower dependency versions with CDN url --- lib/assets.php | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/assets.php b/lib/assets.php index 30c2a35..7f89d4a 100644 --- a/lib/assets.php +++ b/lib/assets.php @@ -38,7 +38,6 @@ function asset_path($filename) { $dist_path = get_template_directory_uri() . '/dist/'; $directory = dirname($filename) . '/'; $file = basename($filename); - static $manifest; if (empty($manifest)) { @@ -54,6 +53,31 @@ function asset_path($filename) { } } +function bower_map_to_cdn($dependency, $fallback) { + static $bower; + + if (empty($bower)) { + $bower_path = get_template_directory() . '/bower.json'; + $bower = new JsonManifest($bower_path); + $bower = $bower->get(); + } + + $templates = [ + 'google' => '//ajax.googleapis.com/ajax/libs/%name%/%version%/%file%' + ]; + + $version = $bower['dependencies'][$dependency['name']]; + + if (isset($version) && preg_match('/^(\d+\.){2}\d+$/', $version)) { + $search = ['%name%', '%version%', '%file%']; + $replace = [$dependency['name'], $version, $dependency['file']]; + return str_replace($search, $replace, $templates[$dependency['cdn']]); + } else { + return $fallback; + } + +} + function assets() { wp_enqueue_style('sage_css', asset_path('styles/main.css'), false, null); @@ -66,7 +90,11 @@ function assets() { if (!is_admin() && current_theme_supports('jquery-cdn')) { wp_deregister_script('jquery'); - wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', [], null, true); + wp_register_script('jquery', bower_map_to_cdn([ + 'name' => 'jquery', + 'cdn' => 'google', + 'file' => 'jquery.min.js' + ], asset_path('scripts/jquery.js')), [], null, true); add_filter('script_loader_src', __NAMESPACE__ . '\\jquery_local_fallback', 10, 2); } From 7b54b36afd92bd74e0e69bd9d3718810b356e002 Mon Sep 17 00:00:00 2001 From: Austin Pray Date: Sat, 31 Jan 2015 11:59:57 -0600 Subject: [PATCH 3/3] Adds get_path method to JsonManifest --- lib/assets.php | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/assets.php b/lib/assets.php index 7f89d4a..c090cfb 100644 --- a/lib/assets.php +++ b/lib/assets.php @@ -32,6 +32,24 @@ class JsonManifest { public function get() { return $this->manifest; } + + public function getPath($key = '', $default = null) { + $collection = $this->manifest; + if (is_null($key)) { + return $collection; + } + if (isset($collection[$key])) { + return $collection[$key]; + } + foreach (explode('.', $key) as $segment) { + if (!isset($collection[$segment])) { + return $default; + } else { + $collection = $collection[$segment]; + } + } + return $collection; + } } function asset_path($filename) { @@ -43,11 +61,10 @@ function asset_path($filename) { if (empty($manifest)) { $manifest_path = get_template_directory() . '/dist/assets.json'; $manifest = new JsonManifest($manifest_path); - $manifest = $manifest->get(); } - if (WP_ENV !== 'development' && array_key_exists($file, $manifest)) { - return $dist_path . $directory . $manifest[$file]; + if (WP_ENV !== 'development' && array_key_exists($file, $manifest->get())) { + return $dist_path . $directory . $manifest->get()[$file]; } else { return $dist_path . $directory . $file; } @@ -59,14 +76,13 @@ function bower_map_to_cdn($dependency, $fallback) { if (empty($bower)) { $bower_path = get_template_directory() . '/bower.json'; $bower = new JsonManifest($bower_path); - $bower = $bower->get(); } $templates = [ 'google' => '//ajax.googleapis.com/ajax/libs/%name%/%version%/%file%' ]; - $version = $bower['dependencies'][$dependency['name']]; + $version = $bower->getPath('dependencies.' . $dependency['name']); if (isset($version) && preg_match('/^(\d+\.){2}\d+$/', $version)) { $search = ['%name%', '%version%', '%file%'];