Passed
Push — feature/custom-services-cache ( 09c1c5...2e1100 )
by Jesús
04:16
created

FileCacheBench::gacelaBootstrapWithCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 1
dl 0
loc 28
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GacelaTest\Benchmark\Framework\ClassResolver\FileCache;
6
7
use Gacela\Framework\Bootstrap\GacelaConfig;
8
use Gacela\Framework\Gacela;
9
use GacelaTest\Fixtures\StringValue;
10
use GacelaTest\Fixtures\StringValueInterface;
11
12
/**
13
 * @Revs(50)
14
 * @Iterations(2)
15
 */
16
final class FileCacheBench
17
{
18
    public function bench_with_cache(): void
19
    {
20
        $this->gacelaBootstrapWithCache(true);
21
        $this->loadAllModules();
22
    }
23
24
    public function bench_without_cache(): void
25
    {
26
        $this->gacelaBootstrapWithCache(false);
27
        $this->loadAllModules();
28
    }
29
30
    private function gacelaBootstrapWithCache(bool $withCache): void
31
    {
32
        Gacela::bootstrap(__DIR__, static function (GacelaConfig $config) use ($withCache): void {
33
            $config->addAppConfig('config/*.php');
34
            $config->setCacheEnabled($withCache);
35
36
            $config->addMappingInterface(StringValueInterface::class, new StringValue('testing-string'));
37
38
            $config->addSuffixTypeFactory('FactoryA');
39
            $config->addSuffixTypeFactory('FactoryB');
40
            $config->addSuffixTypeFactory('FactoryC');
41
            $config->addSuffixTypeFactory('FactoryD');
42
            $config->addSuffixTypeFactory('FactoryE');
43
44
            $config->addSuffixTypeConfig('ConfigA');
45
            $config->addSuffixTypeConfig('ConfigB');
46
            $config->addSuffixTypeConfig('ConfigC');
47
            $config->addSuffixTypeConfig('ConfigD');
48
            $config->addSuffixTypeConfig('ConfigE');
49
50
            $config->addSuffixTypeDependencyProvider('DepProvA');
51
            $config->addSuffixTypeDependencyProvider('DepProvB');
52
            $config->addSuffixTypeDependencyProvider('DepProvC');
53
            $config->addSuffixTypeDependencyProvider('DepProvD');
54
            $config->addSuffixTypeDependencyProvider('DepProvE');
55
        });
56
57
        $this->removeCacheFile($withCache);
58
    }
59
60
    private function removeCacheFile(bool $withCache): void
61
    {
62
        $cacheFilename = __DIR__ . '/' . '.gacela-class-names.cache';
63
        if (!$withCache && file_exists($cacheFilename)) {
64
            unlink($cacheFilename);
65
        }
66
    }
67
68
    private function loadAllModules(): void
69
    {
70
        (new ModuleA\Facade())->loadGacelaCacheFile();
71
        (new ModuleB\Facade())->loadGacelaCacheFile();
72
        (new ModuleC\Facade())->loadGacelaCacheFile();
73
        (new ModuleD\Facade())->loadGacelaCacheFile();
74
        (new ModuleE\Facade())->loadGacelaCacheFile();
75
        (new ModuleF\Facade())->loadGacelaCacheFile();
76
        (new ModuleG\Facade())->loadGacelaCacheFile();
77
    }
78
}
79