Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ModmanParser.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  protected $_moduleDir = null;
17 
21  protected $_file = null;
22 
28  public function __construct($moduleDir = null, $translations = array(), $pathSuffix)
29  {
30  parent::__construct($translations, $pathSuffix);
31 
32  $this->setModuleDir($moduleDir);
33  $this->setFile($this->getModmanFile());
34  }
35 
42  public function setModuleDir($moduleDir)
43  {
44  // Remove trailing slash
45  if (!is_null($moduleDir)) {
46  $moduleDir = rtrim($moduleDir, '\\/');
47  }
48 
49  $this->_moduleDir = $moduleDir;
50  return $this;
51  }
52 
56  public function getModuleDir()
57  {
58  return $this->_moduleDir;
59  }
60 
65  public function setFile($file)
66  {
67  if (is_string($file)) {
68  $file = new \SplFileObject($file);
69  }
70  $this->_file = $file;
71  return $this;
72  }
73 
77  public function getFile()
78  {
79  return $this->_file;
80  }
81 
85  public function getModmanFile()
86  {
87  $file = null;
88  if (!is_null($this->_moduleDir)) {
89  $file = new \SplFileObject($this->_moduleDir . '/modman');
90  }
91  return $file;
92  }
93 
98  public function getMappings()
99  {
100  $file = $this->getFile();
101 
102  if (!$file->isReadable()) {
103  throw new \ErrorException(sprintf('modman file "%s" not readable', $file->getPathname()));
104  }
105 
106  $map = $this->_parseMappings();
107  $map = $this->translatePathMappings($map);
108  return $map;
109  }
110 
115  protected function _parseMappings()
116  {
117  $map = array();
118  $line = 0;
119 
120  foreach ($this->_file as $row) {
121  $line++;
122  $row = trim($row);
123  if ('' === $row || in_array($row[0], array('#', '@'))) {
124  continue;
125  }
126  $parts = preg_split('/\s+/', $row, 2, PREG_SPLIT_NO_EMPTY);
127  if (count($parts) != 2) {
128  throw new \ErrorException(sprintf('Invalid row on line %d has %d parts, expected 2', $line, count($row)));
129  }
130  $map[] = $parts;
131  }
132  return $map;
133  }
134 }
__construct($moduleDir=null, $translations=array(), $pathSuffix)