Skip to content

Commit

Permalink
Use Config object to store config options
Browse files Browse the repository at this point in the history
  • Loading branch information
QWp6t committed Dec 25, 2016
1 parent 5d454ba commit c0fc19e
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 28 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"require": {
"php": ">=5.6.4",
"composer/installers": "~1.0",
"illuminate/view": "~5.3.0"
"illuminate/view": "~5.3.0",
"illuminate/config": "~5.3.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^2.5.1",
Expand Down
49 changes: 47 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* ├── STYLESHEETPATH -> /srv/www/example.com/current/web/app/themes/sage
* └── TEMPLATEPATH -> /srv/www/example.com/current/web/app/themes/sage/templates
*/
if (is_customize_preview()) {
if (App\config('sage.disable_option_hack')) {
return;
}
add_filter('template', function ($stylesheet) {
Expand Down
8 changes: 6 additions & 2 deletions src/filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
array_map(function ($type) {
add_filter("{$type}_template_hierarchy", function ($templates) {
return call_user_func_array('array_merge', array_map(function ($template) {
$normalizedTemplate = preg_replace(['%^/?(templates)?/?%', '%(\.blade)?(\.php)?$%'], '', $template);
$transforms = [
'%^/?(templates)?/?%' => config('sage.disable_option_hack') ? 'templates/' : '',
'%(\.blade)?(\.php)?$%' => ''
];
$normalizedTemplate = preg_replace(array_keys($transforms), array_values($transforms), $template);
return ["{$normalizedTemplate}.blade.php", "{$normalizedTemplate}.php"];
}, $templates));
});
Expand All @@ -53,7 +57,7 @@
echo template($template, $data);

// Return a blank file to make WordPress happy
return get_template_directory() . '/index.php';
return dirname(__DIR__).'/index.php';
}, PHP_INT_MAX);

/**
Expand Down
43 changes: 36 additions & 7 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,51 @@
namespace App;

use Roots\Sage\Container;
use Illuminate\Contracts\Container\Container as ContainerContract;

/**
* @param string $name
* @return Container|mixed
* Get the sage container.
*
* @param string $abstract
* @param array $parameters
* @param ContainerContract $container
* @return ContainerContract|mixed
* @SuppressWarnings(PHPMD.StaticAccess)
*/
function sage($name = '')
function sage($abstract = null, $parameters = [], ContainerContract $container = null)
{
static $container;
if (!$container) {
$container = new Container;
$container = $container ?: Container::getInstance();
if (!$abstract) {
return $container;
}
return $name ? (isset($container[$name]) ? $container[$name] : $container["sage.{$name}"]) : $container;
return $container->bound($abstract)
? $container->make($abstract, $parameters)
: $container->make("sage.{$abstract}", $parameters);
}

/**
* Get / set the specified configuration value.
*
* If an array is passed as the key, we will assume you want to set an array of values.
*
* @param array|string $key
* @param mixed $default
* @return mixed|\Roots\Sage\Config
* @copyright Taylor Otwell
* @link https://github.com/laravel/framework/blob/c0970285/src/Illuminate/Foundation/helpers.php#L254-L265
*/
function config($key = null, $default = null)
{
if (is_null($key)) {
return sage('config');
}
if (is_array($key)) {
return sage('config')->set($key);
}
return sage('config')->get($key, $default);
}

/**
* @param string $file
* @param array $data
* @return string
Expand Down
8 changes: 8 additions & 0 deletions src/lib/Sage/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Roots\Sage;

class Config extends \Illuminate\Config\Repository
{

}
49 changes: 34 additions & 15 deletions src/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App;

use Illuminate\Contracts\Container\Container as ContainerContract;
use Roots\Sage\Assets\JsonManifest;
use Roots\Sage\Config;
use Roots\Sage\Template\Blade;
use Roots\Sage\Template\BladeProvider;

Expand Down Expand Up @@ -88,30 +90,37 @@
/**
* Sage config
*/
sage()->bindIf('config', function () {
return [
'view.paths' => [TEMPLATEPATH, STYLESHEETPATH],
'view.compiled' => wp_upload_dir()['basedir'].'/cache/compiled',
'view.namespaces' => ['App' => WP_CONTENT_DIR],
'assets.manifest' => get_stylesheet_directory().'/dist/assets.json',
'assets.uri' => get_stylesheet_directory_uri().'/dist'
];
});
$paths = [
'dir.stylesheet' => get_stylesheet_directory(),
'dir.template' => get_template_directory(),
'dir.upload' => wp_upload_dir()['basedir'],
'uri.stylesheet' => get_stylesheet_directory_uri(),
'uri.template' => get_template_directory_uri(),
];
$viewPaths = collect(preg_replace('%[\/]?(templates)?[\/.]*?$%', '', [STYLESHEETPATH, TEMPLATEPATH]))
->flatMap(function ($path) {
return ["{$path}/templates", $path];
})->unique()->toArray();
config([
'assets.manifest' => "{$paths['dir.stylesheet']}/dist/assets.json",
'assets.uri' => "{$paths['uri.stylesheet']}/dist",
'view.compiled' => "{$paths['dir.upload']}/cache/compiled",
'view.namespaces' => ['App' => WP_CONTENT_DIR],
'view.paths' => $viewPaths,
] + $paths);

/**
* Add JsonManifest to Sage container
*/
sage()->singleton('sage.assets', function ($app) {
$config = $app['config'];
return new JsonManifest($config['assets.manifest'], $config['assets.uri']);
sage()->singleton('sage.assets', function () {
return new JsonManifest(config('assets.manifest'), config('assets.uri'));
});

/**
* Add Blade to Sage container
*/
sage()->singleton('sage.blade', function ($app) {
$config = $app['config'];
$cachePath = $config['view.compiled'];
sage()->singleton('sage.blade', function (ContainerContract $app) {
$cachePath = config('view.compiled');
if (!file_exists($cachePath)) {
wp_mkdir_p($cachePath);
}
Expand All @@ -126,3 +135,13 @@
return '<?= App\\asset_path(\''.trim($asset, '\'"').'\'); ?>';
});
});

/**
* Init config
*/
sage()->bindIf('config', Config::class, true);

/**
* Disable option hack if we're in Customizer preview
*/
config(['sage.disable_option_hack' => is_customize_preview()]);

0 comments on commit c0fc19e

Please sign in to comment.