Completed
Push — 3.0 ( f23b72...fb09f5 )
by Daniel
04:42
created

PathTranslationParser::createPrefixVariants()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 12
1
<?php
2
3
namespace MagentoHackathon\Composer\Magento\Parser;
4
5
/**
6
 * Parser class supporting translating path mappings according to
7
 * the composer.json configuration.
8
 *
9
 * Class PathTranslationParser
10
 * @package MagentoHackathon\Composer\Magento\Parser
11
 */
12
class PathTranslationParser implements Parser
13
{
14
    /**
15
     * @var array Variants on each prefix that path mappings are checked
16
     * against.
17
     */
18
    protected $pathPrefixVariants = array('', './');
19
20
    /**
21
     * @var array Path mapping prefixes that need to be translated (i.e. to
22
     * use a public directory as the web server root).
23
     */
24
    protected $pathPrefixTranslations = array();
25
26
    /**
27
     * @var Parser
28
     */
29
    protected $parser;
30
31
    /**
32
     * Constructor. Sets the list of path translations to use.
33
     *
34
     * @param Parser $parser
35
     * @param array $translations Path translations
36
     */
37
    public function __construct(Parser $parser, array $translations)
38
    {
39
        $this->pathPrefixTranslations = $this->createPrefixVariants($translations);
40
        $this->parser = $parser;
41
    }
42
43
    /**
44
     * Given an array of path mapping translations, combine them with a list
45
     * of starting variations. This is so that a translation for 'js' will
46
     * also match path mappings beginning with './js'.
47
     *
48
     * @param $translations
49
     * @return array
50
     */
51
    protected function createPrefixVariants($translations)
52
    {
53
        $newTranslations = array();
54
        foreach ($translations as $key => $value) {
55
            foreach ($this->pathPrefixVariants as $variant) {
56
                $newTranslations[$variant . $key] = $value;
57
            }
58
        }
59
60
        return $newTranslations;
61
    }
62
63
    /**
64
     * loop the mappings for the wrapped parser, check if any of the targets are for
65
     * directories that have been moved under the public directory. If so,
66
     * update the target paths to include 'public/'. As no standard Magento
67
     * path mappings should ever start with 'public/', and  path mappings
68
     * that already include the public directory should always have
69
     * js/skin/media paths starting with 'public/', it should be safe to call
70
     * multiple times on either.
71
     *
72
     * @return array Updated path mappings
73
     */
74
    public function getMappings()
75
    {
76
        $translatedMappings = array();
77
        foreach ($this->parser->getMappings() as $index => $mapping) {
78
            $translatedMappings[$index] = $mapping;
79
            foreach ($this->pathPrefixTranslations as $prefix => $translate) {
80
                if (strpos($mapping[1], $prefix) === 0) {
81
                    // replace the old prefix with the translated version
82
                    $translatedMappings[$index][1] = $translate . substr($mapping[1], strlen($prefix));
83
                    // should never need to translate a prefix more than once
84
                    // per path mapping
85
                    break;
86
                }
87
            }
88
        }
89
90
        return $translatedMappings;
91
    }
92
}
93