Skip to content

Commit dae0de5

Browse files
committedMar 28, 2016
Merge pull request #18 from cariboufute/hotfix/fetching-blade-in-laravel-5
Changed way of fetching Blade instance from Laravel 5.x app
2 parents 3f913ab + 6bdfe34 commit dae0de5

File tree

2 files changed

+77
-83
lines changed

2 files changed

+77
-83
lines changed
 

Diff for: ‎readme.md

-4
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ After updating composer, add the ServiceProvider to the providers array in app/c
3535

3636
'Flynsarmy\DbBladeCompiler\DbBladeCompilerServiceProvider',
3737

38-
and the Facade to the aliases array in the same file
39-
40-
'DbView' => 'Flynsarmy\DbBladeCompiler\Facades\DbView',
41-
4238
You have to also publish the config-file
4339

4440
php artisan vendor:publish

Diff for: ‎src/Flynsarmy/DbBladeCompiler/DbBladeCompiler.php

+77-79
Original file line numberDiff line numberDiff line change
@@ -4,104 +4,102 @@
44
use Illuminate\View\Compilers\CompilerInterface;
55

66
class DbBladeCompiler extends BladeCompiler implements CompilerInterface
7-
{
7+
{
88

99
/** @var \Illuminate\Config\Repository */
1010
protected $config;
1111

1212
public function __construct($filesystem, $cache_path, $config, $app)
1313
{
14-
// Get Current Blade Instance
15-
$blade = $app->make('blade.compiler');
14+
// Get Current Blade Instance
15+
$blade = app('view')->getEngineResolver()->resolve('blade')->getCompiler();
1616

1717
parent::__construct($filesystem, $cache_path);
18-
$this->rawTags = $blade->getRawTags();
19-
$this->contentTags = $blade->getContentTags();
20-
$this->escapedTags = $blade->getEscapedContentTags();
21-
$this->extensions = $blade->getExtensions();
18+
$this->rawTags = $blade->getRawTags();
19+
$this->contentTags = $blade->getContentTags();
20+
$this->escapedTags = $blade->getEscapedContentTags();
21+
$this->extensions = $blade->getExtensions();
2222
$this->customDirectives = $blade->getCustomDirectives();
23-
$this->config = $config;
23+
$this->config = $config;
2424
}
2525

26-
/**
27-
* Compile the view at the given path.
28-
*
29-
* @param Illuminate\Database\Eloquent\Model $path
30-
* @return void
31-
*/
32-
public function compile($path)
33-
{
34-
// Defaults to '__db_blade_compiler_content_field' property
35-
$property = $this->config->get('db-blade-compiler.model_property');
36-
// Defaults to 'contents' column
37-
$column = $path->{$property};
38-
// Grab the column contents
39-
$string = $path->{$column};
40-
// Compile to PHP
41-
$contents = $this->compileString($string);
26+
/**
27+
* Compile the view at the given path.
28+
*
29+
* @param Illuminate\Database\Eloquent\Model $path
30+
* @return void
31+
*/
32+
public function compile($path)
33+
{
34+
// Defaults to '__db_blade_compiler_content_field' property
35+
$property = $this->config->get('db-blade-compiler.model_property');
36+
// Defaults to 'contents' column
37+
$column = $path->{$property};
38+
// Grab the column contents
39+
$string = $path->{$column};
40+
// Compile to PHP
41+
$contents = $this->compileString($string);
4242

43-
if ( ! is_null($this->cachePath))
44-
{
45-
$this->files->put($this->getCompiledPath($path), $contents);
46-
}
47-
}
43+
if (!is_null($this->cachePath)) {
44+
$this->files->put($this->getCompiledPath($path), $contents);
45+
}
46+
}
4847

49-
/**
50-
* Get the path to the compiled version of a view.
51-
*
52-
* @param Illuminate\Database\Eloquent\Model $model
53-
* @return string
54-
*/
55-
public function getCompiledPath($model)
56-
{
57-
/*
58-
* A unique path for the given model instance must be generated
59-
* so the view has a place to cache. The following generates a
60-
* path using almost the same logic as Blueprint::createIndexName()
61-
*
62-
* e.g db_table_name_id_4
63-
*/
64-
$field = $this->config->get('db-blade-compiler.model_property');
65-
$path = 'db_' . $model->getTable() . '_' . $model->{$field} . '_';
66-
if ( is_null($model->primaryKey) )
67-
$path .= $model->id;
68-
else if ( is_array($model->primaryKey) )
69-
$path .= implode('_', $model->primaryKey);
70-
else
71-
$path .= $model->primaryKey;
48+
/**
49+
* Get the path to the compiled version of a view.
50+
*
51+
* @param Illuminate\Database\Eloquent\Model $model
52+
* @return string
53+
*/
54+
public function getCompiledPath($model)
55+
{
56+
/*
57+
* A unique path for the given model instance must be generated
58+
* so the view has a place to cache. The following generates a
59+
* path using almost the same logic as Blueprint::createIndexName()
60+
*
61+
* e.g db_table_name_id_4
62+
*/
63+
$field = $this->config->get('db-blade-compiler.model_property');
64+
$path = 'db_' . $model->getTable() . '_' . $model->{$field} . '_';
65+
if (is_null($model->primaryKey)) {
66+
$path .= $model->id;
67+
} else if (is_array($model->primaryKey)) {
68+
$path .= implode('_', $model->primaryKey);
69+
} else {
70+
$path .= $model->primaryKey;
71+
}
7272

73-
$path = strtolower(str_replace(array('-', '.'), '_', $path));
73+
$path = strtolower(str_replace(array('-', '.'), '_', $path));
7474

75-
return $this->cachePath.'/'.md5($path);
76-
}
75+
return $this->cachePath . '/' . md5($path);
76+
}
7777

78-
/**
79-
* Determine if the view at the given path is expired.
80-
*
81-
* @param string $path
82-
* @return bool
83-
*/
84-
public function isExpired($path)
85-
{
86-
if(!$this->config->get('db-blade-compiler.cache'))
87-
{
88-
return true;
78+
/**
79+
* Determine if the view at the given path is expired.
80+
*
81+
* @param string $path
82+
* @return bool
83+
*/
84+
public function isExpired($path)
85+
{
86+
if (!$this->config->get('db-blade-compiler.cache')) {
87+
return true;
8988
}
90-
$compiled = $this->getCompiledPath($path);
89+
$compiled = $this->getCompiledPath($path);
9190

92-
// If the compiled file doesn't exist we will indicate that the view is expired
93-
// so that it can be re-compiled. Else, we will verify the last modification
94-
// of the views is less than the modification times of the compiled views.
95-
if ( ! $this->cachePath || ! $this->files->exists($compiled))
96-
{
97-
return true;
98-
}
91+
// If the compiled file doesn't exist we will indicate that the view is expired
92+
// so that it can be re-compiled. Else, we will verify the last modification
93+
// of the views is less than the modification times of the compiled views.
94+
if (!$this->cachePath || !$this->files->exists($compiled)) {
95+
return true;
96+
}
9997

100-
$lastModified = strtotime($path->updated_at);
98+
$lastModified = strtotime($path->updated_at);
10199

102-
// if ( $lastModified >= $this->files->lastModified($compiled) )
103-
// echo $lastModified . ' ('.date('r', $lastModified).') > ' . $this->files->lastModified($compiled) . ' ('.date('r', $this->files->lastModified($compiled)).')<br/>';
100+
// if ( $lastModified >= $this->files->lastModified($compiled) )
101+
// echo $lastModified . ' ('.date('r', $lastModified).') > ' . $this->files->lastModified($compiled) . ' ('.date('r', $this->files->lastModified($compiled)).')<br/>';
104102

105-
return $lastModified >= $this->files->lastModified($compiled);
106-
}
103+
return $lastModified >= $this->files->lastModified($compiled);
104+
}
107105
}

0 commit comments

Comments
 (0)
Please sign in to comment.