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\Cache\Console;
4
 
5
use Illuminate\Cache\CacheManager;
6
use Illuminate\Console\Command;
7
use Illuminate\Filesystem\Filesystem;
8
use Symfony\Component\Console\Attribute\AsCommand;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
 
12
#[AsCommand(name: 'cache:clear')]
13
class ClearCommand extends Command
14
{
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'cache:clear';
21
 
22
    /**
23
     * The name of the console command.
24
     *
25
     * This name is used to identify the command during lazy loading.
26
     *
27
     * @var string|null
28
     *
29
     * @deprecated
30
     */
31
    protected static $defaultName = 'cache:clear';
32
 
33
    /**
34
     * The console command description.
35
     *
36
     * @var string
37
     */
38
    protected $description = 'Flush the application cache';
39
 
40
    /**
41
     * The cache manager instance.
42
     *
43
     * @var \Illuminate\Cache\CacheManager
44
     */
45
    protected $cache;
46
 
47
    /**
48
     * The filesystem instance.
49
     *
50
     * @var \Illuminate\Filesystem\Filesystem
51
     */
52
    protected $files;
53
 
54
    /**
55
     * Create a new cache clear command instance.
56
     *
57
     * @param  \Illuminate\Cache\CacheManager  $cache
58
     * @param  \Illuminate\Filesystem\Filesystem  $files
59
     * @return void
60
     */
61
    public function __construct(CacheManager $cache, Filesystem $files)
62
    {
63
        parent::__construct();
64
 
65
        $this->cache = $cache;
66
        $this->files = $files;
67
    }
68
 
69
    /**
70
     * Execute the console command.
71
     *
72
     * @return void
73
     */
74
    public function handle()
75
    {
76
        $this->laravel['events']->dispatch(
77
            'cache:clearing', [$this->argument('store'), $this->tags()]
78
        );
79
 
80
        $successful = $this->cache()->flush();
81
 
82
        $this->flushFacades();
83
 
84
        if (! $successful) {
85
            return $this->components->error('Failed to clear cache. Make sure you have the appropriate permissions.');
86
        }
87
 
88
        $this->laravel['events']->dispatch(
89
            'cache:cleared', [$this->argument('store'), $this->tags()]
90
        );
91
 
92
        $this->components->info('Application cache cleared successfully.');
93
    }
94
 
95
    /**
96
     * Flush the real-time facades stored in the cache directory.
97
     *
98
     * @return void
99
     */
100
    public function flushFacades()
101
    {
102
        if (! $this->files->exists($storagePath = storage_path('framework/cache'))) {
103
            return;
104
        }
105
 
106
        foreach ($this->files->files($storagePath) as $file) {
107
            if (preg_match('/facade-.*\.php$/', $file)) {
108
                $this->files->delete($file);
109
            }
110
        }
111
    }
112
 
113
    /**
114
     * Get the cache instance for the command.
115
     *
116
     * @return \Illuminate\Cache\Repository
117
     */
118
    protected function cache()
119
    {
120
        $cache = $this->cache->store($this->argument('store'));
121
 
122
        return empty($this->tags()) ? $cache : $cache->tags($this->tags());
123
    }
124
 
125
    /**
126
     * Get the tags passed to the command.
127
     *
128
     * @return array
129
     */
130
    protected function tags()
131
    {
132
        return array_filter(explode(',', $this->option('tags') ?? ''));
133
    }
134
 
135
    /**
136
     * Get the console command arguments.
137
     *
138
     * @return array
139
     */
140
    protected function getArguments()
141
    {
142
        return [
143
            ['store', InputArgument::OPTIONAL, 'The name of the store you would like to clear'],
144
        ];
145
    }
146
 
147
    /**
148
     * Get the console command options.
149
     *
150
     * @return array
151
     */
152
    protected function getOptions()
153
    {
154
        return [
155
            ['tags', null, InputOption::VALUE_OPTIONAL, 'The cache tags you would like to clear', null],
156
        ];
157
    }
158
}