Failed Conditions
Pull Request — master (#47)
by Mateusz
04:31
created

src/Cache/CacheManagerImpl.php (2 issues)

Labels
Severity

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
3
/*
4
 * This file is part of the puli/manager Module.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\Manager\Cache;
13
14
use Puli\Manager\Api\Cache\CacheFile;
15
use Puli\Manager\Api\Cache\CacheManager;
16
use Puli\Manager\Api\Config\Config;
17
use Puli\Manager\Api\Context\ProjectContext;
18
use Puli\Manager\Api\Module\Module;
19
use Puli\Manager\Api\Module\ModuleCollection;
20
use Puli\Manager\Api\Module\ModuleFile;
21
use Puli\Manager\Api\Module\ModuleManager;
22
use Puli\Manager\Api\Module\RootModule;
23
use Puli\Manager\Assert\Assert;
24
use Webmozart\Expression\Expr;
25
use Webmozart\Expression\Expression;
26
use Webmozart\PathUtil\Path;
27
28
/**
29
 * Manages cached Modules information.
30
 *
31
 * @since  1.0
32
 *
33
 * @author Mateusz Sojda <[email protected]>
34
 */
35
class CacheManagerImpl implements CacheManager
36
{
37
    /**
38
     * @var ModuleManager
39
     */
40
    private $moduleManager;
41
42
    /**
43
     * @var CacheFileStorage
44
     */
45
    private $storage;
46
47
    /**
48
     * @var ProjectContext
49
     */
50
    private $context;
51
52
    /**
53
     * @var string
54
     */
55
    private $rootDir;
56
57
    /**
58
     * Creates new cache manager.
59
     *
60
     * @param ModuleManager    $moduleManager  The Module manager.
61
     * @param CacheFileStorage  $storage         The file storage.
62
     * @param ProjectContext    $context         Project context.
63
     */
64 1
    public function __construct(ModuleManager $moduleManager, CacheFileStorage $storage, ProjectContext $context)
65
    {
66 1
        $this->ModuleManager = $moduleManager;
0 ignored issues
show
The property ModuleManager does not seem to exist. Did you mean moduleManager?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
67 1
        $this->storage = $storage;
68 1
        $this->context = $context;
69 1
        $this->rootDir = $context->getRootDirectory();
70 1
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function getContext()
76
    {
77 1
        return $this->context;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function getCacheFile()
84
    {
85
        $path = $this->getContext()->getConfig()->get(Config::CACHE_FILE);
86
        $path = Path::makeAbsolute($path, $this->rootDir);
87
88
        if (false === $this->storage->cacheFileExists($path)) {
89
            $this->refreshCacheFile();
90
        }
91
92
        $cacheFile = $this->storage->loadCacheFile($path);
93
94
        return $cacheFile;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function refreshCacheFile()
101
    {
102
        $path = $this->getContext()->getConfig()->get(Config::CACHE_FILE);
103
104
        if ($this->storage->cacheFileExists($path)) {
105
            return;
106
        }
107
108
        $cacheFile = new CacheFile(Path::makeAbsolute($path, $this->rootDir));
109
110
        foreach ($this->getInstalledModules() as $module) {
111
            $moduleFile = $module->getModuleFile();
112
            if (false === $moduleFile instanceof ModuleFile) {
113
                continue;
114
            }
115
116
            $cacheFile->addModuleFile($moduleFile);
117
118
            $installInfo = $module->getInstallInfo();
119
            $cacheFile->addInstallInfo($installInfo);
120
        }
121
122
        $this->storage->saveCacheFile($cacheFile);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getModule($name)
129
    {
130
        Assert::string($name, 'The Module name must be a string. Got: %s');
131
132
        $cacheFile = $this->getCacheFile();
133
        $moduleFile = $cacheFile->getModuleFile($name);
134
135
        return $this->buildModule($moduleFile, $cacheFile);
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function getRootModule()
142
    {
143
        return new RootModule(
144
            $this->getContext()->getRootModuleFile(),
145
            $this->rootDir
146
        );
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getModules()
153
    {
154
        $cacheFile = $this->getCacheFile();
155
        $modules = new ModuleCollection();
156
157
        foreach ($cacheFile->getModuleFiles() as $moduleFile) {
158
            $module = $this->buildModule($moduleFile, $cacheFile);
159
            $modules->add($module);
160
        }
161
162
        return $modules;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function findModules(Expression $expr)
169
    {
170
        $cacheFile = $this->getCacheFile();
171
        $modules = new ModuleCollection();
172
173
        foreach ($cacheFile->getModuleFiles() as $moduleFile) {
174
            $module = $this->buildModule($moduleFile, $cacheFile);
175
176
            if ($expr->evaluate($module)) {
177
                $modules->add($module);
178
            }
179
        }
180
181
        return $modules;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function hasModule($name)
188
    {
189
        Assert::string($name, 'The Module name must be a string. Got: %s');
190
191
        $cacheFile = $this->getCacheFile();
192
193
        return $cacheFile->hasModuleFile($name);
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function hasModules(Expression $expr = null)
200
    {
201
        $cacheFile = $this->getCacheFile();
202
203
        if (!$expr) {
204
            return $cacheFile->hasModuleFiles();
205
        }
206
207
        foreach ($this->getModules() as $module) {
208
            if ($expr->evaluate($module)) {
209
                return true;
210
            }
211
        }
212
213
        return false;
214
    }
215
216
    private function getInstalledModules()
217
    {
218
        $modules = $this->ModuleManager->findModules(Expr::true());
0 ignored issues
show
The property ModuleManager does not seem to exist. Did you mean moduleManager?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
219
220
        return $modules->getInstalledModules();
221
    }
222
223
    private function buildModule(ModuleFile $moduleFile, CacheFile $cacheFile)
224
    {
225
        $installInfo = $cacheFile->getInstallInfo($moduleFile->getModuleName());
226
        $installPath = Path::makeAbsolute($installInfo->getInstallPath(), $this->rootDir);
227
228
        return new Module($moduleFile, $installPath, $installInfo);
229
    }
230
}