Skip to content

Commit

Permalink
Laravel 7 and autoload blade custom directives (#59)
Browse files Browse the repository at this point in the history
* Update for Laravel 7

* Autoload Blade custom directives.

- Added an option to the config to autoload custom directives.
- Added env variables to config.
- Updated README.
- Deferred StringBladeServiceProvider.
- Added `vendor/` to .gitignore.
  • Loading branch information
jpscharf committed Aug 31, 2020
1 parent 0551c23 commit 695330f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.netbeans/
/bkp/
/bkp/
vendor/
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,13 @@ In config\app.php, providers section:
Config
=======================

Default cache time for the compiled string template is 300 seconds (5 mins), this can be changed in the config file or when calling a view. The change is global to all string templates.
Default cache time for the compiled string template is 300 seconds (5 mins), this can be changed in the config file, env file, or when calling a view. The change is global to all string templates.

`STRING_BLADE_CACHE_TIMEOUT=300`

Autoloading of blade custom directives can be changed in the config and env files.

`STRING_BLADE_AUTOLOAD=false`

Note: If using homestead or some other vm, the host handles the filemtime of the cache file. This means the vm may have a different time than the file. If the cache is not expiring as expected, check the times between the systems.

Expand Down
4 changes: 3 additions & 1 deletion config/blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// How many seconds past compiled file last modified time to recompile the template
// A value of 0 is always recompile
// Note: homestead time verses pc time may be off
'secondsTemplateCacheExpires' => 300
'secondsTemplateCacheExpires' => env('STRING_BLADE_CACHE_TIMEOUT', 300),

// Determine whether the service provider to autoload blade custom directives.
'autoload_custom_directives' => env('STRING_BLADE_AUTOLOAD', false),
];
1 change: 0 additions & 1 deletion src/Compilers/StringBladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,4 @@ public function isExpired($viewData)

return time() >= ($this->files->lastModified($compiled) + $viewData->secondsTemplateCacheExpires) ;
}

}
38 changes: 34 additions & 4 deletions src/StringBladeServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php
namespace Wpb\String_Blade_Compiler;

use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\FileViewFinder;
use Illuminate\View\ViewServiceProvider;
use Wpb\String_Blade_Compiler\Compilers\StringBladeCompiler;
use Illuminate\View\Engines\EngineResolver;
use Illuminate\Contracts\Support\DeferrableProvider;
use Wpb\String_Blade_Compiler\Engines\CompilerEngine;
use Wpb\String_Blade_Compiler\Compilers\StringBladeCompiler;

class StringBladeServiceProvider extends ViewServiceProvider{
class StringBladeServiceProvider extends ViewServiceProvider implements DeferrableProvider
{

/**
* Register the service provider.
Expand Down Expand Up @@ -148,4 +150,32 @@ public function registerStringBladeEngine($resolver)
return new CompilerEngine($app['stringblade.compiler']);
});
}
}

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
if(config('blade.autoload_custom_directives')) {
$blade = app('blade.compiler');
$string_blade = app('stringblade.compiler');

collect($blade->getCustomDirectives())
->each(function($directive, $name) use ($string_blade) {
$string_blade->directive($name, $directive);
});
}
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [StringBlade::class];
}
}

0 comments on commit 695330f

Please sign in to comment.