• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gordalina / cachetool / 4672969473

pending completion
4672969473

push

github

GitHub
Merge pull request #229 from rodrigowbazevedo/v8.5.1

4 of 4 new or added lines in 4 files covered. (100.0%)

938 of 1027 relevant lines covered (91.33%)

12.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

16.13
/src/Command/OpcacheResetFileCacheCommand.php
1
<?php
2

3
/*
4
 * This file is part of CacheTool.
5
 *
6
 * (c) Samuel Gordalina <samuel.gordalina@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace CacheTool\Command;
13

14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ConfirmationQuestion;
18

19
class OpcacheResetFileCacheCommand extends AbstractOpcacheCommand
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('opcache:reset:file-cache')
28
            ->setDescription('Deletes all contents of the file cache directory')
29
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Delete files without questioning')
30
            ->setHelp('');
31
    }
32

33
    /**
34
     * {@inheritdoc}
35
     */
36
    protected function execute(InputInterface $input, OutputInterface $output): int
37
    {
38
        $this->ensureExtensionLoaded('Zend OPcache');
39
        $fileCache = $this->getCacheTool()->ini_get('opcache.file_cache');
40

41
        if (!is_dir($fileCache)) {
42
            throw new \RuntimeException('opcache.file_cache is not set or is not a directory.');
43
        }
44

45
        if (!$input->getOption('force')) {
46
            $question = new ConfirmationQuestion(
47
                "Are you sure you want to delete the contents of <comment>{$fileCache}</comment>? [no] ",
48
                false,
49
                '/^y/i'
50
            );
51

52
            $helper = $this->getHelper('question');
53
            $result = $helper->ask($input, $output, $question);
54

55
            if (!$result) {
56
                $output->writeln('<info>Aborted file deletion</info>');
57
                return 0;
58
            }
59
        }
60

61

62
        $deleted = $this->performDelete($fileCache);
63
        $output->writeln("<info>Deleted <comment>{$deleted}</comment> files.</info>");
64

65
        return 0;
66
    }
67

68
    /**
69
     * @param string $directory
70
     */
71
    protected function performDelete($directory) {
72
        $count = 0;
73
        $it = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS);
74
        $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
75

76
        foreach($files as $file) {
77
            if ($file->isDir()){
78
                $this->getCacheTool()->_eval("rmdir('{$file->getRealPath()}');");
79
            } else {
80
                $this->getCacheTool()->_eval("unlink('{$file->getRealPath()}');");
81
            }
82
            $count += 1;
83
        }
84

85
        return $count;
86
    }
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc