PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/instances/common/controllers/manager/rebuild.ctrl.php

http://github.com/alombarte/SIFO
PHP | 259 lines | 184 code | 47 blank | 28 comment | 16 complexity | de7a26bc842b2ce33c3e7c8b37dbefb4 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, GPL-2.0, BSD-3-Clause
  1. <?php
  2. namespace Common;
  3. class ManagerRebuildController extends \Sifo\Controller
  4. {
  5. /**
  6. * Filenames where the configuration files will be stored.
  7. * @var string
  8. */
  9. protected $filenames = array(
  10. 'config' => 'configuration_files.config.php',
  11. 'templates' => 'templates.config.php',
  12. 'classes' => 'classes.config.php',
  13. 'locale' => 'locale.config.php'
  14. );
  15. /**
  16. * Writes all the configurattion files to disk.
  17. *
  18. * Input expected is:
  19. *
  20. * array( 'filename' => array( 'folder_to_parse1', 'folder_to_parse2', '...' ) )
  21. *
  22. * @param array $files
  23. * @return array Array of contents write to each file.
  24. */
  25. protected function rebuildFiles( Array $files )
  26. {
  27. $this->setLayout( 'manager/templates.tpl' );
  28. $output = array( );
  29. $instance_inheritance = array_unique( \Sifo\Domains::getInstance()->getInstanceInheritance() );
  30. $instance_inheritance_reverse = array_reverse( $instance_inheritance );
  31. // Build the instance configuration: instance name and his parent instance name is exists.
  32. foreach( $instance_inheritance_reverse as $key => $instance )
  33. {
  34. $instance_config['current'] = $instance;
  35. if ( isset( $instance_inheritance[ $key+1 ] ) )
  36. {
  37. $instance_config['parent'] = $instance_inheritance_reverse[$key+1];
  38. }
  39. $instances_configuration[] = $instance_config;
  40. unset( $instance_config );
  41. }
  42. // For each instance in the inheritance it regenerates his configuration files.
  43. foreach( $instances_configuration as $instance )
  44. {
  45. $current_instance = $instance['current'];
  46. $this->assign( 'instance_parent', null );
  47. if ( isset( $instance['parent'] ) )
  48. {
  49. $this->assign( 'instance_parent', $instance['parent'] );
  50. }
  51. foreach ( $files as $file => $folders )
  52. {
  53. $configs = array( );
  54. foreach ( $folders as $folder )
  55. {
  56. $configs = array_merge( $configs, $this->getAvailableFiles( $folder, $current_instance ) );
  57. }
  58. $this->assign( 'config', $configs );
  59. $this->assign( 'file_name', $this->filenames[$file] );
  60. $configs_content = $this->grabHtml();
  61. file_put_contents( ROOT_PATH . "/instances/" . $current_instance . "/config/" . $this->filenames[$file], $configs_content );
  62. $output[$current_instance][$file] = $configs_content;
  63. }
  64. }
  65. return $output;
  66. }
  67. public function build()
  68. {
  69. if ( true !== \Sifo\Domains::getInstance()->getDevMode() )
  70. {
  71. throw new \Sifo\Exception_404( 'User tried to access the rebuild page, but he\'s not in development' );
  72. }
  73. // Calculate where the config files are taken from.
  74. $files_output = $this->rebuildFiles( array(
  75. 'config' => array( 'config' ),
  76. 'templates' => array( 'templates' ),
  77. 'classes' => array( 'core', 'classes', 'controllers', 'models' ),
  78. 'locale' => array( 'locale' ),
  79. ) );
  80. // Reset the layout and paste the content in the empty template:
  81. $this->setLayout( 'empty.tpl' );
  82. // Disable debug on this page.
  83. \Sifo\Domains::getInstance()->setDebugMode( false );
  84. $instance_inheritance = implode( ' > ', array_reverse( array_unique( \Sifo\Domains::getInstance()->getInstanceInheritance() ) ) );
  85. $message = <<<MESG
  86. INSTANCE INHERITANCE: $instance_inheritance.
  87. REMEMBER: All this instances have changed their configurations files.
  88. MESG;
  89. foreach( $files_output as $instance_name => $files_instance )
  90. {
  91. $message .= "\n************************** " . strtoupper( $instance_name ) . " ******************\n\n";
  92. foreach ( $files_instance as $file => $output )
  93. {
  94. $message .= "\n==== {$this->filenames[$file]} ====\n$output\n";
  95. }
  96. }
  97. $this->assign( 'content', $message );
  98. header( 'Content-Type: text/plain' );
  99. }
  100. protected function getRunningInstances()
  101. {
  102. $d = new \Sifo\Dir();
  103. $instances = $d->getDirs( ROOT_PATH . '/instances' );
  104. return $instances;
  105. }
  106. protected function cleanStartingSlash( $path )
  107. {
  108. if ( 0 === strpos( $path, "/" ) )
  109. {
  110. // Remove starting slashes.
  111. return substr( $path, 1 );
  112. }
  113. return $path;
  114. }
  115. /**
  116. * Converts something like home/index.ctrl.php to HomeIndex.
  117. *
  118. * @param string $path
  119. * @return string
  120. */
  121. private function getClassTypeStandarized( $path )
  122. {
  123. $class = '';
  124. $ctrl_parts = explode( '/', $path );
  125. while ( $class_name = array_shift( $ctrl_parts ) )
  126. {
  127. $class .= ucfirst( $class_name );
  128. }
  129. return $class;
  130. }
  131. protected function getAvailableFiles( $type, $current_instance )
  132. {
  133. $d = new \Sifo\Dir();
  134. $type_files = array( );
  135. $core_inheritance = \Sifo\Domains::getInstance()->getCoreInheritance();
  136. if ( $type == 'core' )
  137. {
  138. foreach ( $core_inheritance as $corelib )
  139. {
  140. $available_files = $d->getFileListRecursive( ROOT_PATH, '/libs/' . $corelib );
  141. if ( count( $available_files ) > 0 )
  142. {
  143. foreach ( $available_files as $v )
  144. {
  145. // Allow only extensions PHP, TPL, CONF
  146. $desired_file_pattern = preg_match( '/\.(php|tpl|conf)$/i', $v["relative"] );
  147. if ( $desired_file_pattern )
  148. {
  149. $rel_path = $this->cleanStartingSlash( $v["relative"] );
  150. $path = $rel_path;
  151. $rel_path = str_replace( 'libs/' . $corelib . '/', '', $rel_path );
  152. $rel_path = str_replace( '.php', '', $rel_path ); // Default
  153. $class = $this->getClassTypeStandarized( $rel_path );
  154. $type_files[$class]['Sifo'] = $path;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. else
  161. {
  162. $available_files = $d->getFileListRecursive( ROOT_PATH . "/instances/" . $current_instance . "/$type" );
  163. if ( is_array( $available_files ) === true && count( $available_files ) > 0 )
  164. {
  165. foreach ( $available_files as $k => $v )
  166. {
  167. $rel_path = $this->cleanStartingSlash( $v["relative"] );
  168. $path = str_replace( '//', '/', "instances/$current_instance/$type/$rel_path" );
  169. // Calculate the class name for the given file:
  170. $rel_path = str_replace( '.model.php', '', $rel_path );
  171. $rel_path = str_replace( '.ctrl.php', '', $rel_path );
  172. $rel_path = str_replace( '.config.php', '', $rel_path );
  173. $rel_path = str_replace( '.php', '', $rel_path ); // Default
  174. $class = $this->getClassTypeStandarized( $rel_path );
  175. if ( 'default' != $current_instance )
  176. {
  177. $class_extended = $class . ucfirst( $current_instance );
  178. }
  179. else
  180. {
  181. $class_extended = $class;
  182. }
  183. switch ( $type )
  184. {
  185. case 'controllers':
  186. $class .= 'Controller';
  187. $type_files[$class][ucfirst( $current_instance )] = $path;
  188. break;
  189. case 'models':
  190. $class .= 'Model';
  191. $type_files[$class][ucfirst( $current_instance )] = $path;
  192. break;
  193. case 'classes':
  194. $type_files[$class][ucfirst( $current_instance )] = $path;
  195. break;
  196. case 'config':
  197. if ( $rel_path == 'configuration_files' )
  198. {
  199. continue;
  200. }
  201. case 'templates':
  202. default:
  203. $type_files[$rel_path] = $path;
  204. }
  205. }
  206. }
  207. }
  208. ksort( $type_files );
  209. return $type_files;
  210. }
  211. }
  212. ?>