Passed
Push — master ( 429b06...f0c7c5 )
by Rafael
08:46
created

TypeAutoLoader::loadFromCacheCache()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
crap 12
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Type\Loader;
12
13
use GraphQL\Type\Definition\Type;
14
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
16
use Symfony\Component\Finder\Finder;
17
use Ynlo\GraphQLBundle\Type\Registry\TypeRegistry;
18
19
/**
20
 * Class TypeAutoLoader
21
 */
22
class TypeAutoLoader implements ContainerAwareInterface
23
{
24
    use ContainerAwareTrait;
25
26
    protected static $loaded = false;
27
28
    /**
29
     * @var string
30
     */
31 21
    private $cacheDir;
32
33 21
    /**
34 20
     * TypeAutoLoader constructor.
35
     *
36
     * @param string $cacheDir
37 1
     */
38 1
    public function __construct(string $cacheDir)
39
    {
40
        $this->cacheDir = $cacheDir;
41
    }
42 1
43 1
    /**
44 1
     * Autoload all registered types
45 1
     */
46 1
    public function autoloadTypes()
47 1
    {
48 1
        //loaded and static
49 1
        if (self::$loaded) {
50
            return;
51 1
        }
52 1
53 1
        if ($this->loadFromCacheCache()) {
54
            self::$loaded = true;
55 1
56
            return;
57
        }
58 1
59 1
        self::$loaded = true;
60 1
        $bundles = $this->container->get('kernel')->getBundles();
61 1
62 1
        foreach ($bundles as $bundle) {
63
            $path = $bundle->getPath().'/Type';
64 1
            if (file_exists($path)) {
65
                $finder = new Finder();
66
                foreach ($finder->in($path)->name('/Type.php$/')->getIterator() as $file) {
67
                    $namespace = $bundle->getNamespace();
68
                    $className = preg_replace('/.php$/', null, $file->getFilename());
69
                    $name = preg_replace('/Type$/', null, $className);
70 1
71
                    if ($file->getRelativePath()) {
72
                        $subNamespace = str_replace('/', '\\', $file->getRelativePath());
73
                        $fullyClassName = $namespace.'\\Type\\'.$subNamespace.'\\'.$className;
74
                    } else {
75
                        $fullyClassName = $namespace.'\\Type\\'.$className;
76
                    }
77
78
                    if (class_exists($fullyClassName)) {
79
                        $ref = new \ReflectionClass($fullyClassName);
80
                        if ($ref->isSubclassOf(Type::class)
81
                            && $ref->isInstantiable()
82
                            && !$ref->getConstructor()->getNumberOfRequiredParameters()
83
                        ) {
84
                            TypeRegistry::addTypeMapping($name, $fullyClassName);
85
                        }
86
                    }
87
                }
88
            }
89
        }
90
91
        $this->saveCache();
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    protected function cacheFileName()
98
    {
99
        return $this->cacheDir.DIRECTORY_SEPARATOR.'graphql.type_map.meta';
100
    }
101
102
    /**
103
     * Load cache
104
     *
105
     * @return bool on success
106
     */
107
    protected function loadFromCacheCache(): bool
108
    {
109
        if (file_exists($this->cacheFileName())) {
110
            $content = @file_get_contents($this->cacheFileName());
111
            if ($content) {
112
                TypeRegistry::setTypeMapping(unserialize($content));
113
114
                return true;
115
            }
116
        }
117
118
        return false;
119
    }
120
121
    /**
122
     * Save cache
123
     */
124
    protected function saveCache()
125
    {
126
        file_put_contents($this->cacheFileName(), serialize(TypeRegistry::getTypeMapp()));
127
    }
128
}
129