annotate vendor/doctrine/cache/lib/Doctrine/Common/Cache/PhpFileCache.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /*
Chris@0 3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@0 4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@0 5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@0 6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Chris@0 7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Chris@0 8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Chris@0 9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Chris@0 10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Chris@0 11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Chris@0 12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Chris@0 13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@0 14 *
Chris@0 15 * This software consists of voluntary contributions made by many individuals
Chris@0 16 * and is licensed under the MIT license. For more information, see
Chris@0 17 * <http://www.doctrine-project.org>.
Chris@0 18 */
Chris@0 19
Chris@0 20 namespace Doctrine\Common\Cache;
Chris@0 21
Chris@0 22 /**
Chris@0 23 * Php file cache driver.
Chris@0 24 *
Chris@0 25 * @since 2.3
Chris@0 26 * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
Chris@0 27 */
Chris@0 28 class PhpFileCache extends FileCache
Chris@0 29 {
Chris@0 30 const EXTENSION = '.doctrinecache.php';
Chris@0 31
Chris@0 32 /**
Chris@0 33 * {@inheritdoc}
Chris@0 34 */
Chris@0 35 public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
Chris@0 36 {
Chris@0 37 parent::__construct($directory, $extension, $umask);
Chris@0 38 }
Chris@0 39
Chris@0 40 /**
Chris@0 41 * {@inheritdoc}
Chris@0 42 */
Chris@0 43 protected function doFetch($id)
Chris@0 44 {
Chris@0 45 $value = $this->includeFileForId($id);
Chris@0 46
Chris@0 47 if (! $value) {
Chris@0 48 return false;
Chris@0 49 }
Chris@0 50
Chris@0 51 if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
Chris@0 52 return false;
Chris@0 53 }
Chris@0 54
Chris@0 55 return $value['data'];
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * {@inheritdoc}
Chris@0 60 */
Chris@0 61 protected function doContains($id)
Chris@0 62 {
Chris@0 63 $value = $this->includeFileForId($id);
Chris@0 64
Chris@0 65 if (! $value) {
Chris@0 66 return false;
Chris@0 67 }
Chris@0 68
Chris@0 69 return $value['lifetime'] === 0 || $value['lifetime'] > time();
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * {@inheritdoc}
Chris@0 74 */
Chris@0 75 protected function doSave($id, $data, $lifeTime = 0)
Chris@0 76 {
Chris@0 77 if ($lifeTime > 0) {
Chris@0 78 $lifeTime = time() + $lifeTime;
Chris@0 79 }
Chris@0 80
Chris@0 81 if (is_object($data) && ! method_exists($data, '__set_state')) {
Chris@0 82 throw new \InvalidArgumentException(
Chris@0 83 "Invalid argument given, PhpFileCache only allows objects that implement __set_state() " .
Chris@0 84 "and fully support var_export(). You can use the FilesystemCache to save arbitrary object " .
Chris@0 85 "graphs using serialize()/deserialize()."
Chris@0 86 );
Chris@0 87 }
Chris@0 88
Chris@0 89 $filename = $this->getFilename($id);
Chris@0 90
Chris@0 91 $value = array(
Chris@0 92 'lifetime' => $lifeTime,
Chris@0 93 'data' => $data
Chris@0 94 );
Chris@0 95
Chris@0 96 $value = var_export($value, true);
Chris@0 97 $code = sprintf('<?php return %s;', $value);
Chris@0 98
Chris@0 99 return $this->writeFile($filename, $code);
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * @param string $id
Chris@0 104 *
Chris@0 105 * @return array|false
Chris@0 106 */
Chris@0 107 private function includeFileForId($id)
Chris@0 108 {
Chris@0 109 $fileName = $this->getFilename($id);
Chris@0 110
Chris@0 111 // note: error suppression is still faster than `file_exists`, `is_file` and `is_readable`
Chris@0 112 $value = @include $fileName;
Chris@0 113
Chris@0 114 if (! isset($value['lifetime'])) {
Chris@0 115 return false;
Chris@0 116 }
Chris@0 117
Chris@0 118 return $value;
Chris@0 119 }
Chris@0 120 }