Completed
Push — master ( bd4c23...b341c0 )
by Filipe
10:29
created

ComposerReader::composePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Console\Command\Task\AskForNamespace;
11
12
/**
13
 * ComposerReader
14
 *
15
 * @package Slick\Mvc\Console\Command\Task\AskForNamespace
16
 * @author  Filipe Silva <[email protected]>
17
 */
18
class ComposerReader
19
{
20
    /**
21
     * @var NameSpaceCollection
22
     */
23
    private $collection;
24
25
    /**
26
     * @var object
27
     */
28
    private $data;
29
30
    /**
31
     * @var string
32
     */
33
    private $basePath;
34
35
    /**
36
     * Creates a composer reader
37
     *
38
     * @param string $composerFile
39
     */
40
    public function __construct($composerFile)
41
    {
42
        $this->read($composerFile);
43
        $this->collection = new NameSpaceCollection();
44
    }
45
46
    /**
47
     * Get the namespaces collection
48
     *
49
     * @return NameSpaceCollection|NameSpaceEntry[]
50
     */
51
    public function nameSpaces()
52
    {
53
        foreach ($this->data['autoload'] as $item) {
54
            $this->add($item);
55
        }
56
        foreach ($this->data['autoload-dev'] as $item) {
57
            $this->add($item);
58
        }
59
        return $this->collection;
60
    }
61
62
    /**
63
     * Read composer file
64
     *
65
     * @param string $composerFile
66
     */
67
    private function read($composerFile)
68
    {
69
        $this->basePath = dirname($composerFile);
70
        $this->data = json_decode(file_get_contents($composerFile), true);
71
    }
72
73
    /**
74
     * Adds a new item to the collection
75
     *
76
     * @param array $item
77
     */
78
    private function add(array $item)
79
    {
80
        foreach ($item as $nameSpace => $path) {
81
            $entry = new NameSpaceEntry($nameSpace, $this->composePath($path));
82
            $this->collection->offsetSet(null, $entry);
83
        }
84
    }
85
86
    /**
87
     * Set the base path to the namespace path
88
     *
89
     * @param string $path
90
     *
91
     * @return string
92
     */
93
    private function composePath($path)
94
    {
95
        return $this->basePath .'/'. $path;
96
    }
97
}