diff --git a/.gitignore b/.gitignore index 81b9258..79281ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.lock phpunit.xml vendor +.idea diff --git a/bladerunner.php b/bladerunner.php index 0a5d3f7..1fc078d 100644 --- a/bladerunner.php +++ b/bladerunner.php @@ -3,7 +3,7 @@ Plugin Name: Bladerunner Plugin URI: http://bladerunner.elseif.se Description: Laravel Blade template engine for WordPress -Version: 1.7.1-dev +Version: 1.7.5 Author: Andreas Ek Author URI: https://www.elseif.se/ License: MIT License diff --git a/composer.json b/composer.json index 62204a6..8c73c7d 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,6 @@ { "name": "ekandreas/bladerunner", "type": "wordpress-plugin", - "version": "1.7.1-dev", "description": "WordPress Blade L5 template engine", "keywords": ["laravel", "blade", "wordpress"], "license": "MIT", @@ -12,26 +11,30 @@ } ], "require": { - "php": ">=5.6", - "illuminate/config": "5.*", - "illuminate/view": "5.*", - "symfony/yaml": "3.*", - "brain/hierarchy":"2.*" + "php": "^5.6 || ^7.0", + "illuminate/config": "^5", + "illuminate/view": "^5", + "symfony/yaml": "^3", + "brain/hierarchy": "^2" + }, + "require-dev": { + "frozzare/wp-test-suite": "*" }, "autoload": { "psr-4": { - "Bladerunner\\Tests\\": "tests/", "Bladerunner\\": "src/" } }, - "prefer-stable": true, - "minimum-stability": "dev", - "require-dev": { - "frozzare/wp-test-suite": "*" + "autoload-dev": { + "psr-4": { + "Bladerunner\\Tests\\": "tests/" + } }, "extra": { "branch-alias": { - "dev-develop": "1.7.1-dev" + "dev-master": "1.7.5-dev" } - } -} + }, + "minimum-stability": "dev", + "prefer-stable": true +} \ No newline at end of file diff --git a/globals/filters.php b/globals/filters.php index de3341e..7e7d5d5 100644 --- a/globals/filters.php +++ b/globals/filters.php @@ -12,10 +12,13 @@ ]; $normalizedTemplate = preg_replace(array_keys($transforms), array_values($transforms), $template); - $controllerPaths = collect([ - apply_filters('bladerunner/controller/paths', []), - get_stylesheet_directory() . '/controllers', - ])->unique()->toArray(); + $paths = apply_filters('bladerunner/controller/paths', []); + if (!is_array($paths)) { + $paths = [$paths]; + } + $paths[] = get_stylesheet_directory() . '/controllers'; + + $controllerPaths = collect($paths)->unique()->toArray(); foreach ($controllerPaths as $path) { if (!is_string($path)) { @@ -39,8 +42,22 @@ return $result; }); }, [ - 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date', 'home', - 'frontpage', 'page', 'paged', 'search', 'single', 'singular', 'attachment' + 'index', + '404', + 'archive', + 'author', + 'category', + 'tag', + 'taxonomy', + 'date', + 'home', + 'frontpage', + 'page', + 'paged', + 'search', + 'single', + 'singular', + 'attachment' ]); add_filter('template_include', function ($template) { diff --git a/globals/setup.php b/globals/setup.php index ed5c8cb..8dc35b6 100644 --- a/globals/setup.php +++ b/globals/setup.php @@ -17,19 +17,9 @@ 'uri.template' => get_template_directory_uri(), ]; - $viewPaths = collect(preg_replace('%[\/]?(templates)?[\/.]*?$%', '', [ - apply_filters('bladerunner/template/bladepath', $paths['dir.stylesheet']), - $paths['dir.stylesheet'] . DIRECTORY_SEPARATOR . 'views', - STYLESHEETPATH, - TEMPLATEPATH, - ])) - ->flatMap(function ($path) { - return ["{$path}/templates", $path]; - })->unique()->toArray(); - \Bladerunner\Config::repo([ - 'view.compiled' => "{$paths['dir.upload']}/.cache", - 'view.paths' => $viewPaths, + 'view.compiled' => apply_filters('bladerunner/cache/path', "{$paths['dir.upload']}/.cache"), + 'view.paths' => \Bladerunner\Config::viewPaths(), ] + $paths); /** @@ -37,9 +27,12 @@ */ Bladerunner\Container::current()->singleton('bladerunner.blade', function (ContainerContract $app) { $cachePath = Bladerunner\Config::repo('view.compiled'); - if (!file_exists($cachePath)) { + + $makePath = apply_filters('bladerunner/cache/make', true); + if ($makePath && !file_exists($cachePath)) { wp_mkdir_p($cachePath); } + (new \Bladerunner\BladeProvider($app))->register(); return new \Bladerunner\Blade($app['view'], $app); }); @@ -48,14 +41,7 @@ return ''; }); - if (defined('WP_DEBUG') && WP_DEBUG) { - array_map('unlink', - glob(\Bladerunner\Config::repo('view.compiled') . '/*')); - } - - $extensions = apply_filters('bladerunner/extend', []); - var_dump($extensions); if ($extensions && is_array($extensions)) { foreach ($extensions as $extension) { if (is_callable($extension)) { diff --git a/readme.md b/readme.md index 6c6949d..f850ce6 100644 --- a/readme.md +++ b/readme.md @@ -12,7 +12,7 @@ WordPress plugin for Laravel Blade templating. To install it to your Composer based WordPress installation: ``` -composer require ekandreas/bladerunner:* +composer require ekandreas/bladerunner ``` Activate the plugin inside WordPress and templates with *.blade.php are inspected and active. Your theme still needs an index.php due to WordPress basic functionality. When removed the theme is known as broken. @@ -23,14 +23,14 @@ Releases to this plugin is listed last in this readme. ## Hello World 1. Install the library with composer -2. Make sure the cache-folder is writeable in uploads, eg ../wp-content/uploads/.cache +2. Make sure the cache-folder is writeable in uploads, eg `../wp-content/uploads/.cache` 3. Activate the plugin 4. Create a view, eg: ```twig Hello World Page rendered at {{ date('Y-m-d H:i:s') }} ``` -5. In your index.php, add a global call for the view created, eg: +5. In your `index.php`, add a global call for the view created, eg: ```php $module]); @@ -88,7 +88,7 @@ Extend the Controller Class, it is recommended that the class name matches the f Create methods within the Controller Class: * Use public function to expose the returned values to the Blade views/s. * Use public static function to use the function within your Blade view/s. -* Use protected function for internal controller methods as only public methods are exposed to the view. You can run them within __construct. +* Use protected function for internal controller methods as only public methods are exposed to the view. You can run them within `__construct` ### Controller example: @@ -143,21 +143,28 @@ add_filter('bladerunner/cache/path', function() { }); ``` -Permission settings to cache folder, default 777 +If you don't want Bladerunner to create the cache folder: ```php -add_filter('bladerunner/cache/permission', function() { - return 644; +add_filter('bladerunner/cache/make', function() { + return false; }); ``` -If you don't want Bladerunner to check for permissions form cache folder then set the return to null, eg: + +If you don't want Blade to use cached view files: ```php -add_filter('bladerunner/cache/permission', '__return_null'); +add_filter('bladerunner/cache/disable', function() { + return true; +}); ``` + If you wan't to customize the base paths where you have your views stored, use: ```php -add_filter('bladerunner/template/bladepath', function ($paths) { - $paths[] = PLUGIN_DIR . '/my-fancy-plugin/views'; - return $path; +add_filter('bladerunner/template/bladepath', function ($paths) { + if (!is_array($paths)) { + $paths = [$paths]; + } + $paths[] = ABSPATH . '../../resources/views'; + return $paths; }); ``` If you wan't to customize the controller paths where you have your controllers stored, use: @@ -168,59 +175,8 @@ add_filter('bladerunner/controller/paths', function ($paths) { }); ``` -#### Custom extensions -If you are comfortable with regular expressions and want to add your own extensions to your templates use the filter ``bladerunner/extend``. -Note! It takes one *array* as parameter and requires two keys; "pattern" and "replace". - -```php -$extensions[] = [ - 'pattern' => '...', - 'replace' => '...', -]; -``` - -Use the filter as possible way to add your own custom extensions. - -In this example we want to add ``@mysyntax`` as a custom extension. -```php -/* - * Add custom extension @mysyntax to Bladerunner - */ -add_filter('bladerunner/extend', function($extensions) { - $extensions[] = [ - 'pattern' => '/(\s*)@mysyntax(\s*)/', - 'replace' => '$1$2', - ]; - return $extensions; -}); -``` -Then use your new syntax inside a WordPress blade template like so: -```php - @mysyntax -``` - We will soon add more WordPress extenstions to the Bladerunner engine. Please give us your great examples to implement! -#### Template Data Filter -A simple way to pass data to a given view before it's loaded. - -Set the filter ``bladerunner/templates/data/{view}`` before running a template to pass custom data to the template, eg: -```php -$data = [ - 'this' => 'that', - 'other' => 'perhaps', -]; -add_filter('bladerunner/templates/data/single', $data); -``` - -Inside your "single.blade.php" / view file you will be able to access the passed data like so: -```php -{{ $data['this'] }} -{{ $data['other'] }} -``` - -Default value for data is an empty array. - ## Links * [Bladerunner site with documentation and distro](http://bladerunner.aekab.se) * [Docs Laravel Blade v5.2](https://laravel.com/docs/5.2/blade) @@ -245,26 +201,3 @@ Using *Testrunner* (required-dev package) and Docker the test should be exexuted ```bash vendor/bin/dep testrunner ``` - -## Releases - -### 1.7 -Controller concept included, read more about it at [https://bladerunner.elseif.se/controllers](https://bladerunner.elseif.se/controllers) - -### Release 1.6.1 and 1.6.2 -Just to update Laravel Blade engine upgrades - -### Release 1.6 -Laravel 5.4 with Components and Slots! -This is a completely rewrite, perhaps v2? Extracted from Roots Sage. -Some breaking changes: -* Laravel Config and View v5.4, these are in dev mode right now. -* Global function view over old global bladerunner for no echo as default. -* No template filters. You need to use "view" or "bladerunner" global functions in your ordinary WordPress templates. -* No WP admin pages, this is a dev tool :-) - -### Release 1.5 -Now only supports PHP5.6 and greater. -Laravel 5.3 is used as blade base. - - diff --git a/readme.txt b/readme.txt index acc49e3..7dae1b2 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: ekandreas Tags: Blade,templates,development,laravel Requires at least: 4.4 Tested up to: 4.8.1 -Stable tag: 1.7.1-dev +Stable tag: 1.7.5 License: MIT WordPress plugin for Blade L5 templating diff --git a/src/BladeCompiler.php b/src/BladeCompiler.php new file mode 100644 index 0000000..c824d43 --- /dev/null +++ b/src/BladeCompiler.php @@ -0,0 +1,17 @@ +app->singleton('blade.compiler', function () { + return new BladeCompiler( + $this->app['files'], $this->app['config']['view.compiled'] + ); + }); + + $resolver->register('blade', function () { + return new CompilerEngine($this->app['blade.compiler']); + }); + } } diff --git a/src/Config.php b/src/Config.php index 1fcb5b9..f0ed3ab 100644 --- a/src/Config.php +++ b/src/Config.php @@ -24,4 +24,41 @@ public static function repo($key = null, $default = null) } return Container::current('config')->get($key, $default); } + + /** + * Gets all valid paths to views and templates through the installation + * @return array + */ + public static function viewPaths() + { + $bladePaths = apply_filters('bladerunner/template/bladepath', get_stylesheet_directory()); + if (!is_array($bladePaths)) { + $bladePaths = [$bladePaths]; + } + + $stylesheetPath = get_stylesheet_directory(); + $templatePath = get_template_directory(); + + $bladePaths[] = $stylesheetPath . DIRECTORY_SEPARATOR . 'views'; + $bladePaths[] = $stylesheetPath . DIRECTORY_SEPARATOR . 'views'; + + $themePaths = [ + $stylesheetPath, + $stylesheetPath . DIRECTORY_SEPARATOR . 'templates', + $templatePath, + $templatePath . DIRECTORY_SEPARATOR . 'templates', + STYLESHEETPATH, // compability + STYLESHEETPATH . DIRECTORY_SEPARATOR . 'templates', + TEMPLATEPATH, // compability + TEMPLATEPATH . DIRECTORY_SEPARATOR . 'templates', + ]; + + $bladePaths = array_merge($bladePaths, $themePaths); + + $viewPaths = array_values( + collect($bladePaths, preg_replace('%[\/]?(templates)?[\/.]*?$%', '', $bladePaths))->unique()->toArray() + ); + + return $viewPaths; + } } diff --git a/src/Container.php b/src/Container.php index 1ba9ee0..9f0b8a3 100644 --- a/src/Container.php +++ b/src/Container.php @@ -10,13 +10,13 @@ class Container extends BaseContainer * * @param string $abstract * @param array $parameters - * @param ContainerContract $container - * @return ContainerContract|mixed + * @param Container $container + * @return Container|mixed * @SuppressWarnings(PHPMD.StaticAccess) */ - public static function current($abstract = null, $parameters = [], ContainerContract $container = null) + public static function current($abstract = null, $parameters = [], Container $container = null) { - $container = $container ?: \Bladerunner\Container::getInstance(); + $container = $container ?: Container::getInstance(); if (!$abstract) { return $container; } diff --git a/src/Controller.php b/src/Controller.php index 09e6201..1b7a346 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -118,6 +118,10 @@ public function __getData() public function __getView() { + if (isset($this->template) && $this->template) { + return $this->template; + } + if (isset($this->view) && $this->view) { return $this->view; } diff --git a/src/ControllerDebug.php b/src/ControllerDebug.php index 6477531..d7b038a 100755 --- a/src/ControllerDebug.php +++ b/src/ControllerDebug.php @@ -2,6 +2,8 @@ namespace Bladerunner; +use Brain\Hierarchy\Hierarchy; + class ControllerDebug { protected $data; @@ -55,7 +57,6 @@ public function data() */ public function controller() { - // unset($this->data['post']); echo "
Controller: