Completed
Branch 09branch (ecf38f)
by Anton
04:23
created

Memory::getSections()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 13

Duplication

Lines 5
Ratio 20 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 6
nop 1
dl 5
loc 25
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Core;
10
11
use Spiral\Core\Exceptions\ScopeException;
12
use Spiral\Files\FilesInterface;
13
14
/**
15
 * Default implementation of MemoryInterface.
16
 */
17
class Memory implements MemoryInterface
18
{
19
    /**
20
     * Extension for memory files.
21
     */
22
    const EXTENSION = '.php';
23
24
    /**
25
     * Default memory location.
26
     *
27
     * @var string
28
     */
29
    private $directory = null;
30
31
    /**
32
     * Files are needed for write/read operations.
33
     *
34
     * @var FilesInterface
35
     */
36
    private $files = null;
37
38
    /**
39
     * @param string         $directory
40
     * @param FilesInterface $files
41
     *
42
     * @throws ScopeException
43
     */
44
    public function __construct(string $directory, FilesInterface $files)
45
    {
46
        $this->directory = $directory;
47
        $this->files = $files;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     * @param string $filename Cache filename.
54
     */
55
    public function loadData(string $section, string $location = null, string &$filename = null)
56
    {
57
        $filename = $this->memoryFilename($section, $location);
58
59
        if (!file_exists($filename)) {
60
            return null;
61
        }
62
63
        try {
64
            return include($filename);
65
        } catch (\ErrorException $exception) {
0 ignored issues
show
Bug introduced by
The class ErrorException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
66
            return null;
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function saveData(string $section, $data, string $location = null)
74
    {
75
        $filename = $this->memoryFilename($section, $location);
76
77
        //We are packing data into plain php
78
        $data = '<?php return ' . var_export($data, true) . ';';
79
80
        //We need help to write file with directory creation
81
        $this->files->write($filename, $data, FilesInterface::RUNTIME, true);
82
    }
83
84
    /**
85
     * Get extension to use for runtime data or configuration cache.
86
     *
87
     * @param string $name     Runtime data file name (without extension).
88
     * @param string $location Location to store data in.
89
     *
90
     * @return string
91
     */
92
    private function memoryFilename(string $name, string $location = null): string
93
    {
94
        $name = strtolower(str_replace(['/', '\\'], '-', $name));
95
96
        if (!empty($location)) {
97
            $location = $this->directory . $location . '/';
98
        } else {
99
            $location = $this->directory;
100
        }
101
102
        //Runtime cache
103
        return $location . $name . static::EXTENSION;
104
    }
105
}
106