-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[WIP] [2.2] [HttpKernel] added commands to clear, cleanup and introspect the HttpCache #6213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/Symfony/Bundle/FrameworkBundle/Command/AbstractHttpCacheCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
use Symfony\Component\HttpKernel\HttpCache\HttpCache; | ||
|
||
abstract 10000 class AbstractHttpCacheCommand extends ContainerAwareCommand | ||
{ | ||
/** | ||
* @return array | ||
*/ | ||
protected function getDefinitionArray() | ||
{ | ||
return array( | ||
new InputArgument('uri', InputArgument::OPTIONAL, 'A full uri, including the scheme, host, path and query string'), | ||
new InputOption('kernel_class_name', '', InputOption::VALUE_OPTIONAL, 'Name of the HttpCache kernel', 'AppCache'), | ||
); | ||
} | ||
|
||
/** | ||
* @return string cache dir | ||
* @throws \RuntimeException | ||
*/ | ||
protected function getCacheDir() | ||
{ | ||
$cacheDir = $this->getContainer()->getParameter('kernel.cache_dir').'/http_cache'; | ||
if (!is_readable($cacheDir)) { | ||
throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $cacheDir)); | ||
} | ||
|
||
return $cacheDir; | ||
} | ||
|
||
/** | ||
* @param \Symfony\Component\Console\Input\InputInterface $input | ||
* @param \Symfony\Component\HttpKernel\KernelInterface $kernel | ||
* @return \Symfony\Component\HttpKernel\HttpCache\HttpCache | ||
* @throws \RuntimeException | ||
*/ | ||
protected function getCacheKernel(InputInterface $input, KernelInterface $kernel) | ||
{ | ||
$kernelClassName = $input->getOption('kernel_class_name'); | ||
if (!class_exists($kernelClassName)) { | ||
throw new \RuntimeException(sprintf('Kernel class "%s" not loadable', $kernelClassName)); | ||
} | ||
|
||
$cacheKernel = new $kernelClassName($kernel); | ||
if (! $cacheKernel instanceof HttpCache) { | ||
throw new \RuntimeException(sprintf('Kernel ("%s") is not an instance of HttpCache', get_class($kernel))); | ||
} | ||
|
||
return $cacheKernel; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Symfony/Bundle/FrameworkBundle/Command/HttpCacheCleanupCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* TODO: this command is a bit all over the place | ||
* imho there should be a command for releasing locks and purging stale data | ||
* and it should be able to do this for all cache entries or just a specific uri | ||
* note sure if this should be two separate or just one command | ||
* also this will require further changes to Store/StoreInterface | ||
*/ | ||
class HttpCacheCleanupCommand extends AbstractHttpCacheCommand | ||
{ | ||
/** | ||
* @see Command | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('http:cache:cleanup') | ||
->setDefinition($this->getDefinitionArray()) | ||
->setDescription('Cleans up locked files in the http cache') | ||
->setHelp(<<<EOF | ||
The <info>%command.name%</info> command cleans up all locked entries in the http cache | ||
for a given environment and debug mode: | ||
|
||
<info>php %command.full_name% --env=dev</info> | ||
<info>php %command.full_name% --env=prod --no-debug</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$kernel = $this->getContainer()->get('kernel'); | ||
$cacheKernel = $this->getCacheKernel($input, $kernel); | ||
|
||
$uri = $input->getArgument('uri'); | ||
if (empty($uri)) { | ||
$output->writeln(sprintf('Cleaning up the http cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true))); | ||
$cacheKernel->getStore()->cleanup(); | ||
return 0; | ||
} | ||
|
||
$output->writeln(sprintf('Cleaning up the http cache for the uri <info>%s</info> and the <info>%s</info> environment with debug <info>%s</info>', $uri, $kernel->getEnvironment(), var_export($kernel->isDebug(), true))); | ||
|
||
$store = $cacheKernel->getStore(); | ||
$request = Request::create($uri); | ||
if ($store->isLocked($request)) { | ||
$output->writeln(sprintf('Removed lock for the uri <info>%s</info>', $uri)); | ||
$store->unlock($request); | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/Symfony/Bundle/FrameworkBundle/Command/HttpCacheClearCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class HttpCacheClearCommand extends AbstractHttpCacheCommand | ||
{ | ||
/** | ||
* @see Command | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('http:cache:clear') | ||
->setDefinition($this->getDefinitionArray()) | ||
->setDescription('Clears the http cache') | ||
->setHelp(<<<EOF | ||
The <info>%command.name%</info> command clears the application http cache for a given environment | ||
and debug mode: | ||
|
||
<info>php %command.full_name% --env=dev</info> | ||
<info>php %command.full_name% --env=prod --no-debug</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$kernel = $this->getContainer()->get('kernel'); | ||
$cacheKernel = $this->getCacheKernel($input, $kernel); | ||
|
||
$uri = $input->getArgument('uri'); | ||
if (empty($uri)) { | ||
$output->writeln(sprintf('Clearing the http cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true))); | ||
$filesystem = $this->getContainer()->get('filesystem'); | ||
$cacheDir = $this->getCacheDir(); | ||
$filesystem->remove($cacheDir); | ||
return 0; | ||
} | ||
|
||
$output->writeln(sprintf('Clearing the http cache for the uri <info>%s</info> and the <info>%s</info> environment with debug <info>%s</info>', $uri, $kernel->getEnvironment(), var_export($kernel->isDebug(), true))); | ||
|
||
$store = $cacheKernel->getStore(); | ||
$store->purge($uri); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Symfony/Bundle/FrameworkBundle/Command/HttpCacheInfoCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class HttpCacheInfoCommand extends AbstractHttpCacheCommand | ||
{ | ||
/** | ||
* @see Command | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('http:cache:info') | ||
->setDefinition($this->getDefinitionArray()) | ||
->setDescription('Provides information about entries in the http cache') | ||
->setHelp(<<<EOF | ||
The <info>%command.name%</info> command provides information about entries in the http cache | ||
for a given environment and debug mode: | ||
|
||
<info>php %command.full_name% --env=dev</info> | ||
<info>php %command.full_name% --env=prod --no-debug</info> | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$kernel = $this->getContainer()->get('kernel'); | ||
$cacheKernel = $this->getCacheKernel($input, $kernel); | ||
|
||
$uri = $input->getArgument('uri'); | ||
|
||
$output->writeln(sprintf('Reading information from the http cache for the uri <info>%s</info> and the <info>%s</info> environment with debug <info>%s</info>', $uri, $kernel->getEnvironment(), var_export($kernel->isDebug(), true))); | ||
|
||
$store = $cacheKernel->getStore(); | ||
$request = Request::create($uri); | ||
$txt = $store->isCached($request) ? ('cached'.($store->isLocked($request) ? ' and locked' : ' and unlocked')) : 'not cached'; | ||
$output->writeln(sprintf('Location (is %s): <info>%s</info>', $txt, $store->getLocation($request))); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,11 @@ public function isLocked(Request $request) | |
return is_file($this->getPath($this->getCacheKey($request).'.lck')); | ||
} | ||
|
||
public function isCached(Request $request) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should maybe be moved to a different interface? |
||
{ | ||
return is_file($this->getLocation($request)); | ||
} | ||
|
||
/** | ||
* Locates a cached Response for the Request provided. | ||
* | ||
|
@@ -361,6 +366,11 @@ private function save($key, $data) | |
@chmod($path, 0666 & ~umask()); | ||
} | ||
|
||
public function getLocation(Request $request) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should maybe be moved to a different interface? |
||
{ | ||
return $this->getPath($this->getCacheKey($request)); | ||
} | ||
|
||
public function getPath($key) | ||
{ | ||
return $this->root.DIRECTORY_SEPARATOR.substr($key, 0, 2).DIRECTORY_SEPARATOR.substr($key, 2, 2).DIRECTORY_SEPARATOR.substr($key, 4, 2).DIRECTORY_SEPARATOR.substr($key, 6); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this command needs more work