├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Brouwers │ └── Shortcodes │ │ ├── Compilers │ │ ├── Shortcode.php │ │ └── ShortcodeCompiler.php │ │ ├── Facades │ │ └── Shortcode.php │ │ ├── Illuminate │ │ └── View │ │ │ ├── Factory.php │ │ │ └── View.php │ │ ├── Shortcode.php │ │ └── ShortcodesServiceProvider.php └── config │ └── config.php └── tests ├── .gitkeep ├── TestCase.php ├── TestConfig.php ├── TestServiceProvider.php ├── TestShortcodeRegistration.php ├── TestViewCompiling.php └── helpers ├── BoldShortcode.php └── shortcode.blade.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | - hhvm 8 | 9 | before_script: 10 | - composer self-update 11 | - composer install --prefer-source --no-interaction --dev 12 | 13 | script: phpunit 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Patrick Brouwers 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of the {organization} nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Laravel-Shortcodes 2 | ================== 3 | 4 | Wordpress like shortcodes for Laravel 4.2. 5 | 6 | ```php 7 | [b class="bold"]Bold text[/b] 8 | 9 | [tabs] 10 | [tab]Tab 1[/tab] 11 | [tab]Tab 2[/tab] 12 | [/tabs] 13 | 14 | [user id="1" display="name"] 15 | ``` 16 | 17 | > If you are looking for BBcodes, see: https://github.com/patrickbrouwers/Laravel-BBcodes 18 | 19 | [![Build Status](https://travis-ci.org/patrickbrouwers/Laravel-Shortcodes.svg?branch=master)](https://travis-ci.org/patrickbrouwers/Laravel-Shortcodes) 20 | [![Latest Stable Version](https://poser.pugx.org/brouwers/shortcodes/v/stable.png)](https://packagist.org/packages/brouwers/shortcodes) [![Total Downloads](https://poser.pugx.org/brouwers/shortcodes/downloads.png)](https://packagist.org/packages/brouwers/shortcodes) [![License](https://poser.pugx.org/brouwers/shortcodes/license.png)](https://packagist.org/packages/brouwers/shortcodes) 21 | [![Monthly Downloads](https://poser.pugx.org/brouwers/shortcodes/d/monthly.png)](https://packagist.org/packages/brouwers/shortcodes) 22 | [![Daily Downloads](https://poser.pugx.org/brouwers/shortcodes/d/daily.png)](https://packagist.org/packages/brouwers/shortcodes) 23 | 24 | #Installation 25 | 26 | Require this package in your `composer.json` and update composer. 27 | 28 | ```php 29 | "brouwers/shortcodes": "1.*" 30 | ``` 31 | 32 | After updating composer, add the ServiceProvider to the providers array in `app/config/app.php` 33 | 34 | ```php 35 | 'Brouwers\Shortcodes\ShortcodesServiceProvider', 36 | ``` 37 | 38 | You can use the facade for shorter code. Add this to your aliases: 39 | 40 | ```php 41 | 'Shortcode' => 'Brouwers\Shortcodes\Facades\Shortcode', 42 | ``` 43 | 44 | The class is bound to the ioC as `shortcode` 45 | 46 | ```php 47 | $shortcode = App::make('shortcode'); 48 | ``` 49 | 50 | # Usage 51 | 52 | ## View compiling 53 | 54 | By default shortcode compiling is set to false inside the config. 55 | 56 | ### withShortcodes() 57 | 58 | To enable the view compiling features: 59 | 60 | ```php 61 | return View::make('view')->withShortcodes(); 62 | ``` 63 | 64 | This will enable shortcode rendering for that view only. 65 | 66 | ### Config 67 | 68 | Enabeling the shortcodes through config `shortcodes::enabled` will enable shortcoding rendering for all views. 69 | 70 | ### Enable through class 71 | 72 | ```php 73 | Shortcode::enable(); 74 | ``` 75 | 76 | ### Disable through class 77 | 78 | ```php 79 | Shortcode::disable(); 80 | ``` 81 | 82 | ### Disabeling some views from shortcode compiling 83 | 84 | With the config set to true, you can disable the compiling per view. 85 | 86 | ```php 87 | return View::make('view')->withoutShortcodes(); 88 | ``` 89 | 90 | ## Default compiling 91 | 92 | To use default compiling: 93 | 94 | ```php 95 | Shortcode::compile($contents); 96 | ``` 97 | 98 | ## Registering new shortcodes 99 | 100 | Inside a file or service provider you can register the shortcodes. (E.g. `app/start/shortcodes.php` or `App/Services/ShortcodeServiceProvider.php`) 101 | 102 | 103 | ### Callback 104 | 105 | Shortcodes can be registered like Laravel macro's with a callback: 106 | 107 | ```php 108 | Shortcode::register('b', function($shortcode, $content, $compiler, $name) 109 | { 110 | return '' . $content . ''; 111 | }); 112 | 113 | ``` 114 | 115 | ### Default class 116 | 117 | ```php 118 | class BoldShortcode { 119 | 120 | public function register($shortcode, $content, $compiler, $name) 121 | { 122 | return '' . $content . ''; 123 | } 124 | } 125 | 126 | Shortcode::register('b', 'BoldShortcode'); 127 | 128 | ``` 129 | 130 | ### Class with custom method 131 | 132 | ```php 133 | class BoldShortcode { 134 | 135 | public function custom($shortcode, $content, $compiler, $name) 136 | { 137 | return '' . $content . ''; 138 | } 139 | } 140 | 141 | Shortcode::register('b', 'BoldShortcode@custom'); 142 | 143 | ``` 144 | 145 | ### Register helpers 146 | 147 | If you only want to show the html attribute when the attribute is provided in the shortcode, you can use `$shortcode->get($attributeKey, $fallbackValue = null)` 148 | 149 | ```php 150 | class BoldShortcode { 151 | 152 | public function register($shortcode, $content, $compiler, $name) 153 | { 154 | return 'get('class', 'default') .'>' . $content . ''; 155 | } 156 | } 157 | 158 | 159 | ``` 160 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "brouwers/shortcodes", 3 | "description": "Wordpress like shortcodes for Laravel 4.2", 4 | "license": "BSD-3-Clause", 5 | "keywords": ["laravel", "wordpress", "shortcodes"], 6 | "authors": [ 7 | { 8 | "name": "Patrick Brouwers", 9 | "email": "patrickbrouwers@live.nl" 10 | } 11 | ], 12 | "require": { 13 | "php": ">=5.4.0" 14 | }, 15 | "require-dev": { 16 | "phpunit/phpunit": "~4.0", 17 | "mockery/mockery": "~0.9", 18 | "orchestra/testbench": "~2.2.0@dev" 19 | }, 20 | "autoload": { 21 | "classmap": [ 22 | "tests/TestCase.php" 23 | ], 24 | "psr-0": { 25 | "Brouwers\\Shortcodes": "src/" 26 | } 27 | }, 28 | "repositories": [ 29 | { 30 | "type": "vcs", 31 | "url": "git://github.com/orchestral/phpseclib.git" 32 | } 33 | ] 34 | } 35 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/Brouwers/Shortcodes/Compilers/Shortcode.php: -------------------------------------------------------------------------------- 1 | name = $name; 32 | $this->attributes = $attributes; 33 | $this->content = $content; 34 | } 35 | 36 | /** 37 | * Get html attribute 38 | * @param string $attribute 39 | * @return string|null 40 | */ 41 | public function get($attribute, $fallback = null) 42 | { 43 | $value = $this->{$attribute}; 44 | 45 | if(!is_null($value)) 46 | { 47 | return $attribute . '="' . $value . '"'; 48 | } 49 | elseif(!is_null($fallback)) 50 | { 51 | return $attribute . '="' . $fallback . '"'; 52 | } 53 | } 54 | 55 | /** 56 | * Get shortcode name 57 | * @return string 58 | */ 59 | public function getName() 60 | { 61 | return $this->name; 62 | } 63 | 64 | /** 65 | * Get shortcode attributes 66 | * @return string 67 | */ 68 | public function getContent() 69 | { 70 | return $this->content; 71 | } 72 | 73 | /** 74 | * Return array of attributes; 75 | * @return array 76 | */ 77 | public function toArray() 78 | { 79 | return $this->attributes; 80 | } 81 | 82 | /** 83 | * Dynamically get attributes 84 | * @param string $param 85 | * @return string|null 86 | */ 87 | public function __get($param) 88 | { 89 | return isset($this->attributes[$param]) ? $this->attributes[$param] : null; 90 | } 91 | 92 | } -------------------------------------------------------------------------------- /src/Brouwers/Shortcodes/Compilers/ShortcodeCompiler.php: -------------------------------------------------------------------------------- 1 | enabled = true; 26 | } 27 | 28 | /** 29 | * Disable 30 | * @return void 31 | */ 32 | public function disable() 33 | { 34 | $this->enabled = false; 35 | } 36 | 37 | /** 38 | * Add a new shortcode 39 | * @param string $name 40 | * @param callable|string $callback 41 | */ 42 | public function add($name, $callback) 43 | { 44 | $this->registered[$name] = $callback; 45 | } 46 | 47 | /** 48 | * Compile the contents 49 | * @param string $value 50 | * @return string 51 | */ 52 | public function compile($value) 53 | { 54 | // Only continue is shortcodes have been registered 55 | if(!$this->enabled || !$this->hasShortcodes()) 56 | return $value; 57 | 58 | // Set empty result 59 | $result = ''; 60 | 61 | // Here we will loop through all of the tokens returned by the Zend lexer and 62 | // parse each one into the corresponding valid PHP. We will then have this 63 | // template as the correctly rendered PHP that can be rendered natively. 64 | foreach (token_get_all($value) as $token) 65 | { 66 | $result .= is_array($token) ? $this->parseToken($token) : $token; 67 | } 68 | 69 | return $result; 70 | } 71 | 72 | /** 73 | * Check if shortcodes have been registered 74 | * @return boolean 75 | */ 76 | public function hasShortcodes() 77 | { 78 | return !empty($this->registered); 79 | } 80 | 81 | /** 82 | * Parse the tokens from the template. 83 | * @param array $token 84 | * @return string 85 | */ 86 | protected function parseToken($token) 87 | { 88 | list($id, $content) = $token; 89 | 90 | if ($id == T_INLINE_HTML) 91 | { 92 | $content = $this->renderShortcodes($content); 93 | } 94 | 95 | return $content; 96 | } 97 | 98 | /** 99 | * Render shortcodes 100 | * @param string $value 101 | * @return string 102 | */ 103 | protected function renderShortcodes($value) 104 | { 105 | return preg_replace_callback($this->getRegex(), array( 106 | &$this, 107 | 'render' 108 | ), $value); 109 | } 110 | 111 | /** 112 | * Render the current calld shortcode. 113 | * @param array $matches 114 | * @return string 115 | */ 116 | public function render($matches) 117 | { 118 | // Compile the shortcode 119 | $compiled = $this->compileShortcode($matches); 120 | $name = $compiled->getName(); 121 | 122 | // Render the shortcode through the callback 123 | return call_user_func_array($this->getCallback($name), array( 124 | $compiled, 125 | $compiled->getContent(), 126 | $this, 127 | $name 128 | )); 129 | } 130 | 131 | /** 132 | * Get Compiled Attributes. 133 | * @return \Brouwers\Shortcodes\Shortcode 134 | */ 135 | protected function compileShortcode($matches) 136 | { 137 | // Set matches 138 | $this->setMatches($matches); 139 | 140 | // pars the attributes 141 | $attributes = $this->parseAttributes($this->matches[3]); 142 | 143 | // return shortcode instance 144 | return new Shortcode( 145 | $this->getName(), 146 | $attributes, 147 | $this->getContent() 148 | ); 149 | } 150 | 151 | /** 152 | * Set the macthes 153 | * @param array $matches 154 | */ 155 | protected function setMatches($matches = array()) 156 | { 157 | $this->matches = $matches; 158 | } 159 | 160 | /** 161 | * Return the shortcode name 162 | * @return string 163 | */ 164 | public function getName() 165 | { 166 | return $this->matches[2]; 167 | } 168 | 169 | /** 170 | * Return the shortcode content 171 | * @return string 172 | */ 173 | public function getContent() 174 | { 175 | // Compile the content, to support nested shortcodes 176 | return $this->compile($this->matches[5]); 177 | } 178 | 179 | /** 180 | * Get the callback for the current shortcode (class or callback) 181 | * @param string $name 182 | * @return callable|array 183 | */ 184 | public function getCallback($name) 185 | { 186 | // Get the callback from the shortcodes array 187 | $callback = $this->registered[$name]; 188 | 189 | // if is a string 190 | if(is_string($callback)) 191 | { 192 | // Parse the callback 193 | list($class, $method) = Str::parseCallback($callback, 'register'); 194 | 195 | // If the class exist 196 | if(class_exists($class)) 197 | { 198 | // return class and method 199 | return array( 200 | app($class), 201 | $method 202 | ); 203 | } 204 | } 205 | 206 | return $callback; 207 | } 208 | 209 | /** 210 | * Parse the shortcode attributes 211 | * @author Wordpress 212 | * @return array 213 | */ 214 | protected function parseAttributes($text) 215 | { 216 | $attributes = array(); 217 | 218 | // attributes pattern 219 | $pattern = '/(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/'; 220 | 221 | // Match 222 | if(preg_match_all($pattern, preg_replace("/[\x{00a0}\x{200b}]+/u", " ", $text), $match, PREG_SET_ORDER)) 223 | { 224 | foreach ($match as $m) 225 | { 226 | if (!empty($m[1])) 227 | { 228 | $attributes[strtolower($m[1])] = stripcslashes($m[2]); 229 | } 230 | elseif (!empty($m[3])) 231 | { 232 | $attributes[strtolower($m[3])] = stripcslashes($m[4]); 233 | } 234 | elseif (!empty($m[5])) 235 | { 236 | $attributes[strtolower($m[5])] = stripcslashes($m[6]); 237 | } 238 | elseif (isset($m[7]) and strlen($m[7])) 239 | { 240 | $attributes[] = stripcslashes($m[7]); 241 | } 242 | elseif (isset($m[8])) 243 | { 244 | $attributes[] = stripcslashes($m[8]); 245 | } 246 | } 247 | } 248 | else 249 | { 250 | $attributes = ltrim($text); 251 | } 252 | 253 | // return attributes 254 | return is_array($attributes) ? $attributes : array($attributes); 255 | } 256 | 257 | /** 258 | * Get shortcode regex. 259 | * @author Wordpress 260 | * @return string 261 | */ 262 | protected function getRegex() 263 | { 264 | // Get shortcode names 265 | $shortcodeNames = $this->getShortcodeNames(); 266 | 267 | // return regex 268 | return "/" 269 | . '\\[' // Opening bracket 270 | . '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]] 271 | . "($shortcodeNames)" // 2: Shortcode name 272 | . '(?![\\w-])' // Not followed by word character or hyphen 273 | . '(' // 3: Unroll the loop: Inside the opening shortcode tag 274 | . '[^\\]\\/]*' // Not a closing bracket or forward slash 275 | . '(?:' 276 | . '\\/(?!\\])' // A forward slash not followed by a closing bracket 277 | . '[^\\]\\/]*' // Not a closing bracket or forward slash 278 | . ')*?' 279 | . ')' 280 | . '(?:' 281 | . '(\\/)' // 4: Self closing tag ... 282 | . '\\]' // ... and closing bracket 283 | . '|' 284 | . '\\]' // Closing bracket 285 | . '(?:' 286 | . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags 287 | . '[^\\[]*+' // Not an opening bracket 288 | . '(?:' 289 | . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag 290 | . '[^\\[]*+' // Not an opening bracket 291 | . ')*+' 292 | . ')' 293 | . '\\[\\/\\2\\]' // Closing shortcode tag 294 | . ')?' 295 | . ')' 296 | . '(\\]?)' // 6: Optional second closing brocket for escaping shortcodes: [[tag]] 297 | . "/s"; 298 | } 299 | 300 | /** 301 | * Get shortcode names 302 | * @return string 303 | */ 304 | protected function getShortcodeNames() 305 | { 306 | return join('|', array_map('preg_quote', array_keys($this->registered))); 307 | } 308 | } 309 | -------------------------------------------------------------------------------- /src/Brouwers/Shortcodes/Facades/Shortcode.php: -------------------------------------------------------------------------------- 1 | shortcode = $shortcode; 30 | } 31 | 32 | /** 33 | * Get the evaluated view contents for the given view. 34 | * 35 | * @param string $view 36 | * @param array $data 37 | * @param array $mergeData 38 | * @return \Illuminate\View\View 39 | */ 40 | public function make($view, $data = array(), $mergeData = array()) 41 | { 42 | if (isset($this->aliases[$view])) $view = $this->aliases[$view]; 43 | 44 | $path = $this->finder->find($view); 45 | 46 | $data = array_merge($mergeData, $this->parseData($data)); 47 | 48 | $this->callCreator($view = new View($this, $this->getEngineFromPath($path), $view, $path, $data, $this->shortcode)); 49 | 50 | return $view; 51 | } 52 | } -------------------------------------------------------------------------------- /src/Brouwers/Shortcodes/Illuminate/View/View.php: -------------------------------------------------------------------------------- 1 | shortcode = $shortcode; 34 | } 35 | 36 | /** 37 | * Enable the shortcodes 38 | * @return [type] [description] 39 | */ 40 | public function withShortcodes() 41 | { 42 | $this->shortcode->enable(); 43 | return $this; 44 | } 45 | 46 | /** 47 | * Disable the shortcodes 48 | * @return [type] [description] 49 | */ 50 | public function withoutShortcodes() 51 | { 52 | $this->shortcode->disable(); 53 | return $this; 54 | } 55 | 56 | /** 57 | * Get the contents of the view instance. 58 | * 59 | * @return string 60 | */ 61 | protected function renderContents() 62 | { 63 | // We will keep track of the amount of views being rendered so we can flush 64 | // the section after the complete rendering operation is done. This will 65 | // clear out the sections for any separate views that may be rendered. 66 | $this->factory->incrementRender(); 67 | 68 | $this->factory->callComposer($this); 69 | 70 | $contents = $this->getContents(); 71 | 72 | // compile the shortcodes 73 | $contents = $this->shortcode->compile($contents); 74 | 75 | // Once we've finished rendering the view, we'll decrement the render count 76 | // so that each sections get flushed out next time a view is created and 77 | // no old sections are staying around in the memory of an environment. 78 | $this->factory->decrementRender(); 79 | 80 | return $contents; 81 | } 82 | 83 | } -------------------------------------------------------------------------------- /src/Brouwers/Shortcodes/Shortcode.php: -------------------------------------------------------------------------------- 1 | compiler = $compiler; 20 | } 21 | 22 | /** 23 | * Register a new shortcode 24 | * @param string $name 25 | * @param callable|string $callback 26 | * @return \Brouwers\Shortcodes\Shortcode 27 | */ 28 | public function register($name, $callback) 29 | { 30 | $this->compiler->add($name, $callback); 31 | return $this; 32 | } 33 | 34 | /** 35 | * Enable the shortcodes 36 | * @return \Brouwers\Shortcodes\Shortcode 37 | */ 38 | public function enable() 39 | { 40 | $this->compiler->enable(); 41 | return $this; 42 | } 43 | 44 | /** 45 | * Disable the shortcodes 46 | * @return \Brouwers\Shortcodes\Shortcode 47 | */ 48 | public function disable() 49 | { 50 | $this->compiler->disable(); 51 | return $this; 52 | } 53 | 54 | /** 55 | * Compile the given string 56 | * @param string $value 57 | * @return string 58 | */ 59 | public function compile($value) 60 | { 61 | // Always enable when we call the compile method directly 62 | $this->enable(); 63 | 64 | // return compiled contents 65 | return $this->compiler->compile($value); 66 | } 67 | } -------------------------------------------------------------------------------- /src/Brouwers/Shortcodes/ShortcodesServiceProvider.php: -------------------------------------------------------------------------------- 1 | package('brouwers/shortcodes'); 18 | $this->enableCompiler(); 19 | } 20 | 21 | /** 22 | * Enable the compiler 23 | * @return [type] [description] 24 | */ 25 | public function enableCompiler() 26 | { 27 | // Check if the compiler is auto enabled 28 | $state = $this->app['config']->get('shortcodes::enabled', false); 29 | 30 | // enable when needed 31 | if($state) 32 | $this->app['shortcode.compiler']->enable(); 33 | } 34 | 35 | /** 36 | * Register the service provider. 37 | * 38 | * @return void 39 | */ 40 | public function register() 41 | { 42 | $this->registerShortcodeCompiler(); 43 | $this->registerShortcode(); 44 | $this->registerView(); 45 | } 46 | 47 | /** 48 | * Register short code compiler 49 | * @return [type] [description] 50 | */ 51 | public function registerShortcodeCompiler() 52 | { 53 | $this->app->bindShared('shortcode.compiler', function($app) 54 | { 55 | return new ShortcodeCompiler(); 56 | }); 57 | } 58 | 59 | /** 60 | * Register the shortcode 61 | * @return [type] [description] 62 | */ 63 | public function registerShortcode() 64 | { 65 | $this->app->bindShared('shortcode', function($app) { 66 | return new Shortcode($app['shortcode.compiler']); 67 | }); 68 | } 69 | 70 | /** 71 | * Register Laravel view 72 | * @return [type] [description] 73 | */ 74 | public function registerView() 75 | { 76 | $this->app->bindShared('view', function($app) 77 | { 78 | // Next we need to grab the engine resolver instance that will be used by the 79 | // environment. The resolver will be used by an environment to get each of 80 | // the various engine implementations such as plain PHP or Blade engine. 81 | $resolver = $app['view.engine.resolver']; 82 | 83 | $finder = $app['view.finder']; 84 | 85 | $env = new Factory($resolver, $finder, $app['events'], $app['shortcode.compiler']); 86 | 87 | // We will also set the container instance on this view environment since the 88 | // view composers may be classes registered in the container, which allows 89 | // for great testable, flexible composers for the application developer. 90 | $env->setContainer($app); 91 | 92 | $env->share('app', $app); 93 | 94 | return $env; 95 | }); 96 | } 97 | 98 | /** 99 | * Get the services provided by the provider. 100 | * 101 | * @return array 102 | */ 103 | public function provides() 104 | { 105 | return array( 106 | 'shortcode', 107 | 'shortcode.compiler', 108 | 'view' 109 | ); 110 | } 111 | } -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | false 14 | 15 | ); -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/patrickbrouwers/Laravel-Shortcodes/4cd580d655004715e0ab9f7018bd0028acb31e32/tests/.gitkeep -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf('Brouwers\Shortcodes\Shortcode', $shortcode); 12 | } 13 | 14 | protected function getPackageProviders() 15 | { 16 | return array('Brouwers\Shortcodes\ShortcodesServiceProvider'); 17 | } 18 | 19 | protected function getPackagePath() 20 | { 21 | return realpath(implode(DIRECTORY_SEPARATOR, array( 22 | __DIR__, 23 | '..', 24 | 'src', 25 | 'Brouwers', 26 | 'Shortcodes' 27 | ))); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /tests/TestConfig.php: -------------------------------------------------------------------------------- 1 | assertEquals(Config::get('shortcodes::enabled'), false); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /tests/TestServiceProvider.php: -------------------------------------------------------------------------------- 1 | assertCount(3, $provider->provides()); 26 | 27 | $this->assertContains('shortcode', $provider->provides()); 28 | $this->assertContains('shortcode.compiler', $provider->provides()); 29 | $this->assertContains('view', $provider->provides()); 30 | } 31 | } -------------------------------------------------------------------------------- /tests/TestShortcodeRegistration.php: -------------------------------------------------------------------------------- 1 | Test'; 20 | 21 | public function setUp() 22 | { 23 | parent::setUp(); 24 | $this->shortcode = app('shortcode'); 25 | } 26 | 27 | public function testInstance() 28 | { 29 | $this->assertInstanceOf('Brouwers\Shortcodes\Shortcode', $this->shortcode); 30 | } 31 | 32 | public function testCallbackRegistration() 33 | { 34 | // Register b tag with a callback 35 | $this->shortcode->register('b', function($shortcode, $content) 36 | { 37 | return '' . $content . ''; 38 | }); 39 | 40 | // Compile the string 41 | $compiled = $this->shortcode->compile($this->string); 42 | 43 | // Test 44 | $this->assertEquals($this->compiled, $compiled); 45 | } 46 | 47 | public function testClassRegistration() 48 | { 49 | // Register b tag with a callback 50 | $this->shortcode->register('b', 'BoldShortcode'); 51 | 52 | // Compile the string 53 | $compiled = $this->shortcode->compile($this->string); 54 | 55 | // Test 56 | $this->assertEquals($this->compiled, $compiled); 57 | } 58 | 59 | public function testCustomClassRegistration() 60 | { 61 | // Register b tag with a callback 62 | $this->shortcode->register('b', 'BoldShortcode@custom'); 63 | 64 | // Compile the string 65 | $compiled = $this->shortcode->compile($this->string); 66 | 67 | // Test 68 | $this->assertEquals($this->compiled, $compiled); 69 | } 70 | 71 | } -------------------------------------------------------------------------------- /tests/TestViewCompiling.php: -------------------------------------------------------------------------------- 1 | Test'; 20 | 21 | public function setUp() 22 | { 23 | parent::setUp(); 24 | $this->shortcode = app('shortcode'); 25 | 26 | // Register b tag with a callback 27 | $this->shortcode->register('b', 'BoldShortcode'); 28 | 29 | // Add view namespace 30 | View::addNamespace('shortcodeViews', __DIR__ . '/helpers'); 31 | 32 | // Share the string which should be render inside the view 33 | View::share('string', $this->string); 34 | } 35 | 36 | public function testNormalRenderWithConfigDisabledShortcodes() 37 | { 38 | $view = $this->makeView()->render(); 39 | 40 | // Nothing should have happended 41 | $this->assertEquals($this->string, $view); 42 | } 43 | 44 | public function testNormalRenderWithDisabledShortcodes() 45 | { 46 | // Disable 47 | $this->shortcode->disable(); 48 | 49 | $view = $this->makeView()->render(); 50 | 51 | // Nothing should have happended 52 | $this->assertEquals($this->string, $view); 53 | } 54 | 55 | public function testNormalRenderWithoutShortcodesEnabledThroughView() 56 | { 57 | // Render the view 58 | $view = $this->makeView() 59 | ->withoutShortcodes() 60 | ->render(); 61 | 62 | // Nothing should have happended 63 | $this->assertEquals($this->string, $view); 64 | } 65 | 66 | public function testNormalRenderWithEnabledShortcodes() 67 | { 68 | // Enable 69 | $this->shortcode->enable(); 70 | 71 | // Render the view 72 | $view = $this->makeView()->render(); 73 | 74 | // Nothing should have happended 75 | $this->assertEquals($this->compiled, $view); 76 | } 77 | 78 | public function testNormalRenderWithShortcodesEnabledThroughView() 79 | { 80 | // Render the view 81 | $view = $this->makeView() 82 | ->withShortcodes() 83 | ->render(); 84 | 85 | // Nothing should have happended 86 | $this->assertEquals($this->compiled, $view); 87 | } 88 | 89 | protected function makeView() 90 | { 91 | return View::make('shortcodeViews::shortcode'); 92 | } 93 | 94 | } -------------------------------------------------------------------------------- /tests/helpers/BoldShortcode.php: -------------------------------------------------------------------------------- 1 | class .'">' . $content . ''; 14 | } 15 | 16 | /** 17 | * Custom shortcode rendering 18 | * @param \Brouwers\Shortcode\Shortcode $shortcode 19 | * @param string $content 20 | * @return string 21 | */ 22 | public function custom($shortcode, $content) 23 | { 24 | return '' . $content . ''; 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /tests/helpers/shortcode.blade.php: -------------------------------------------------------------------------------- 1 | {{ $string }} --------------------------------------------------------------------------------