/Extensions/Lib/CroogoTheme.php

https://github.com/kareypowell/croogo · PHP · 193 lines · 130 code · 18 blank · 45 comment · 33 complexity · bc95e2da308a4dead85036f7cf4bf323 MD5 · raw file

  1. <?php
  2. /**
  3. * CroogoTheme class
  4. *
  5. * @category Component
  6. * @package Croogo.Extensions.Lib
  7. * @version 1.4
  8. * @since 1.4
  9. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  10. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  11. * @link http://www.croogo.org
  12. */
  13. class CroogoTheme extends Object {
  14. /**
  15. * Constructor
  16. */
  17. public function __construct() {
  18. $this->Setting = ClassRegistry::init('Settings.Setting');
  19. }
  20. /**
  21. * Get theme aliases (folder names)
  22. *
  23. * @return array
  24. */
  25. public function getThemes() {
  26. $themes = array(
  27. 'default' => 'default',
  28. );
  29. $this->folder = new Folder;
  30. $viewPaths = App::path('views');
  31. $expected = array('name' => '', 'description' => '');
  32. foreach ($viewPaths as $viewPath) {
  33. $this->folder->path = $viewPath . 'Themed';
  34. if (!is_dir($this->folder->path)) {
  35. continue;
  36. }
  37. $themeFolders = $this->folder->read();
  38. foreach ($themeFolders['0'] as $themeFolder) {
  39. $themeRoot = $viewPath . 'Themed' . DS . $themeFolder . DS;
  40. $composerJson = $themeRoot . 'composer.json';
  41. $this->folder->path = $themeRoot;
  42. $themeRootFolderContent = $this->folder->read();
  43. if (in_array('composer.json', $themeRootFolderContent['1'])) {
  44. $contents = file_get_contents($composerJson);
  45. $json = json_decode($contents, true);
  46. if (isset($json['type']) && $json['type'] === 'croogo-theme') {
  47. $themes[$themeFolder] = $themeFolder;
  48. continue;
  49. }
  50. }
  51. $themeWebroot = $themeRoot . 'webroot' . DS;
  52. if (!is_dir($themeWebroot)) {
  53. continue;
  54. }
  55. $themeJson = $themeWebroot . 'theme.json';
  56. $this->folder->path = $themeWebroot;
  57. $themeFolderContent = $this->folder->read();
  58. if (in_array('theme.json', $themeFolderContent['1'])) {
  59. $contents = file_get_contents($themeJson);
  60. $json = json_decode($contents, true);
  61. $intersect = array_intersect_key($expected, $json);
  62. if ($json !== null && $intersect == $expected) {
  63. $themes[$themeFolder] = $themeFolder;
  64. }
  65. }
  66. }
  67. }
  68. return $themes;
  69. }
  70. /**
  71. * Get the content of theme.json or composer.json file from a theme
  72. *
  73. * @param string $alias theme folder name
  74. * @return array
  75. */
  76. public function getData($alias = null) {
  77. $themeData = array(
  78. 'name' => $alias,
  79. 'regions' => array(),
  80. 'screenshot' => null,
  81. );
  82. $default = CakePlugin::path('Croogo') . 'webroot' . DS . 'theme.json';
  83. if ($alias == null || $alias == 'default') {
  84. $manifestFile = $default;
  85. } else {
  86. $viewPaths = App::path('views');
  87. foreach ($viewPaths as $viewPath) {
  88. $themeRoot = $viewPath . 'Themed' . DS . $alias . DS;
  89. $themeJson = $themeRoot . 'webroot' . DS . 'theme.json';
  90. if (file_exists($themeJson)) {
  91. $manifestFile = $themeJson;
  92. }
  93. if (file_exists($themeRoot . 'composer.json')) {
  94. $composerJson = $themeRoot . 'composer.json';
  95. }
  96. if (isset($manifestFile) || isset($composerJson)) {
  97. break;
  98. }
  99. }
  100. }
  101. if (!isset($manifestFile) && !isset($composerJson)) {
  102. return array();
  103. }
  104. if (isset($manifestFile)) {
  105. $json = json_decode(file_get_contents($manifestFile), true);
  106. if ($json) {
  107. $themeData = array_merge($themeData, $json);
  108. }
  109. }
  110. if (isset($composerJson)) {
  111. $json = json_decode(file_get_contents($composerJson), true);
  112. if ($json) {
  113. $json['vendor'] = $json['name'];
  114. unset($json['name']);
  115. $themeData = array_merge($themeData, $json);
  116. }
  117. }
  118. return $themeData;
  119. }
  120. /**
  121. * Get the content of theme.json file from a theme
  122. *
  123. * @param string $alias theme folder name
  124. * @return array
  125. * @deprecated use getData()
  126. */
  127. public function getThemeData($alias = null) {
  128. return $this->getData($alias);
  129. }
  130. /**
  131. * Activate theme $alias
  132. * @param $alias theme alias
  133. * @return mixed On success Setting::$data or true, false on failure
  134. */
  135. public function activate($alias) {
  136. if ($alias == 'default' || $alias == null) {
  137. $alias = '';
  138. }
  139. Cache::delete('file_map', '_cake_core_');
  140. return $this->Setting->write('Site.theme', $alias);
  141. }
  142. /**
  143. * Delete theme
  144. *
  145. * @param string $alias Theme alias
  146. * @return boolean true when successful, false or array or error messages when failed
  147. * @throws InvalidArgumentException
  148. * @throws UnexpectedValueException
  149. */
  150. public function delete($alias) {
  151. if (empty($alias)) {
  152. throw new InvalidArgumentException(__d('croogo', 'Invalid theme'));
  153. }
  154. $paths = array(
  155. APP . 'webroot' . DS . 'theme' . DS . $alias,
  156. APP . 'View' . DS . 'Themed' . DS . $alias,
  157. );
  158. $folder = new Folder;
  159. foreach ($paths as $path) {
  160. if (!file_exists($path)) {
  161. continue;
  162. }
  163. if (is_link($path)) {
  164. return unlink($path);
  165. } elseif (is_dir($path)) {
  166. if ($folder->delete($path)) {
  167. return true;
  168. } else {
  169. return $folder->errors();
  170. }
  171. }
  172. }
  173. throw new UnexpectedValueException(__d('croogo', 'Theme %s not found', $alias));
  174. }
  175. }