Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Tools / Console / Command / ClearCache / ResultCommand.php
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 Symfony\Component\Console\Input\InputArgument,
23     Symfony\Component\Console\Input\InputOption,
24     Symfony\Component\Console,
25     Doctrine\Common\Cache;
26
27 /**
28  * Command to clear the result cache of the various cache drivers.
29  *
30  *
31  * @link    www.doctrine-project.org
32  * @since   2.0
33  * @author  Benjamin Eberlei <kontakt@beberlei.de>
34  * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
35  * @author  Jonathan Wage <jonwage@gmail.com>
36  * @author  Roman Borschel <roman@code-factory.org>
37  */
38 class ResultCommand extends Console\Command\Command
39 {
40     /**
41      * @see Console\Command\Command
42      */
43     protected function configure()
44     {
45         $this
46         ->setName('orm:clear-cache:result')
47         ->setDescription('Clear all result cache of the various cache drivers.')
48         ->setDefinition(array(
49             new InputOption(
50                 'flush', null, InputOption::VALUE_NONE,
51                 'If defined, cache entries will be flushed instead of deleted/invalidated.'
52             )
53         ));
54
55         $this->setHelp(<<<EOT
56 The <info>%command.name%</info> command is meant to clear the result cache of associated Entity Manager.
57 It is possible to invalidate all cache entries at once - called delete -, or flushes the cache provider
58 instance completely.
59
60 The execution type differ on how you execute the command.
61 If you want to invalidate the entries (and not delete from cache instance), this command would do the work:
62
63 <info>%command.name%</info>
64
65 Alternatively, if you want to flush the cache provider using this command:
66
67 <info>%command.name% --flush</info>
68
69 Finally, be aware that if <info>--flush</info> option is passed, not all cache providers are able to flush entries,
70 because of a limitation of its execution nature.
71 EOT
72         );
73     }
74
75     /**
76      * @see Console\Command\Command
77      */
78     protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
79     {
80         $em = $this->getHelper('em')->getEntityManager();
81         $cacheDriver = $em->getConfiguration()->getResultCacheImpl();
82
83         if ( ! $cacheDriver) {
84             throw new \InvalidArgumentException('No Result cache driver is configured on given EntityManager.');
85         }
86
87         if ($cacheDriver instanceof Cache\ApcCache) {
88             throw new \LogicException("Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI.");
89         }
90
91         $output->write('Clearing ALL Result cache entries' . PHP_EOL);
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         $output->write($message . PHP_EOL);
102     }
103 }