PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/laravel/public/phpMyAdmin/vendor/symfony/cache/Adapter/FilesystemAdapterTrait.php

https://bitbucket.org/dprograma/laravelweb
PHP | 110 lines | 91 code | 5 blank | 14 comment | 6 complexity | ae18c2b79a617d8fbb9a444673a1cd81 MD5 | raw file
Possible License(s): GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Adapter;
  11. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. *
  15. * @internal
  16. */
  17. trait FilesystemAdapterTrait
  18. {
  19. private $directory;
  20. private $tmp;
  21. private function init($namespace, $directory)
  22. {
  23. if (!isset($directory[0])) {
  24. $directory = sys_get_temp_dir().'/symfony-cache';
  25. }
  26. if (isset($namespace[0])) {
  27. if (preg_match('#[^-+_.A-Za-z0-9]#', $namespace, $match)) {
  28. throw new InvalidArgumentException(sprintf('Namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.', $match[0]));
  29. }
  30. $directory .= '/'.$namespace;
  31. }
  32. if (!file_exists($dir = $directory.'/.')) {
  33. @mkdir($directory, 0777, true);
  34. }
  35. if (false === $dir = realpath($dir) ?: (file_exists($dir) ? $dir : false)) {
  36. throw new InvalidArgumentException(sprintf('Cache directory does not exist (%s)', $directory));
  37. }
  38. $dir .= DIRECTORY_SEPARATOR;
  39. // On Windows the whole path is limited to 258 chars
  40. if ('\\' === DIRECTORY_SEPARATOR && strlen($dir) > 234) {
  41. throw new InvalidArgumentException(sprintf('Cache directory too long (%s)', $directory));
  42. }
  43. $this->directory = $dir;
  44. $this->tmp = $this->directory.uniqid('', true);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function doClear($namespace)
  50. {
  51. $ok = true;
  52. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS)) as $file) {
  53. $ok = ($file->isDir() || @unlink($file) || !file_exists($file)) && $ok;
  54. }
  55. return $ok;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. protected function doDelete(array $ids)
  61. {
  62. $ok = true;
  63. foreach ($ids as $id) {
  64. $file = $this->getFile($id);
  65. $ok = (!file_exists($file) || @unlink($file) || !file_exists($file)) && $ok;
  66. }
  67. return $ok;
  68. }
  69. private function write($file, $data, $expiresAt = null)
  70. {
  71. if (false === @file_put_contents($this->tmp, $data)) {
  72. return false;
  73. }
  74. if (null !== $expiresAt) {
  75. @touch($this->tmp, $expiresAt);
  76. }
  77. if (@rename($this->tmp, $file)) {
  78. return true;
  79. }
  80. @unlink($this->tmp);
  81. return false;
  82. }
  83. private function getFile($id, $mkdir = false)
  84. {
  85. $hash = str_replace('/', '-', base64_encode(hash('sha256', static::class.$id, true)));
  86. $dir = $this->directory.strtoupper($hash[0].DIRECTORY_SEPARATOR.$hash[1].DIRECTORY_SEPARATOR);
  87. if ($mkdir && !file_exists($dir)) {
  88. @mkdir($dir, 0777, true);
  89. }
  90. return $dir.substr($hash, 2, 20);
  91. }
  92. }