PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/classes/Mibew/Style/AbstractStyle.php

https://gitlab.com/fabiorf/chat
PHP | 159 lines | 56 code | 19 blank | 84 comment | 8 complexity | 69887326c38e460e50934163678c9359 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is a part of Mibew Messenger.
  4. *
  5. * Copyright 2005-2015 the original author or authors.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. namespace Mibew\Style;
  20. use Symfony\Component\Yaml\Parser as YamlParser;
  21. /**
  22. * Base class for styles
  23. */
  24. abstract class AbstractStyle
  25. {
  26. /**
  27. * Styles configuration array or NULL by default
  28. *
  29. * @var array|NULL
  30. */
  31. protected $cachedConfigurations = null;
  32. /**
  33. * This value is used to store name of a style
  34. *
  35. * @var string
  36. */
  37. protected $styleName;
  38. /**
  39. * An instance of a parser for config files.
  40. *
  41. * @var YamlParser|null
  42. */
  43. protected $configParser = null;
  44. /**
  45. * Contains cached results of the \Mibew\Style\StyleInterface::getStyleList
  46. * method. The lists are keyed by the $root_dir argument of the method.
  47. *
  48. * @var array
  49. * @see \Mibew\Style\StyleInterface::getStyleList
  50. */
  51. protected static $cachedStyleLists = array();
  52. /**
  53. * Object constructor
  54. *
  55. * @param string $style_name Name of the style
  56. */
  57. public function __construct($style_name)
  58. {
  59. $this->styleName = $style_name;
  60. }
  61. /**
  62. * Returns name of the style related with the object
  63. *
  64. * @return string Name of the style
  65. */
  66. public function getName()
  67. {
  68. return $this->styleName;
  69. }
  70. /**
  71. * Loads configurations of the style. The results is cached in the class
  72. * instance.
  73. *
  74. * @return array Style configurations
  75. * @throws \RuntimeException
  76. */
  77. public function getConfigurations()
  78. {
  79. $config_file = MIBEW_FS_ROOT . '/' . $this->getFilesPath() . '/config.yml';
  80. // Check if configurations already loaded. Do not do the job twice.
  81. if (is_null($this->cachedConfigurations)) {
  82. // Set empty value for configuration array
  83. $this->cachedConfigurations = array();
  84. // Try to read configuration file
  85. if (!is_readable($config_file)) {
  86. throw new \RuntimeException('Cannot read configuration file');
  87. }
  88. // Load configurations from file, merge it with default configs and
  89. // cache the result.
  90. $loaded_config = $this->getConfigParser()->parse(file_get_contents($config_file));
  91. $default_config = $this->getDefaultConfigurations();
  92. $this->cachedConfigurations = array_replace_recursive($default_config, $loaded_config);
  93. }
  94. return $this->cachedConfigurations;
  95. }
  96. /**
  97. * Returns a parser which is sutable for parse config files of the style.
  98. *
  99. * @return YamlParser
  100. */
  101. protected function getConfigParser()
  102. {
  103. if (is_null($this->configParser)) {
  104. $this->configParser = new YamlParser();
  105. }
  106. return $this->configParser;
  107. }
  108. /**
  109. * Gets names of styles which are located in the $root_dir.
  110. *
  111. * @param string $root_dir Root styles directory
  112. * @return array List of styles' names
  113. */
  114. protected static function getStyleList($root_dir)
  115. {
  116. // Check if styles list is already stored in the cache
  117. if (!isset(self::$cachedStyleLists[$root_dir])) {
  118. // Build list of styles for the specified root directory.
  119. $style_list = array();
  120. if ($handle = opendir($root_dir)) {
  121. while (false !== ($file = readdir($handle))) {
  122. if (preg_match("/^\w+$/", $file) && is_dir("$root_dir/$file")) {
  123. $style_list[$file] = $file;
  124. }
  125. }
  126. closedir($handle);
  127. }
  128. // Cache the list
  129. self::$cachedStyleLists[$root_dir] = $style_list;
  130. }
  131. return self::$cachedStyleLists[$root_dir];
  132. }
  133. /**
  134. * Returns array of default configurations for concrete style object. This
  135. * method uses "Template method" design pattern.
  136. *
  137. * @return array Default configurations of the style
  138. */
  139. abstract protected function getDefaultConfigurations();
  140. }