Completed
Push — develop ( 8eb671...133594 )
by Mike
19:30 queued 09:24
created

Infrastructure/JmsSerializer/FileCache.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Infrastructure\JmsSerializer;
17
18
use Metadata\Cache\CacheInterface;
19
use Metadata\ClassMetadata;
20
21
class FileCache implements CacheInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    private $dir;
27
28
    /**
29
     * FileCache constructor.
30
     */
31
    public function __construct(string $dir)
32
    {
33
        if (!is_dir($dir)) {
34
            $this->createDirectory($dir);
35
        }
36
37
        $this->dir = rtrim($dir, '\\/');
38
    }
39
40
41
    /**
42
     * @param \ReflectionClass $class
43
     * @return ClassMetadata|null
44
     */
45
    public function loadClassMetadataFromCache(\ReflectionClass $class)
46
    {
47
        $path = $this->dir . '/' . strtr($class->name, '\\', '-') . '.cache.php';
48
        if (!file_exists($path)) {
49
            return null;
50
        }
51
52
        return include $path;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58
    public function putClassMetadataInCache(ClassMetadata $metadata)
59
    {
60
        $path = $this->getFileName($metadata);
61
62
        if (!is_writable(dirname($path))) {
63
            throw new \RuntimeException("Cache file {$path} is not writable.");
64
        }
65
66
        if (false === (@file_put_contents(
67
            $path,
68
            '<?php return unserialize(' . var_export(serialize($metadata), true) . ');'
69
        )
70
            )) {
71
            throw new \RuntimeException("Can't not write new cache file to {$path}");
72
        }
73
74
        // Let's not break filesystems which do not support chmod.
75
        @chmod($path, 0666 & ~umask());
0 ignored issues
show
Security Best Practice introduced by Jaapio
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    public function evictClassMetadataFromCache(\ReflectionClass $class)
82
    {
83
        $path = $this->dir . '/' . strtr($class->name, '\\', '-') . '.cache.php';
84
        if (file_exists($path)) {
85
            unlink($path);
86
        }
87
    }
88
89
    private function getFileName(ClassMetadata $metadata)
90
    {
91
        return $this->dir . '/' . strtr($metadata->name, '\\', '-') . '.cache.php';
92
    }
93
94
    private function createDirectory(string $dir)
95
    {
96
        if (!mkdir($dir, 0775, true) && !is_dir($dir)) {
97
            throw new \RuntimeException("Can't create directory for cache at {$dir}");
98
        }
99
    }
100
}
101