Skip to content

Commit 695330f

Browse files
authored
Laravel 7 and autoload blade custom directives (#59)
* 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.
1 parent 0551c23 commit 695330f

5 files changed

Lines changed: 46 additions & 8 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.netbeans/
2-
/bkp/
2+
/bkp/
3+
vendor/

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ In config\app.php, providers section:
7474
Config
7575
=======================
7676

77-
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.
77+
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.
78+
79+
`STRING_BLADE_CACHE_TIMEOUT=300`
80+
81+
Autoloading of blade custom directives can be changed in the config and env files.
82+
83+
`STRING_BLADE_AUTOLOAD=false`
7884

7985
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.
8086

config/blade.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// How many seconds past compiled file last modified time to recompile the template
55
// A value of 0 is always recompile
66
// Note: homestead time verses pc time may be off
7-
'secondsTemplateCacheExpires' => 300
7+
'secondsTemplateCacheExpires' => env('STRING_BLADE_CACHE_TIMEOUT', 300),
88

9+
// Determine whether the service provider to autoload blade custom directives.
10+
'autoload_custom_directives' => env('STRING_BLADE_AUTOLOAD', false),
911
];

src/Compilers/StringBladeCompiler.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,4 @@ public function isExpired($viewData)
132132

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

src/StringBladeServiceProvider.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?php
22
namespace Wpb\String_Blade_Compiler;
33

4-
use Illuminate\View\Engines\EngineResolver;
54
use Illuminate\View\FileViewFinder;
65
use Illuminate\View\ViewServiceProvider;
7-
use Wpb\String_Blade_Compiler\Compilers\StringBladeCompiler;
6+
use Illuminate\View\Engines\EngineResolver;
7+
use Illuminate\Contracts\Support\DeferrableProvider;
88
use Wpb\String_Blade_Compiler\Engines\CompilerEngine;
9+
use Wpb\String_Blade_Compiler\Compilers\StringBladeCompiler;
910

10-
class StringBladeServiceProvider extends ViewServiceProvider{
11+
class StringBladeServiceProvider extends ViewServiceProvider implements DeferrableProvider
12+
{
1113

1214
/**
1315
* Register the service provider.
@@ -148,4 +150,32 @@ public function registerStringBladeEngine($resolver)
148150
return new CompilerEngine($app['stringblade.compiler']);
149151
});
150152
}
151-
}
153+
154+
/**
155+
* Bootstrap any application services.
156+
*
157+
* @return void
158+
*/
159+
public function boot()
160+
{
161+
if(config('blade.autoload_custom_directives')) {
162+
$blade = app('blade.compiler');
163+
$string_blade = app('stringblade.compiler');
164+
165+
collect($blade->getCustomDirectives())
166+
->each(function($directive, $name) use ($string_blade) {
167+
$string_blade->directive($name, $directive);
168+
});
169+
}
170+
}
171+
172+
/**
173+
* Get the services provided by the provider.
174+
*
175+
* @return array
176+
*/
177+
public function provides()
178+
{
179+
return [StringBlade::class];
180+
}
181+
}

0 commit comments

Comments
 (0)