Completed
Push — master ( ba3223...b47a39 )
by Luís
17s
created

Tools/Console/Command/ClearCache/QueryCommand.php (1 issue)

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Tools\Console\Command\ClearCache;
21
22
use Doctrine\Common\Cache\ApcCache;
23
use Doctrine\Common\Cache\XcacheCache;
24
use Symfony\Component\Console\Command\Command;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Symfony\Component\Console\Style\SymfonyStyle;
29
30
/**
31
 * Command to clear the query cache of the various cache drivers.
32
 *
33
 * @link    www.doctrine-project.org
34
 * @since   2.0
35
 * @author  Benjamin Eberlei <[email protected]>
36
 * @author  Guilherme Blanco <[email protected]>
37
 * @author  Jonathan Wage <[email protected]>
38
 * @author  Roman Borschel <[email protected]>
39
 */
40 View Code Duplication
class QueryCommand extends Command
0 ignored issues
show
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
{
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function configure()
46
    {
47
        $this->setName('orm:clear-cache:query')
48
             ->setDescription('Clear all query cache of the various cache drivers')
49
             ->addOption('flush', null, InputOption::VALUE_NONE, 'If defined, cache entries will be flushed instead of deleted/invalidated.')
50
             ->setHelp(<<<EOT
51
The <info>%command.name%</info> command is meant to clear the query cache of associated Entity Manager.
52
It is possible to invalidate all cache entries at once - called delete -, or flushes the cache provider
53
instance completely.
54
55
The execution type differ on how you execute the command.
56
If you want to invalidate the entries (and not delete from cache instance), this command would do the work:
57
58
<info>%command.name%</info>
59
60
Alternatively, if you want to flush the cache provider using this command:
61
62
<info>%command.name% --flush</info>
63
64
Finally, be aware that if <info>--flush</info> option is passed, not all cache providers are able to flush entries,
65
because of a limitation of its execution nature.
66
EOT
67
             );
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75
        $ui = new SymfonyStyle($input, $output);
76
77
        $em = $this->getHelper('em')->getEntityManager();
78
        $cacheDriver = $em->getConfiguration()->getQueryCacheImpl();
79
80
        if ( ! $cacheDriver) {
81
            throw new \InvalidArgumentException('No Query cache driver is configured on given EntityManager.');
82
        }
83
84
        if ($cacheDriver instanceof ApcCache) {
85
            throw new \LogicException("Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI.");
86
        }
87
        if ($cacheDriver instanceof XcacheCache) {
88
            throw new \LogicException("Cannot clear XCache Cache from Console, its shared in the Webserver memory and not accessible from the CLI.");
89
        }
90
91
        $ui->comment('Clearing <info>all</info> Query cache entries');
92
93
        $result  = $cacheDriver->deleteAll();
94
        $message = ($result) ? 'Successfully deleted cache entries.' : 'No cache entries were deleted.';
95
96
        if (true === $input->getOption('flush')) {
97
            $result  = $cacheDriver->flushAll();
98
            $message = ($result) ? 'Successfully flushed cache entries.' : $message;
99
        }
100
101
        if ( ! $result) {
102
            $ui->error($message);
103
104
            return 1;
105
        }
106
107
        $ui->success($message);
108
109
        return 0;
110
    }
111
}
112