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