Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\View\Compilers;
4
 
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Str;
7
use InvalidArgumentException;
8
 
9
abstract class Compiler
10
{
11
    /**
12
     * The filesystem instance.
13
     *
14
     * @var \Illuminate\Filesystem\Filesystem
15
     */
16
    protected $files;
17
 
18
    /**
19
     * The cache path for the compiled views.
20
     *
21
     * @var string
22
     */
23
    protected $cachePath;
24
 
25
    /**
26
     * The base path that should be removed from paths before hashing.
27
     *
28
     * @var string
29
     */
30
    protected $basePath;
31
 
32
    /**
33
     * Determines if compiled views should be cached.
34
     *
35
     * @var bool
36
     */
37
    protected $shouldCache;
38
 
39
    /**
40
     * The compiled view file extension.
41
     *
42
     * @var string
43
     */
44
    protected $compiledExtension = 'php';
45
 
46
    /**
47
     * Create a new compiler instance.
48
     *
49
     * @param  \Illuminate\Filesystem\Filesystem  $files
50
     * @param  string  $cachePath
51
     * @param  string  $basePath
52
     * @param  bool  $shouldCache
53
     * @param  string  $compiledExtension
54
     * @return void
55
     *
56
     * @throws \InvalidArgumentException
57
     */
58
    public function __construct(
59
        Filesystem $files,
60
        $cachePath,
61
        $basePath = '',
62
        $shouldCache = true,
63
        $compiledExtension = 'php')
64
    {
65
        if (! $cachePath) {
66
            throw new InvalidArgumentException('Please provide a valid cache path.');
67
        }
68
 
69
        $this->files = $files;
70
        $this->cachePath = $cachePath;
71
        $this->basePath = $basePath;
72
        $this->shouldCache = $shouldCache;
73
        $this->compiledExtension = $compiledExtension;
74
    }
75
 
76
    /**
77
     * Get the path to the compiled version of a view.
78
     *
79
     * @param  string  $path
80
     * @return string
81
     */
82
    public function getCompiledPath($path)
83
    {
84
        return $this->cachePath.'/'.sha1('v2'.Str::after($path, $this->basePath)).'.'.$this->compiledExtension;
85
    }
86
 
87
    /**
88
     * Determine if the view at the given path is expired.
89
     *
90
     * @param  string  $path
91
     * @return bool
92
     */
93
    public function isExpired($path)
94
    {
95
        if (! $this->shouldCache) {
96
            return true;
97
        }
98
 
99
        $compiled = $this->getCompiledPath($path);
100
 
101
        // If the compiled file doesn't exist we will indicate that the view is expired
102
        // so that it can be re-compiled. Else, we will verify the last modification
103
        // of the views is less than the modification times of the compiled views.
104
        if (! $this->files->exists($compiled)) {
105
            return true;
106
        }
107
 
108
        return $this->files->lastModified($path) >=
109
               $this->files->lastModified($compiled);
110
    }
111
 
112
    /**
113
     * Create the compiled file directory if necessary.
114
     *
115
     * @param  string  $path
116
     * @return void
117
     */
118
    protected function ensureCompiledDirectoryExists($path)
119
    {
120
        if (! $this->files->exists(dirname($path))) {
121
            $this->files->makeDirectory(dirname($path), 0777, true, true);
122
        }
123
    }
124
}