ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
EntriesYamlParser.php
Go to the documentation of this file.
1 <?php
2 /***
3  * @author Timon Amstutz <timon.amstutz@ilub.unibe.ch>
4  * @version $Id$
5  *
6  */
8 
11 
12 class EntriesYamlParser implements YamlParser
13 {
15  const PARSER_STATE_ENTRY = 2;
18 
22  protected $items = array();
23 
27  protected $ef = null;
28 
34  protected $file_path = "none";
35 
39  public function __construct(){
40  $this->ef = new Exception\Factory();
41  }
42 
48  public function parseYamlStringArrayFromFile($filePath){
49  $this->file_path = $filePath;
50  $content = $this->getFileContentAsString($filePath);
51  return $this->parseYamlStringArrayFromString($content);
52  }
53 
59  public function parseArrayFromFile($filePath){
60  $this->file_path = $filePath;
61  $content = $this->getFileContentAsString($filePath);
62  return $this->parseArrayFromString($content);
63  }
64 
70  public function parseEntriesFromFile($filePath){
71  $this->file_path = $filePath;
72  $content = $this->getFileContentAsString($filePath);
73  return $this->parseEntriesFromString($content);
74  }
75 
81  public function parseYamlStringArrayFromString($content){
82  return $this->getYamlEntriesFromString($content);
83  }
84 
90  public function parseArrayFromString($content){
91  return $this->getPHPArrayFromYamlArray(
92  $this->getYamlEntriesFromString($content)
93  );
94  }
95 
100  public function parseEntriesFromString($content){
101  $entries_array = $this->parseArrayFromString($content);
102  return $this->getEntriesFromArray($entries_array);
103  }
104 
110  protected function getFileContentAsString($filePath){
111  if ( !file_exists($filePath) ) {
112  throw $this->ef->exception(Exception\CrawlerException::INVALID_FILE_PATH,$filePath);
113  }
114  $content = file_get_contents($filePath);
115  if ( !$content ) {
116  throw $this->ef->exception(Exception\CrawlerException::FILE_OPENING_FAILED,$filePath);
117  }
118  return $content;
119  }
120 
126  protected function getYamlEntriesFromString($content){
127  $parser_state = self::PARSER_STATE_OUTSIDE;
128  $current_entry = "";
129  $yaml_entries = array();
130 
131  foreach(preg_split("/((\r?\n)|(\r\n?))/", $content) as $line){
132  if($parser_state === self::PARSER_STATE_OUTSIDE ){
133  if(preg_match('/---/', $line)) {
134  $current_entry = "";
135  $parser_state = self::PARSER_STATE_ENTRY;
136 
137  }
138  if(preg_match('/\@return/', $line)) {
139  throw $this->ef->exception(Exception\CrawlerException::ENTRY_WITH_NO_YAML_DESCRIPTION,
140  " in file: ".$this->file_path.", ".$line);
141  }
142  if(preg_match('/public function (.*)\(/', $line)) {
143  throw $this->ef->exception(Exception\CrawlerException::ENTRY_WITH_NO_YAML_DESCRIPTION,
144  " in file: ".$this->file_path.", ".$line);
145  }
146  }else if($parser_state === self::PARSER_STATE_ENTRY){
147  if(!preg_match('/(\*$)|(---)/', $line)){
148  $current_entry .= $this->purifyYamlLine($line);
149  }
150  if(preg_match('/---/', $line)) {
151  $parser_state = self::PARSER_STATE_SEEKING_RETURN;
152  }
153  if(preg_match('/\@return/', $line)) {
154  throw $this->ef->exception(Exception\CrawlerException::ENTRY_WITH_NO_YAML_DESCRIPTION,
155  " in file: ".$this->file_path.", ".$line);
156  }
157  if(preg_match('/public function (.*)\(/', $line)) {
158  throw $this->ef->exception(Exception\CrawlerException::ENTRY_WITH_NO_YAML_DESCRIPTION,
159  " in file: ".$this->file_path.", ".$line);
160  }
161  }else if($parser_state === self::PARSER_STATE_SEEKING_RETURN) {
162  if(preg_match('/\@return/', $line)) {
163  $current_entry .= "namespace: ".ltrim($this->purifyYamlLine($line),'@return');
164  $parser_state = self::PARSER_STATE_SEEKING_FUNCTION_NAME;
165  }
166  if(preg_match('/---/', $line)) {
167  throw $this->ef->exception(Exception\CrawlerException::ENTRY_WITH_NO_VALID_RETURN_STATEMENT,
168  " in file: ".$this->file_path." line ".$current_entry);
169  }
170  if(preg_match('/public function (.*)\(/', $line)) {
171  throw $this->ef->exception(Exception\CrawlerException::ENTRY_WITH_NO_VALID_RETURN_STATEMENT,
172  " in file: ".$this->file_path." line ".$current_entry);
173  }
174  }else{
175  if(preg_match('/public function (.*)\(/', $line,$matches)) {
176  preg_match('/public function (.*)\(/',$line, $matches);
177  $current_entry .= "function_name: ".$matches[1];
178  $yaml_entries[] = $current_entry;
179  $parser_state = self::PARSER_STATE_OUTSIDE;
180  }
181  if(preg_match('/---/', $line)) {
182  throw $this->ef->exception(Exception\CrawlerException::ENTRY_WITHOUT_FUNCTION,
183  " in file: ".$this->file_path." line ".$current_entry);
184  }
185  }
186 
187  }
188  if($parser_state === self::PARSER_STATE_SEEKING_RETURN ){
189  throw $this->ef->exception(Exception\CrawlerException::ENTRY_WITH_NO_VALID_RETURN_STATEMENT,
190  " in file: ".$this->file_path." line ".$current_entry);
191  }else if($parser_state === self::PARSER_STATE_ENTRY){
192  throw $this->ef->exception(Exception\CrawlerException::ENTRY_WITH_NO_YAML_DESCRIPTION,
193  " in file: ".$this->file_path);
194  }else if($parser_state === self::PARSER_STATE_SEEKING_FUNCTION_NAME){;
195  throw $this->ef->exception(Exception\CrawlerException::ENTRY_WITHOUT_FUNCTION,
196  " in file: ".$this->file_path);
197  }
198  return $yaml_entries;
199  }
200 
205  protected function purifyYamlLine($line){
206  return str_replace("* ", "", ltrim($line)).PHP_EOL;
207  }
208 
214  protected function getPHPArrayFromYamlArray($yaml_entries){
215  $entries = array();
216  $parser = new Yaml\Parser();
217 
218  foreach($yaml_entries as $yaml_entry){
219  try{
220  $entries[] = $parser->parse($yaml_entry);
221  }catch(\Exception $e){
222 
223  throw $this->ef->exception(Exception\CrawlerException::PARSING_YAML_ENTRY_FAILED," file: ".$this->file_path."; ".$e);
224  }
225  }
226 
227 
228  array_walk_recursive($entries, function(&$item){
229  $item = rtrim($item,PHP_EOL);
230  });
231 
232  return $entries;
233  }
234 
239  protected function getEntriesFromArray(array $entries_array){
240  $entries = new Entry\ComponentEntries();
241 
242  foreach($entries_array as $entry_data){
243  $entries->addEntry($this->getEntryFromData($entry_data));
244  }
245 
246  return $entries;
247  }
248 
254  protected function getEntryFromData(array $entry_data){
255 
256  $entry_data['title'] = self::fromCamelCaseToWords($entry_data['function_name']);
257 
258  if(!array_key_exists("title",$entry_data) || !$entry_data['title'] || $entry_data['title'] ==""){
259  throw $this->ef->exception(Exception\CrawlerException::ENTRY_TITLE_MISSING," File: ".$this->file_path);
260  }
261  if(!array_key_exists("namespace",$entry_data) || !$entry_data['namespace'] || $entry_data['namespace'] ==""){
262  throw $this->ef->exception(Exception\CrawlerException::ENTRY_WITH_NO_VALID_RETURN_STATEMENT," File: ".$this->file_path);
263  }
264 
265  $entry_data['id'] = str_replace("\\","",
266  str_replace("\\ILIAS\\UI\\","", str_replace("\\ILIAS\\UI\\Component\\","",$entry_data['namespace'])) )
267  .self::toUpperCamelCase($entry_data['title'], ' ');
268  $entry_data['abstract'] = preg_match("/Factory/",$entry_data['namespace']);
269  $entry_data['path'] = str_replace("/ILIAS","src",str_replace("\\","/",$entry_data['namespace']));
270 
271  try{
272  $entry = new Entry\ComponentEntry($entry_data);
273  }catch(\Exception $e){
274  throw $this->ef->exception(Exception\CrawlerException::PARSING_YAML_ENTRY_FAILED,
275  " could not convert data to entry, message: '".$e->getMessage()."' file: ".$this->file_path);
276  }
277 
278  return $entry;
279  }
280 
286  public static function toUpperCamelCase($string,$seperator){
287  return str_replace($seperator, '', ucwords($string));
288  }
289 
295  public static function toLowerCamelCase($string,$seperator){
296  return str_replace($seperator, '', lcfirst(ucwords($string)));
297  }
298 
303  public static function fromCamelCaseToWords($camelCaseString) {
304  return join(preg_split('/(?<=[a-z])(?=[A-Z])/x', ucwords($camelCaseString)), " " );
305  }
306 }
Container storing a list of UI Component Entries, can act as Iterator, countable and is serializable...
if($is_dev) echo "Review changes write something in WHATSNEW and and then commit with log PHP_EOL
Stores Information of UI Components parsed from YAML, examples and less files.
Create styles array
The data for the language used.
$parser
Definition: BPMN2Parser.php:24
Parses information from UI components.
Definition: YamlParser.php:10