Completed
Push — master ( ef0404...81c807 )
by Craig
23:17 queued 14:56
created

Scanner::scan()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 6
nop 3
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
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 Zikula\Bundle\CoreBundle\Bundle;
13
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\Finder\SplFileInfo;
16
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
17
18
class Scanner
19
{
20
    private $jsons = [];
21
    private $invalid = [];
22
23
    /**
24
     * Scans and loads composer.json files.
25
     *
26
     * @param array $paths
27
     * @param int $depth
28
     * @param Finder $finder
29
     */
30
    public function scan(array $paths, $depth = 3, Finder $finder = null)
31
    {
32
        $paths = (array)$paths;
33
        $finder = null === $finder ? new Finder() : $finder;
34
        $finder->files()
35
            ->in($paths)
36
            ->notPath('docs')
37
            ->notPath('vendor')
38
            ->notPath('Resources')
39
            ->ignoreDotFiles(true)
40
            ->ignoreVCS(true)
41
            ->depth('<' . $depth)
42
            ->name('composer.json');
43
44
        /** @var $file SplFileInfo */
45
        foreach ($finder as $file) {
46
            $json = $this->decode($file->getRealPath());
47
            if (false !== $json) {
48
                $this->jsons[$json['name']] = $json;
49
            } else {
50
                $this->invalid[] = $file->getRelativePath();
51
            }
52
        }
53
    }
54
55
    /**
56
     * Decodes a json string.
57
     *
58
     * @param string $file Path to json file
59
     *
60
     * @return bool|mixed
61
     */
62
    public function decode($file)
63
    {
64
        $base = str_replace('\\', '/', dirname($file));
65
        $zkRoot = realpath(dirname(__FILE__) . '/../../../../../');
66
        $base = substr($base, strlen($zkRoot) + 1);
67
68
        $json = json_decode($this->getFileContents($file), true);
69
        if (\JSON_ERROR_NONE === json_last_error()) {
70
            // add base-path for future use
71
            $json['extra']['zikula']['base-path'] = $base;
72
73
            // calculate PSR-4 autoloading path for this namespace
74
            $class = $json['extra']['zikula']['class'];
75
            $ns = substr($class, 0, strrpos($class, '\\') + 1);
76
            if (false === isset($json['autoload']['psr-4'][$ns])) {
77
                return false;
78
            }
79
            $path = $json['extra']['zikula']['root-path'] = $base;
80
            $json['autoload']['psr-4'][$ns] = $path;
81
            $json['extra']['zikula']['short-name'] = substr($class, strrpos($class, '\\') + 1, strlen($class));
82
            $json['extensionType'] = ZikulaKernel::isCoreModule($json['extra']['zikula']['short-name']) ? MetaData::TYPE_SYSTEM : MetaData::TYPE_MODULE;
83
84
            return $json;
85
        }
86
87
        return false;
88
    }
89
90
    public function getFileContents($file)
91
    {
92
        return file_get_contents($file);
93
    }
94
95
    public function getModulesMetaData($indexByShortName = false)
96
    {
97
        return $this->getMetaData('zikula-module', $indexByShortName);
98
    }
99
100
    public function getThemesMetaData($indexByShortName = false)
101
    {
102
        return $this->getMetaData('zikula-theme', $indexByShortName);
103
    }
104
105
    private function getMetaData($type, $indexByShortName)
106
    {
107
        $array = [];
108
        foreach ($this->jsons as $json) {
109
            if ($json['type'] === $type) {
110
                $indexField = $indexByShortName ? $json['extra']['zikula']['short-name'] : $json['name'];
111
                $array[$indexField] = new MetaData($json);
112
            }
113
        }
114
115
        return $array;
116
    }
117
118
    public function getInvalid()
119
    {
120
        return $this->invalid;
121
    }
122
}
123