├── .php_cs.dist.php ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── blade-cache-directive.php └── src └── BladeCacheDirectiveServiceProvider.php /.php_cs.dist.php: -------------------------------------------------------------------------------- 1 | in([ 5 | __DIR__ . '/src', 6 | __DIR__ . '/tests', 7 | ]) 8 | ->name('*.php') 9 | ->notName('*.blade.php') 10 | ->ignoreDotFiles(true) 11 | ->ignoreVCS(true); 12 | 13 | return (new PhpCsFixer\Config()) 14 | ->setRules([ 15 | '@PSR2' => true, 16 | 'array_syntax' => ['syntax' => 'short'], 17 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 18 | 'no_unused_imports' => true, 19 | 'not_operator_with_successor_space' => true, 20 | 'trailing_comma_in_multiline' => true, 21 | 'phpdoc_scalar' => true, 22 | 'unary_operator_spaces' => true, 23 | 'binary_operator_spaces' => true, 24 | 'blank_line_before_statement' => [ 25 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 26 | ], 27 | 'phpdoc_single_line_var_spacing' => true, 28 | 'phpdoc_var_without_name' => true, 29 | 'class_attributes_separation' => [ 30 | 'elements' => [ 31 | 'method' => 'one', 32 | ], 33 | ], 34 | 'method_argument_space' => [ 35 | 'on_multiline' => 'ensure_fully_multiline', 36 | 'keep_multiple_spaces_after_comma' => true, 37 | ], 38 | 'single_trait_insert_per_statement' => true, 39 | ]) 40 | ->setFinder($finder); 41 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `blade-cache-directive` will be documented in this file. 4 | 5 | ## 0.1.0 - 2021-05-22 6 | 7 | - Initial release. 🎉 8 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) ryangjchandler 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Blade Cache Directive 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/ryangjchandler/blade-cache-directive.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/blade-cache-directive) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/ryangjchandler/blade-cache-directive.svg?style=flat-square)](https://packagist.org/packages/ryangjchandler/blade-cache-directive) 5 | 6 | Cache chunks of your Blade markup with ease. 7 | 8 | ## Installation 9 | 10 | You can install the package via Composer: 11 | 12 | ```bash 13 | composer require ryangjchandler/blade-cache-directive 14 | ``` 15 | 16 | You can publish the config file with: 17 | ```bash 18 | php artisan vendor:publish --provider="RyanChandler\BladeCacheDirective\BladeCacheDirectiveServiceProvider" --tag="blade-cache-directive-config" 19 | ``` 20 | 21 | This is the contents of the published config file: 22 | 23 | ```php 24 | return [ 25 | 26 | 'enabled' => env('BLADE_CACHE_DIRECTIVE_ENABLED', true), 27 | 28 | 'ttl' => env('BLADE_CACHE_DIRECTIVE_TTL', 3600), 29 | 30 | ]; 31 | ``` 32 | 33 | ## Usage 34 | 35 | This package adds a new `@cache` Blade directive. It accepts 2 arguments - the cache key and a TTL. 36 | 37 | ```blade 38 | @cache('current_time', 30) 39 | {{ now() }} 40 | @endcache 41 | ``` 42 | 43 | When used inside of a Blade template, the content between the 2 directives will be cached using Laravel's application cache. If a TTL (in seconds) isn't provided, the default TTL of **1 hour** will be used instead. 44 | 45 | If you want to cache the content for a particular model, i.e. a `User` model, you can use string interpolation to change the key. 46 | 47 | ```blade 48 | @cache("user_profile_{$user->id}") 49 | {{ $user->name }} 50 | @endcache 51 | ``` 52 | 53 | When a new user is passed to this view, a separate cache entry will be created. 54 | 55 | ### Disabling caching 56 | 57 | If you wish to disable caching when using the `@cache` directive (useful for local development and testing), you can set the `BLADE_CACHE_DIRECTIVE_ENABLED` environment variable to `false`. 58 | 59 | Alternatively, publish the configuration file and modify the `enabled` entry accordingly. 60 | 61 | ## Testing 62 | 63 | ```bash 64 | composer test 65 | ``` 66 | 67 | ## Changelog 68 | 69 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 70 | 71 | ## Contributing 72 | 73 | Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. 74 | 75 | ## Security Vulnerabilities 76 | 77 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 78 | 79 | ## Credits 80 | 81 | - [Ryan Chandler](https://github.com/ryangjchandler) 82 | - [All Contributors](../../contributors) 83 | 84 | ## License 85 | 86 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 87 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ryangjchandler/blade-cache-directive", 3 | "description": "Cache chunks of your Blade markup with ease.", 4 | "keywords": [ 5 | "ryangjchandler", 6 | "laravel", 7 | "blade-cache-directive" 8 | ], 9 | "homepage": "https://github.com/ryangjchandler/blade-cache-directive", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Ryan Chandler", 14 | "email": "support@ryangjchandler.co.uk", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.2", 20 | "spatie/laravel-package-tools": "^1.16", 21 | "illuminate/contracts": "^11.0|^12.0" 22 | }, 23 | "require-dev": { 24 | "brianium/paratest": "^7.4", 25 | "laravel/pint": "^1.21", 26 | "nunomaduro/collision": "^8.0", 27 | "orchestra/testbench": "^9.0|^10.0", 28 | "phpunit/phpunit": "^10.0|^11.5.3", 29 | "spatie/laravel-ray": "^1.9", 30 | "vimeo/psalm": "^4.4|^5.22|^6.5" 31 | }, 32 | "autoload": { 33 | "psr-4": { 34 | "RyanChandler\\BladeCacheDirective\\": "src", 35 | "RyanChandler\\BladeCacheDirective\\Database\\Factories\\": "database/factories" 36 | } 37 | }, 38 | "autoload-dev": { 39 | "psr-4": { 40 | "RyanChandler\\BladeCacheDirective\\Tests\\": "tests" 41 | } 42 | }, 43 | "scripts": { 44 | "psalm": "vendor/bin/psalm", 45 | "test": "./vendor/bin/testbench package:test --parallel --no-coverage", 46 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 47 | }, 48 | "config": { 49 | "sort-packages": true 50 | }, 51 | "extra": { 52 | "laravel": { 53 | "providers": [ 54 | "RyanChandler\\BladeCacheDirective\\BladeCacheDirectiveServiceProvider" 55 | ], 56 | "aliases": { 57 | "BladeCacheDirective": "RyanChandler\\BladeCacheDirective\\BladeCacheDirectiveFacade" 58 | } 59 | } 60 | }, 61 | "minimum-stability": "dev", 62 | "prefer-stable": true 63 | } 64 | -------------------------------------------------------------------------------- /config/blade-cache-directive.php: -------------------------------------------------------------------------------- 1 | env('BLADE_CACHE_DIRECTIVE_ENABLED', true), 6 | 7 | 'ttl' => env('BLADE_CACHE_DIRECTIVE_TTL', 3600), 8 | 9 | ]; 10 | -------------------------------------------------------------------------------- /src/BladeCacheDirectiveServiceProvider.php: -------------------------------------------------------------------------------- 1 | name('blade-cache-directive') 15 | ->hasConfigFile(); 16 | } 17 | 18 | public function packageBooted() 19 | { 20 | Blade::directive('cache', function ($expression) { 21 | return ""; 38 | }); 39 | 40 | Blade::directive('endcache', function () { 41 | return ""; 51 | }); 52 | } 53 | } 54 | --------------------------------------------------------------------------------