1 | <?php |
||||
2 | |||||
3 | namespace Kiroushi\DbBlade\Compilers; |
||||
4 | |||||
5 | use Illuminate\Database\Eloquent\Model; |
||||
6 | use Illuminate\View\Compilers\BladeCompiler; |
||||
7 | use Illuminate\View\Compilers\CompilerInterface; |
||||
8 | |||||
9 | class DbBladeCompiler extends BladeCompiler implements CompilerInterface |
||||
10 | { |
||||
11 | /** |
||||
12 | * Compile the view from the given model. |
||||
13 | * |
||||
14 | * @param Model $model |
||||
15 | * @return void |
||||
16 | */ |
||||
17 | public function compile($model = null) |
||||
18 | { |
||||
19 | if (is_null($model)) { |
||||
20 | return; |
||||
21 | } |
||||
22 | |||||
23 | // Defaults to '__db_blade_compiler_content_field' property |
||||
24 | $field = config('db-blade.model_property'); |
||||
25 | |||||
26 | $column = $model->{$field}; |
||||
27 | $content = $model->{$column}; |
||||
28 | |||||
29 | // Compile to PHP |
||||
30 | $contents = $this->compileString($content); |
||||
31 | |||||
32 | if (! is_null($this->cachePath)) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
33 | $this->files->put($this->getCompiledPath($model), $contents); |
||||
34 | } |
||||
35 | } |
||||
36 | |||||
37 | /** |
||||
38 | * Get the path to the compiled version of a view. |
||||
39 | * |
||||
40 | * @param Model $model |
||||
41 | * @return string |
||||
42 | */ |
||||
43 | public function getCompiledPath($model) |
||||
44 | { |
||||
45 | |||||
46 | /* |
||||
47 | * A unique path for the given model instance must be generated |
||||
48 | * so the view has a place to cache. The following generates a |
||||
49 | * path using almost the same logic as Blueprint::createIndexName() |
||||
50 | * |
||||
51 | * e.g db_table_name_id_4 |
||||
52 | */ |
||||
53 | $field = config('db-blade.model_property'); |
||||
54 | $path = 'db_'.$model->getTable().'_'.$model->{$field}.'_'; |
||||
55 | |||||
56 | if (is_null($model->primaryKey)) { |
||||
0 ignored issues
–
show
|
|||||
57 | $path .= $model->id; |
||||
58 | } elseif (is_array($model->primaryKey)) { |
||||
0 ignored issues
–
show
|
|||||
59 | $path .= implode('_', $model->primaryKey); |
||||
60 | } else { |
||||
61 | $path .= $model->primaryKey; |
||||
62 | } |
||||
63 | |||||
64 | $path = strtolower(str_replace(['-', '.'], '_', $path)); |
||||
65 | |||||
66 | return $this->cachePath.'/'.md5($path); |
||||
67 | } |
||||
68 | |||||
69 | /** |
||||
70 | * Determine if the view for the given model is expired. |
||||
71 | * |
||||
72 | * @param Model $model |
||||
73 | * @return bool |
||||
74 | */ |
||||
75 | public function isExpired($model) |
||||
76 | { |
||||
77 | if (! config('db-blade.cache')) { |
||||
78 | return true; |
||||
79 | } |
||||
80 | |||||
81 | $compiled = $this->getCompiledPath($model); |
||||
82 | |||||
83 | // If the compiled file doesn't exist we will indicate that the view is expired |
||||
84 | // so that it can be re-compiled. Else, we will verify the last modification |
||||
85 | // of the views is less than the modification times of the compiled views. |
||||
86 | if (! $this->cachePath || ! $this->files->exists($compiled)) { |
||||
87 | return true; |
||||
88 | } |
||||
89 | |||||
90 | $lastModified = strtotime($model->updated_at); |
||||
0 ignored issues
–
show
It seems like
$model->updated_at can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation ; however, parameter $time of strtotime() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
91 | |||||
92 | return $lastModified >= $this->files->lastModified($compiled); |
||||
93 | } |
||||
94 | } |
||||
95 |