PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Library/Kumbia/Migrate/Migrate.php

http://kumbia-enterprise.googlecode.com/
PHP | 581 lines | 404 code | 28 blank | 149 comment | 103 complexity | be0407211280ad884dcc76848f3035dd MD5 | raw file
  1. <?php
  2. /**
  3. * Kumbia Enterprise Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the New BSD License that is bundled
  8. * with this package in the file docs/LICENSE.txt.
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@loudertechnology.com so we can send you a copy immediately.
  12. *
  13. * @category Kumbia
  14. * @package Migrate
  15. * @copyright Copyright (c) 2008-2010 Louder Technology COL. (http://www.loudertechnology.com)
  16. * @license New BSD License
  17. * @version $Id: Migrate.php 131 2010-03-24 23:55:50Z gutierrezandresfelipe $
  18. */
  19. /**
  20. * Migrate
  21. *
  22. * Subcomponente que permite actualizar versiones anteriores del framework a la más reciente
  23. *
  24. * @category Kumbia
  25. * @package Migrate
  26. * @copyright Copyright (c) 2008-2010 Louder Technology COL. (http://www.loudertechnology.com)
  27. * @license New BSD License
  28. */
  29. class Migrate {
  30. /**
  31. * Indica si el controlador actual es StandardForm
  32. *
  33. * @var boolean
  34. */
  35. private $_isStandardForm = false;
  36. /**
  37. * Nombre de la clase actual
  38. *
  39. * @var string
  40. */
  41. private $_className = '';
  42. /**
  43. * Relación de métodos de ActiveRecord
  44. *
  45. * @var array
  46. */
  47. private $_activeRecordMethodMap = array(
  48. 'find_first' => 'findFirst',
  49. 'belongs_to' => 'belongsTo',
  50. 'has_many' => 'hasMany',
  51. 'has_one' => 'hasOne'
  52. );
  53. /**
  54. * Relación de métodos de Db
  55. *
  56. * @var array
  57. */
  58. private $_dbMethodMap = array(
  59. 'fetch_one' => 'fetchOne'
  60. );
  61. /**
  62. * Mapa de Funciones a migrar de controladores
  63. *
  64. * @var array
  65. */
  66. private $_controllerMethodMap = array(
  67. 'set_response' => 'setResponse',
  68. 'route_to' => 'routeTo',
  69. 'request' => 'getRequestParam',
  70. 'post' => 'getPostParam',
  71. 'getPost' => 'getPostParam',
  72. 'getPOST' => 'getPostParam',
  73. 'get' => 'getQueryParam',
  74. 'getGet' => 'getQueryParam',
  75. 'getGET' => 'getQueryParam',
  76. 'render_text' => 'renderText',
  77. 'render_partial' => 'renderPartial'
  78. );
  79. /**
  80. * Mapa de funciones a migrar de controladores
  81. *
  82. * @var array
  83. */
  84. private $_standardFormMethodMap = array(
  85. 'set_form_caption' => 'setFormCaption',
  86. 'set_caption' => 'setCaption',
  87. 'set_type_image' => 'setTypeImage',
  88. 'set_text_upper' => 'setTextUpper',
  89. 'set_combo_static' => 'setComboStatic',
  90. 'set_combo_dynamic' => 'setComboDynamic',
  91. 'set_query_only' => 'setQueryOnly',
  92. 'not_report' => 'notReport',
  93. 'not_browse' => 'notBrowse',
  94. 'set_hidden' => 'setHidden',
  95. 'set_title_form' => 'setTitleForm',
  96. 'unable_insert' => 'unableInsert',
  97. 'unable_delete' => 'unableDelete',
  98. 'unable_update' => 'unableUpdate',
  99. );
  100. private $_controllerFilters = array(
  101. 'before_filter' => 'beforeFilter',
  102. 'after_filter' => 'afterFilter'
  103. );
  104. private $_activeRecordEvent = array(
  105. 'after_create' => 'afterCreate',
  106. 'before_create' => 'beforeCreate',
  107. 'after_update' => 'afterUpdate',
  108. 'before_update' => 'beforeUpdate',
  109. 'before_delete' => 'beforeDelete'
  110. );
  111. /**
  112. * Eventos de StandardForm
  113. *
  114. * @var unknown_type
  115. */
  116. private $_standardFormEvents = array(
  117. 'before_insert' => 'beforeInsert',
  118. 'before_report' => 'beforeReport',
  119. 'before_delete' => 'beforeDelete',
  120. 'after_report' => 'afterReport',
  121. 'before_update' => 'beforeUpdate'
  122. );
  123. /**
  124. * Relación de componentes y sus métodos
  125. *
  126. * @var unknown_type
  127. */
  128. private $_commonMap = array(
  129. 'Kumbia' => array(
  130. 'route_to' => array('Router', 'routeTo'),
  131. 'stylesheet_link_tags' => array('Tag', 'stylesheetLinkTags'),
  132. 'javascript_base' => array('Tag', 'javascriptBase'),
  133. 'import' => array('Core', 'import')
  134. ),
  135. 'kumbia' => array(
  136. 'route_to' => array('Router', 'routeTo'),
  137. 'stylesheet_link_tags' => array('Tag', 'stylesheetLinkTags'),
  138. 'javascript_base' => array('Tag', 'javascriptBase'),
  139. ),
  140. 'Db' => array(
  141. 'raw_connect' => array('Db', 'rawConnect'),
  142. ),
  143. 'Config' => array(
  144. 'read' => array('CoreConfig', 'readFromActiveApplication')
  145. ),
  146. 'Session' => array(
  147. 'set_data' => array('Session', 'setData'),
  148. 'get_data' => array('Session', 'getData'),
  149. 'unset_data' => array('Session', 'unsetData')
  150. ),
  151. );
  152. /**
  153. * Relación de Helpers
  154. *
  155. * @var array
  156. */
  157. private $_tagHelpers = array(
  158. 'form_tag' => 'Tag::form',
  159. 'link_to' => 'Tag::linkTo',
  160. 'link_to_remote' => 'Tag::linkTo',
  161. 'submit_tag' => 'Tag::submitButton',
  162. 'end_form_tag' => 'Tag::endForm',
  163. 'text_field_tag' => 'Tag::textField',
  164. 'password_field_tag' => 'Tag::passwordField',
  165. 'numeric_field_tag' => 'Tag::numericField',
  166. 'stylesheet_link_tag' => 'Tag::stylesheetLink',
  167. 'javascript_include_tag' => 'Tag::javascriptInclude',
  168. 'javascript_library_tag' => 'Tag::javascriptLibrary',
  169. 'img_tag' => 'Tag::image',
  170. 'tr_break' => 'Tag::trBreak',
  171. 'date_field_tag' => 'Tag::dateField',
  172. 'button_to_action' => 'Tag::buttonToAction',
  173. 'textupper_field_tag' => 'Tag::textUpperField',
  174. 'text_field_with_autocomplete' => 'Tag::textFieldWithAutocomplete',
  175. 'button_tag' => 'Tag::button',
  176. 'select_tag' => 'Tag::select',
  177. 'hidden_field_tag' => 'Tag::hiddenField',
  178. 'file_field_tag' => 'Tag::fileFieldTag'
  179. );
  180. /**
  181. * Indica si es un metodo de active record
  182. *
  183. * @param array $tokens
  184. * @param int $i
  185. */
  186. private function _isActiveRecordMethod($tokens, $i){
  187. if(isset($tokens[$i-4])){
  188. if(isset($tokens[$i-4][1])){
  189. if($tokens[$i-4][1]=='$this'){
  190. if(isset($this->_activeRecordMethodMap[$tokens[$i][1]])){
  191. $tokens[$i][1] = $this->_activeRecordMethodMap[$tokens[$i][1]];
  192. return true;
  193. } else {
  194. return false;
  195. }
  196. }
  197. } else {
  198. return false;
  199. }
  200. } else {
  201. return false;
  202. }
  203. }
  204. /**
  205. * Indica si es un metodo de active record
  206. *
  207. * @param array $tokens
  208. * @param int $i
  209. */
  210. private function _isOwnActiveRecordMethod($tokens, $i){
  211. if(isset($tokens[$i-2])){
  212. if(isset($tokens[$i-2][1])){
  213. if($tokens[$i-2][1]=='$this'){
  214. if(isset($this->_activeRecordMethodMap[$tokens[$i][1]])){
  215. $tokens[$i][1] = $this->_activeRecordMethodMap[$tokens[$i][1]];
  216. return true;
  217. }
  218. }
  219. if($tokens[$i-2][1]=='function'){
  220. if($tokens[$i][1]=='__construct'){
  221. $tokens[$i][1] = 'initialize';
  222. return true;
  223. } else {
  224. if(isset($this->_activeRecordEvent[$tokens[$i][1]])){
  225. $tokens[$i][1] = $this->_activeRecordEvent[$tokens[$i][1]];
  226. return true;
  227. }
  228. }
  229. }
  230. } else {
  231. return false;
  232. }
  233. } else {
  234. return false;
  235. }
  236. }
  237. /**
  238. * Migra un metodo del controlador
  239. *
  240. * @param array $tokens
  241. * @param int $i
  242. */
  243. private function _isControllerMethod($tokens, $i){
  244. if(isset($tokens[$i-2])){
  245. if(isset($tokens[$i-2][1])){
  246. if($tokens[$i-2][1]=='$this'){
  247. if(isset($this->_controllerMethodMap[$tokens[$i][1]])){
  248. $tokens[$i][1] = $this->_controllerMethodMap[$tokens[$i][1]];
  249. return true;
  250. } else {
  251. if($this->_isStandardForm==true){
  252. if(isset($this->_standardFormMethodMap[$tokens[$i][1]])){
  253. $tokens[$i][1] = $this->_standardFormMethodMap[$tokens[$i][1]];
  254. return true;
  255. }
  256. }
  257. }
  258. }
  259. if($tokens[$i-2][1]=='function'){
  260. if(substr($tokens[$i][1], 0, 2)!='__'&&$tokens[$i][1]!=$this->_className){
  261. if($this->_isStandardForm==false){
  262. if(isset($this->_controllerFilters[$tokens[$i][1]])){
  263. $tokens[$i][1] = $this->_controllerFilters[$tokens[$i][1]];
  264. return true;
  265. } else {
  266. $tokens[$i][1] = $tokens[$i][1].'Action';
  267. return true;
  268. }
  269. } else {
  270. if($tokens[$i][1]!='initialize'){
  271. if(isset($this->_standardFormEvents[$tokens[$i][1]])){
  272. $tokens[$i][1] = $this->_standardFormEvents[$tokens[$i][1]];
  273. return true;
  274. } else {
  275. if(isset($this->_controllerFilters[$tokens[$i][1]])){
  276. $tokens[$i][1] = $this->_controllerFilters[$tokens[$i][1]];
  277. return true;
  278. } else {
  279. $tokens[$i][1] = $tokens[$i][1].'Action';
  280. return true;
  281. }
  282. }
  283. }
  284. }
  285. } else {
  286. if($tokens[$i][1]=='__construct'||$tokens[$i][1]==$this->_className){
  287. $tokens[$i][1] = 'initialize';
  288. return true;
  289. }
  290. }
  291. }
  292. } else {
  293. return false;
  294. }
  295. } else {
  296. return false;
  297. }
  298. }
  299. /**
  300. * Migra metodos comunes
  301. *
  302. * @param array $tokens
  303. * @param int $i
  304. */
  305. private function _isCommonMethod($tokens, $i){
  306. if(isset($tokens[$i])){
  307. if(isset($tokens[$i][1])){
  308. if(isset($this->_commonMap[$tokens[$i][1]])){
  309. if(isset($this->_commonMap[$tokens[$i][1]][$tokens[$i+2][1]])){
  310. $replaceMethod = &$this->_commonMap[$tokens[$i][1]][$tokens[$i+2][1]];
  311. $tokens[$i][1] = $replaceMethod[0];
  312. $tokens[$i+2][1] = $replaceMethod[1];
  313. return true;
  314. }
  315. } else {
  316. return false;
  317. }
  318. } else {
  319. return false;
  320. }
  321. } else {
  322. return false;
  323. }
  324. }
  325. /**
  326. * Migra los helpers JavaScript
  327. *
  328. * @param array $tokens
  329. * @param int $i
  330. * @return
  331. */
  332. private function _isTagHelper($tokens, $i){
  333. if(isset($this->_tagHelpers[$tokens[$i][1]])){
  334. $tokens[$i][1] = $this->_tagHelpers[$tokens[$i][1]];
  335. return true;
  336. } else {
  337. return false;
  338. }
  339. }
  340. /**
  341. * Migra los helpers JavaScript
  342. *
  343. * @param array $tokens
  344. * @param int $i
  345. * @return
  346. */
  347. private function _isViewContent($tokens, $i){
  348. if($tokens[$i][1]=='content'){
  349. $tokens[$i][1] = 'View::getContent';
  350. return true;
  351. } else {
  352. return false;
  353. }
  354. }
  355. /**
  356. * Indica si es un método de acceso a la base de datos
  357. *
  358. * @param array $tokens
  359. * @param int $i
  360. * @return boolean
  361. */
  362. private function _isDbMethod($tokens, $i){
  363. if(isset($this->_dbMethodMap[$tokens[$i][1]])){
  364. $tokens[$i][1] = $this->_dbMethodMap[$tokens[$i][1]];
  365. return true;
  366. }
  367. return false;
  368. }
  369. /**
  370. * Indica si es un helper en una vista
  371. *
  372. * @param array $tokens
  373. * @param int $i
  374. * @return boolean
  375. */
  376. private function _isInViewActiveRecord($tokens, $i){
  377. if(isset($this->_activeRecordMethodMap[$tokens[$i][1]])){
  378. $tokens[$i][1] = $this->_activeRecordMethodMap[$tokens[$i][1]];
  379. return true;
  380. }
  381. return false;
  382. }
  383. /**
  384. * Migra el antiguo KUMBIA_PATH
  385. *
  386. * @param array $tokens
  387. * @param int $i
  388. * @return booelan
  389. */
  390. private function _isKumbiaPath($tokens, $i){
  391. if($tokens[$i][1]=='KUMBIA_PATH'){
  392. $tokens[$i][1] = 'Core::getInstancePath()';
  393. return true;
  394. }
  395. return false;
  396. }
  397. /**
  398. * Migra un modelo a KEF
  399. *
  400. * @param string $source
  401. */
  402. public function migrateModel($source){
  403. $migratedSource = "";
  404. $tokens = token_get_all($source);
  405. $i = 0;
  406. $tl = count($tokens);
  407. for($i=0;$i<$tl;++$i){
  408. $token = $tokens[$i];
  409. if(!isset($token[1])){
  410. $migratedSource.=$token[0];
  411. } else {
  412. if($token[0]==T_STRING){
  413. if($this->_isOwnActiveRecordMethod(&$tokens, $i)==true){
  414. $token = $tokens[$i];
  415. } else {
  416. if($this->_isCommonMethod(&$tokens, $i)==true){
  417. $token = $tokens[$i];
  418. }
  419. }
  420. }
  421. $migratedSource.=$token[1];
  422. }
  423. }
  424. return $migratedSource;
  425. }
  426. /**
  427. * Migra un controlador a KEF
  428. *
  429. * @param string $source
  430. */
  431. public function migrateController($source){
  432. $migratedSource = "";
  433. $tokens = token_get_all($source);
  434. $i = 0;
  435. $tl = count($tokens);
  436. for($i=0;$i<$tl;++$i){
  437. $token = $tokens[$i];
  438. if(!isset($token[1])){
  439. $migratedSource.=$token[0];
  440. } else {
  441. if($i<$tl){
  442. #$migratedSource.=$token[0];
  443. } else {
  444. if($token[0]==T_CLOSE_TAG){
  445. continue;
  446. }
  447. }
  448. if($token[0]==T_STRING){
  449. if($this->_isActiveRecordMethod(&$tokens, $i)==true){
  450. $token = $tokens[$i];
  451. } else {
  452. if($this->_isControllerMethod(&$tokens, $i)==true){
  453. $token = $tokens[$i];
  454. } else {
  455. if($this->_isCommonMethod(&$tokens, $i)==true){
  456. $token = $tokens[$i];
  457. } else {
  458. if($this->_isKumbiaPath(&$tokens, $i)==true){
  459. $token = $tokens[$i];
  460. }
  461. }
  462. }
  463. }
  464. } else {
  465. if($token[0]==T_EXTENDS){
  466. if(isset($tokens[$i+2][1])){
  467. if($tokens[$i+2][1]=='StandardForm'){
  468. $this->_isStandardForm = true;
  469. }
  470. }
  471. } else {
  472. if($token[0]==T_CLASS){
  473. if(isset($tokens[$i+2][1])){
  474. $this->_className = $tokens[$i+2][1];
  475. }
  476. }
  477. }
  478. }
  479. $migratedSource.=$token[1];
  480. }
  481. }
  482. return $migratedSource;
  483. }
  484. public function migrateAppController($source){
  485. $migratedSource = "";
  486. $tokens = token_get_all($source);
  487. $tl = count($tokens);
  488. for($i=0;$i<$tl;++$i){
  489. $token = $tokens[$i];
  490. if(!isset($token[1])){
  491. $migratedSource.=$token[0];
  492. } else {
  493. if($this->_isCommonMethod(&$tokens, $i)==true){
  494. $token = $tokens[$i];
  495. }
  496. $migratedSource.=$token[1];
  497. }
  498. }
  499. return $migratedSource;
  500. }
  501. /**
  502. * Migra una vista de cualquier tipo
  503. *
  504. * @param string $source
  505. * @return string
  506. */
  507. public function migrateView($source){
  508. $migratedSource = "";
  509. $tokens = token_get_all($source);
  510. $tl = count($tokens);
  511. for($i=0;$i<$tl;++$i){
  512. $token = $tokens[$i];
  513. if(!isset($token[1])){
  514. $migratedSource.=$token[0];
  515. } else {
  516. if($token[0]==T_OPEN_TAG){
  517. $migratedSource.='<?php ';
  518. } else {
  519. if($token[0]==T_OPEN_TAG_WITH_ECHO){
  520. $migratedSource.='<?php echo ';
  521. } else {
  522. if($this->_isCommonMethod(&$tokens, $i)==true){
  523. $token = $tokens[$i];
  524. } else {
  525. if($this->_isTagHelper(&$tokens, $i)==true){
  526. $token = $tokens[$i];
  527. } else {
  528. if($this->_isViewContent(&$tokens, $i)==true){
  529. $token = $tokens[$i];
  530. } else {
  531. if($this->_isInViewActiveRecord(&$tokens, $i)==true){
  532. $token = $tokens[$i];
  533. } else {
  534. if($this->_isKumbiaPath(&$tokens, $i)==true){
  535. $token = $tokens[$i];
  536. } else {
  537. if($this->_isDbMethod(&$tokens, $i)==true){
  538. $token = $tokens[$i];
  539. }
  540. }
  541. }
  542. }
  543. }
  544. }
  545. $migratedSource.=$token[1];
  546. }
  547. }
  548. }
  549. }
  550. return $migratedSource;
  551. }
  552. }