PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/prado-cli.php

https://bitbucket.org/freshflow/prado-fork-v3.10
PHP | 789 lines | 620 code | 75 blank | 94 comment | 53 complexity | 86a2801605554ac45d4f904e208bb175 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. <?php
  2. /**
  3. * Prado command line developer tools.
  4. *
  5. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  6. * @link http://www.pradosoft.com/
  7. * @copyright Copyright &copy; 2005-2008 PradoSoft
  8. * @license http://www.pradosoft.com/license/
  9. * @version $Id: prado-cli.php 2498 2008-08-18 00:28:11Z knut $
  10. */
  11. if(!isset($_SERVER['argv']) || php_sapi_name()!=='cli')
  12. die('Must be run from the command line');
  13. ini_set('error_reporting', E_ALL & ~E_STRICT & ~E_NOTICE & ~E_DEPRECATED);
  14. //ini_set('display_errors', true);
  15. //ini_set('track_errors',true);
  16. //newrelic_ignore_transaction ();
  17. $basePath = getcwd();
  18. //require_once(dirname(__FILE__).'/PEAR/PEAR.php');
  19. require_once(dirname(__FILE__).'/prado.php');
  20. Prado::setPathOfAlias('Root',$basePath);
  21. Prado::setPathOfAlias('FreshSystem',$basePath.'/FreshSystem');
  22. Prado::setPathOfAlias('JS',$basePath.'/FreshSystem/lib');
  23. Prado::using('Root.PEAR.*');
  24. //stub application class
  25. class PradoShellApplication extends TShellApplication
  26. {
  27. }
  28. restore_exception_handler();
  29. //config PHP shell
  30. if(count($_SERVER['argv']) > 1 && strtolower($_SERVER['argv'][1])==='shell')
  31. {
  32. function __shell_print_var($shell,$var)
  33. {
  34. if(!$shell->has_semicolon) echo Prado::varDump($var);
  35. }
  36. include_once(dirname(__FILE__).'/3rdParty/PhpShell/php-shell-init.php');
  37. }
  38. //register action classes
  39. PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineCreateProject');
  40. PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineCreateTests');
  41. PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLinePhpShell');
  42. PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineUnitTest');
  43. PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineActiveRecordGen');
  44. //run it;
  45. PradoCommandLineInterpreter::getInstance()->run($_SERVER['argv']);
  46. /**************** END CONFIGURATION **********************/
  47. /**
  48. * PradoCommandLineInterpreter Class
  49. *
  50. * Command line interface, configures the action classes and dispatches the command actions.
  51. *
  52. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  53. * @version $Id: prado-cli.php 2498 2008-08-18 00:28:11Z knut $
  54. * @since 3.0.5
  55. */
  56. class PradoCommandLineInterpreter
  57. {
  58. /**
  59. * @var array command action classes
  60. */
  61. protected $_actions=array();
  62. /**
  63. * @param string action class name
  64. */
  65. public function addActionClass($class)
  66. {
  67. $this->_actions[$class] = new $class;
  68. }
  69. /**
  70. * @return PradoCommandLineInterpreter static instance
  71. */
  72. public static function getInstance()
  73. {
  74. static $instance;
  75. if($instance === null)
  76. $instance = new self;
  77. return $instance;
  78. }
  79. /**
  80. * Dispatch the command line actions.
  81. * @param array command line arguments
  82. */
  83. public function run($args)
  84. {
  85. echo "Command line tools for Prado ".Prado::getVersion().".\n";
  86. if(count($args) > 1)
  87. array_shift($args);
  88. $valid = false;
  89. foreach($this->_actions as $class => $action)
  90. {
  91. if($action->isValidAction($args))
  92. {
  93. $valid |= $action->performAction($args);
  94. break;
  95. }
  96. else
  97. {
  98. $valid = false;
  99. }
  100. }
  101. if(!$valid)
  102. $this->printHelp();
  103. }
  104. /**
  105. * Print command line help, default action.
  106. */
  107. public function printHelp()
  108. {
  109. echo "usage: php prado-cli.php action <parameter> [optional]\n";
  110. echo "example: php prado-cli.php -c mysite\n\n";
  111. echo "actions:\n";
  112. foreach($this->_actions as $action)
  113. echo $action->renderHelp();
  114. }
  115. }
  116. /**
  117. * Base class for command line actions.
  118. *
  119. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  120. * @version $Id: prado-cli.php 2498 2008-08-18 00:28:11Z knut $
  121. * @since 3.0.5
  122. */
  123. abstract class PradoCommandLineAction
  124. {
  125. /**
  126. * Execute the action.
  127. * @param array command line parameters
  128. * @return boolean true if action was handled
  129. */
  130. public abstract function performAction($args);
  131. protected function createDirectory($dir, $mask)
  132. {
  133. if(!is_dir($dir))
  134. {
  135. mkdir($dir);
  136. echo "creating $dir\n";
  137. }
  138. if(is_dir($dir))
  139. chmod($dir, $mask);
  140. }
  141. protected function createFile($filename, $content)
  142. {
  143. if(!is_file($filename))
  144. {
  145. file_put_contents($filename, $content);
  146. echo "creating $filename\n";
  147. }
  148. }
  149. public function isValidAction($args)
  150. {
  151. return strtolower($args[0]) === $this->action &&
  152. count($args)-1 >= count($this->parameters);
  153. }
  154. public function renderHelp()
  155. {
  156. $params = array();
  157. foreach($this->parameters as $v)
  158. $params[] = '<'.$v.'>';
  159. $parameters = join($params, ' ');
  160. $options = array();
  161. foreach($this->optional as $v)
  162. $options[] = '['.$v.']';
  163. $optional = (strlen($parameters) ? ' ' : ''). join($options, ' ');
  164. $description='';
  165. foreach(explode("\n", wordwrap($this->description,65)) as $line)
  166. $description .= ' '.$line."\n";
  167. return <<<EOD
  168. {$this->action} {$parameters}{$optional}
  169. {$description}
  170. EOD;
  171. }
  172. protected function initializePradoApplication($directory)
  173. {
  174. $app_dir = realpath($directory.'/protected/');
  175. if($app_dir !== false && is_dir($app_dir))
  176. {
  177. if(Prado::getApplication()===null)
  178. {
  179. $app = new PradoShellApplication($app_dir);
  180. Prado::trace('Initializing application','System.TApplication');
  181. $app->run();
  182. //var_dump($app);
  183. echo '*';
  184. $dir = substr(str_replace(realpath('./'),'',$app_dir),1);
  185. $initialized=true;
  186. echo '** Loaded PRADO appplication in directory "'.$dir."\".\n";
  187. }
  188. return Prado::getApplication();
  189. }
  190. else
  191. {
  192. echo '+'.str_repeat('-',77)."+\n";
  193. echo '** Unable to load PRADO application in directory "'.$directory."\".\n";
  194. echo '+'.str_repeat('-',77)."+\n";
  195. }
  196. return false;
  197. }
  198. }
  199. /**
  200. * Create a Prado project skeleton, including directories and files.
  201. *
  202. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  203. * @version $Id: prado-cli.php 2498 2008-08-18 00:28:11Z knut $
  204. * @since 3.0.5
  205. */
  206. class PradoCommandLineCreateProject extends PradoCommandLineAction
  207. {
  208. protected $action = '-c';
  209. protected $parameters = array('directory');
  210. protected $optional = array();
  211. protected $description = 'Creates a Prado project skeleton for the given <directory>.';
  212. public function performAction($args)
  213. {
  214. $this->createNewPradoProject($args[1]);
  215. return true;
  216. }
  217. /**
  218. * Functions to create new prado project.
  219. */
  220. protected function createNewPradoProject($dir)
  221. {
  222. if(strlen(trim($dir)) == 0)
  223. return;
  224. $rootPath = realpath(dirname(trim($dir)));
  225. if(basename($dir)!=='.')
  226. $basePath = $rootPath.DIRECTORY_SEPARATOR.basename($dir);
  227. else
  228. $basePath = $rootPath;
  229. $appName = basename($basePath);
  230. $assetPath = $basePath.DIRECTORY_SEPARATOR.'assets';
  231. $protectedPath = $basePath.DIRECTORY_SEPARATOR.'protected';
  232. $runtimePath = $basePath.DIRECTORY_SEPARATOR.'protected'.DIRECTORY_SEPARATOR.'runtime';
  233. $pagesPath = $protectedPath.DIRECTORY_SEPARATOR.'pages';
  234. $indexFile = $basePath.DIRECTORY_SEPARATOR.'index.php';
  235. $htaccessFile = $protectedPath.DIRECTORY_SEPARATOR.'.htaccess';
  236. $configFile = $protectedPath.DIRECTORY_SEPARATOR.'application.xml';
  237. $defaultPageFile = $pagesPath.DIRECTORY_SEPARATOR.'Home.page';
  238. $this->createDirectory($basePath, 0755);
  239. $this->createDirectory($assetPath,0777);
  240. $this->createDirectory($protectedPath,0755);
  241. $this->createDirectory($runtimePath,0777);
  242. $this->createDirectory($pagesPath,0755);
  243. $this->createFile($indexFile, $this->renderIndexFile());
  244. $this->createFile($configFile, $this->renderConfigFile($appName));
  245. $this->createFile($htaccessFile, $this->renderHtaccessFile());
  246. $this->createFile($defaultPageFile, $this->renderDefaultPage());
  247. }
  248. protected function renderIndexFile()
  249. {
  250. $framework = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'prado.php';
  251. return '<?php
  252. $frameworkPath=\''.$framework.'\';
  253. // The following directory checks may be removed if performance is required
  254. $basePath=dirname(__FILE__);
  255. $assetsPath=$basePath.\'/assets\';
  256. $runtimePath=$basePath.\'/protected/runtime\';
  257. if(!is_file($frameworkPath))
  258. die("Unable to find prado framework path $frameworkPath.");
  259. if(!is_writable($assetsPath))
  260. die("Please make sure that the directory $assetsPath is writable by Web server process.");
  261. if(!is_writable($runtimePath))
  262. die("Please make sure that the directory $runtimePath is writable by Web server process.");
  263. require_once($frameworkPath);
  264. $application=new TApplication;
  265. $application->run();
  266. ?>';
  267. }
  268. protected function renderConfigFile($appName)
  269. {
  270. return <<<EOD
  271. <?xml version="1.0" encoding="utf-8"?>
  272. <application id="$appName" mode="Debug">
  273. <!-- alias definitions and namespace usings
  274. <paths>
  275. <alias id="myalias" path="./lib" />
  276. <using namespace="Application.common.*" />
  277. </paths>
  278. -->
  279. <!-- configurations for modules -->
  280. <modules>
  281. <!-- Remove this comment mark to enable caching
  282. <module id="cache" class="System.Caching.TDbCache" />
  283. -->
  284. <!-- Remove this comment mark to enable PATH url format
  285. <module id="request" class="THttpRequest" UrlFormat="Path" />
  286. -->
  287. <!-- Remove this comment mark to enable logging
  288. <module id="log" class="System.Util.TLogRouter">
  289. <route class="TBrowserLogRoute" Categories="System" />
  290. </module>
  291. -->
  292. </modules>
  293. <!-- configuration for available services -->
  294. <services>
  295. <service id="page" class="TPageService" DefaultPage="Home" />
  296. </services>
  297. <!-- application parameters
  298. <parameters>
  299. <parameter id="param1" value="value1" />
  300. <parameter id="param2" value="value2" />
  301. </parameters>
  302. -->
  303. </application>
  304. EOD;
  305. }
  306. protected function renderHtaccessFile()
  307. {
  308. return 'deny from all';
  309. }
  310. protected function renderDefaultPage()
  311. {
  312. return <<<EOD
  313. <html>
  314. <head>
  315. <title>Welcome to PRADO</title>
  316. </head>
  317. <body>
  318. <h1>Welcome to PRADO!</h1>
  319. </body>
  320. </html>
  321. EOD;
  322. }
  323. }
  324. /**
  325. * Creates test fixtures for a Prado application.
  326. *
  327. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  328. * @version $Id: prado-cli.php 2498 2008-08-18 00:28:11Z knut $
  329. * @since 3.0.5
  330. */
  331. class PradoCommandLineCreateTests extends PradoCommandLineAction
  332. {
  333. protected $action = '-t';
  334. protected $parameters = array('directory');
  335. protected $optional = array();
  336. protected $description = 'Create test fixtures in the given <directory>.';
  337. public function performAction($args)
  338. {
  339. $this->createTestFixtures($args[1]);
  340. return true;
  341. }
  342. protected function createTestFixtures($dir)
  343. {
  344. if(strlen(trim($dir)) == 0)
  345. return;
  346. $rootPath = realpath(dirname(trim($dir)));
  347. $basePath = $rootPath.'/'.basename($dir);
  348. $tests = $basePath.'/tests';
  349. $unit_tests = $tests.'/unit';
  350. $functional_tests = $tests.'/functional';
  351. $this->createDirectory($tests,0755);
  352. $this->createDirectory($unit_tests,0755);
  353. $this->createDirectory($functional_tests,0755);
  354. $unit_test_index = $tests.'/unit.php';
  355. $functional_test_index = $tests.'/functional.php';
  356. $this->createFile($unit_test_index, $this->renderUnitTestFixture());
  357. $this->createFile($functional_test_index, $this->renderFunctionalTestFixture());
  358. }
  359. protected function renderUnitTestFixture()
  360. {
  361. $tester = realpath(dirname(__FILE__).'/../tests/test_tools/unit_tests.php');
  362. return '<?php
  363. include_once \''.$tester.'\';
  364. $app_directory = "../protected";
  365. $test_cases = dirname(__FILE__)."/unit";
  366. $tester = new PradoUnitTester($test_cases, $app_directory);
  367. $tester->run(new HtmlReporter());
  368. ?>';
  369. }
  370. protected function renderFunctionalTestFixture()
  371. {
  372. $tester = realpath(dirname(__FILE__).'/../tests/test_tools/functional_tests.php');
  373. return '<?php
  374. include_once \''.$tester.'\';
  375. $test_cases = dirname(__FILE__)."/functional";
  376. $tester=new PradoFunctionalTester($test_cases);
  377. $tester->run(new SimpleReporter());
  378. ?>';
  379. }
  380. }
  381. /**
  382. * Creates and run a Prado application in a PHP Shell.
  383. *
  384. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  385. * @version $Id: prado-cli.php 2498 2008-08-18 00:28:11Z knut $
  386. * @since 3.0.5
  387. */
  388. class PradoCommandLinePhpShell extends PradoCommandLineAction
  389. {
  390. protected $action = 'shell';
  391. protected $parameters = array();
  392. protected $optional = array('directory');
  393. protected $description = 'Runs a PHP interactive interpreter. Initializes the Prado application in the given [directory].';
  394. public function performAction($args)
  395. {
  396. if(count($args) > 1)
  397. $app = $this->initializePradoApplication($args[1]);
  398. return true;
  399. }
  400. }
  401. /**
  402. * Runs unit test cases.
  403. *
  404. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  405. * @version $Id: prado-cli.php 2498 2008-08-18 00:28:11Z knut $
  406. * @since 3.0.5
  407. */
  408. class PradoCommandLineUnitTest extends PradoCommandLineAction
  409. {
  410. protected $action = 'test';
  411. protected $parameters = array('directory');
  412. protected $optional = array('testcase ...');
  413. protected $description = 'Runs all unit test cases in the given <directory>. Use [testcase] option to run specific test cases.';
  414. protected $matches = array();
  415. public function performAction($args)
  416. {
  417. $dir = realpath($args[1]);
  418. if($dir !== false && is_dir($dir))
  419. $this->runUnitTests($dir,$args);
  420. else
  421. echo '** Unable to find directory "'.$args[1]."\".\n";
  422. return true;
  423. }
  424. protected function initializeTestRunner()
  425. {
  426. $TEST_TOOLS = realpath(dirname(__FILE__).'/../tests/test_tools/');
  427. require_once($TEST_TOOLS.'/simpletest/unit_tester.php');
  428. require_once($TEST_TOOLS.'/simpletest/web_tester.php');
  429. require_once($TEST_TOOLS.'/simpletest/mock_objects.php');
  430. require_once($TEST_TOOLS.'/simpletest/reporter.php');
  431. }
  432. protected function runUnitTests($dir, $args)
  433. {
  434. $app_dir = $this->getAppDir($dir);
  435. if($app_dir !== false)
  436. $this->initializePradoApplication($app_dir.'/../');
  437. $this->initializeTestRunner();
  438. $test_dir = $this->getTestDir($dir);
  439. if($test_dir !== false)
  440. {
  441. $test =$this->getUnitTestCases($test_dir,$args);
  442. $running_dir = substr(str_replace(realpath('./'),'',$test_dir),1);
  443. echo 'Running unit tests in directory "'.$running_dir."\":\n";
  444. $test->run(new TextReporter());
  445. }
  446. else
  447. {
  448. $running_dir = substr(str_replace(realpath('./'),'',$dir),1);
  449. echo '** Unable to find test directory "'.$running_dir.'/unit" or "'.$running_dir.'/tests/unit".'."\n";
  450. }
  451. }
  452. protected function getAppDir($dir)
  453. {
  454. $app_dir = realpath($dir.'/protected');
  455. if($app_dir !== false && is_dir($app_dir))
  456. return $app_dir;
  457. return realpath($dir.'/../protected');
  458. }
  459. protected function getTestDir($dir)
  460. {
  461. $test_dir = realpath($dir.'/unit');
  462. if($test_dir !== false && is_dir($test_dir))
  463. return $test_dir;
  464. return realpath($dir.'/tests/unit/');
  465. }
  466. protected function getUnitTestCases($dir,$args)
  467. {
  468. $matches = null;
  469. if(count($args) > 2)
  470. $matches = array_slice($args,2);
  471. $test=new GroupTest(' ');
  472. $this->addTests($test,$dir,true,$matches);
  473. $test->setLabel(implode(' ',$this->matches));
  474. return $test;
  475. }
  476. protected function addTests($test,$path,$recursive=true,$match=null)
  477. {
  478. $dir=opendir($path);
  479. while(($entry=readdir($dir))!==false)
  480. {
  481. if(is_file($path.'/'.$entry) && (preg_match('/[^\s]*test[^\s]*\.php/', strtolower($entry))))
  482. {
  483. if($match==null||($match!=null && $this->hasMatch($match,$entry)))
  484. $test->addTestFile($path.'/'.$entry);
  485. }
  486. if($entry!=='.' && $entry!=='..' && $entry!=='.svn' && is_dir($path.'/'.$entry) && $recursive)
  487. $this->addTests($test,$path.'/'.$entry,$recursive,$match);
  488. }
  489. closedir($dir);
  490. }
  491. protected function hasMatch($match,$entry)
  492. {
  493. $file = strtolower(substr($entry,0,strrpos($entry,'.')));
  494. foreach($match as $m)
  495. {
  496. if(strtolower($m) === $file)
  497. {
  498. $this->matches[] = $m;
  499. return true;
  500. }
  501. }
  502. return false;
  503. }
  504. }
  505. /**
  506. * Create active record skeleton
  507. *
  508. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  509. * @version $Id: prado-cli.php 2498 2008-08-18 00:28:11Z knut $
  510. * @since 3.1
  511. */
  512. class PradoCommandLineActiveRecordGen extends PradoCommandLineAction
  513. {
  514. protected $action = 'generate';
  515. protected $parameters = array('table', 'output');
  516. protected $optional = array('directory', 'soap');
  517. protected $description = 'Generate Active Record skeleton for <table> to <output> file using application.xml in [directory]. May also generate [soap] properties.';
  518. private $_soap=false;
  519. public function performAction($args)
  520. {
  521. $app_dir = count($args) > 3 ? $this->getAppDir($args[3]) : $this->getAppDir();
  522. $this->_soap = count($args) > 4;
  523. if($app_dir !== false)
  524. {
  525. $config = $this->getActiveRecordConfig($app_dir);
  526. $output = $this->getOutputFile($app_dir, $args[2]);
  527. if(is_file($output))
  528. echo "** File $output already exists, skiping. \n";
  529. else if($config !== false && $output !== false)
  530. $this->generateActiveRecord($config, $args[1], $output);
  531. }
  532. return true;
  533. }
  534. protected function getAppDir($dir=".")
  535. {
  536. if(is_dir($dir))
  537. return realpath($dir);
  538. if(false !== ($app_dir = realpath($dir.'/protected/')) && is_dir($app_dir))
  539. return $app_dir;
  540. echo '** Unable to find directory "'.$dir."\".\n";
  541. return false;
  542. }
  543. protected function getXmlFile($app_dir)
  544. {
  545. if(false !== ($xml = realpath($app_dir.'/application.xml')) && is_file($xml))
  546. return $xml;
  547. if(false !== ($xml = realpath($app_dir.'/protected/application.xml')) && is_file($xml))
  548. return $xml;
  549. echo '** Unable to find application.xml in '.$app_dir."\n";
  550. return false;
  551. }
  552. protected function getActiveRecordConfig($app_dir)
  553. {
  554. if(false === ($xml=$this->getXmlFile($app_dir)))
  555. return false;
  556. if(false !== ($app=$this->initializePradoApplication($app_dir)))
  557. {
  558. Prado::using('System.Data.ActiveRecord.TActiveRecordConfig');
  559. foreach($app->getModules() as $module)
  560. if($module instanceof TActiveRecordConfig)
  561. return $module;
  562. echo '** Unable to find TActiveRecordConfig module in '.$xml."\n";
  563. }
  564. return false;
  565. }
  566. protected function getOutputFile($app_dir, $namespace)
  567. {
  568. if(is_file($namespace) && strpos($namespace, $app_dir)===0)
  569. return $namespace;
  570. $file = Prado::getPathOfNamespace($namespace, ".php");
  571. if($file !== null && false !== ($path = realpath(dirname($file))) && is_dir($path))
  572. {
  573. if(strpos($path, $app_dir)===0)
  574. return $file;
  575. }
  576. echo '** Output file '.$file.' must be within directory '.$app_dir."\n";
  577. return false;
  578. }
  579. protected function generateActiveRecord($config, $tablename, $output)
  580. {
  581. $manager = TActiveRecordManager::getInstance();
  582. if($connection = $manager->getDbConnection()) {
  583. $gateway = $manager->getRecordGateway();
  584. $tableInfo = $gateway->getTableInfo($manager->getDbConnection(), $tablename);
  585. if(count($tableInfo->getColumns()) === 0)
  586. {
  587. echo '** Unable to find table or view "'.$tablename.'" in "'.$manager->getDbConnection()->getConnectionString()."\".\n";
  588. return false;
  589. }
  590. else
  591. {
  592. $properties = array();
  593. foreach($tableInfo->getColumns() as $field=>$column)
  594. $properties[] = $this->generateProperty($field,$column);
  595. }
  596. $classname = basename($output, '.php');
  597. $class = $this->generateClass($properties, $tablename, $classname);
  598. echo " Writing class $classname to file $output\n";
  599. file_put_contents($output, $class);
  600. } else {
  601. echo '** Unable to connect to database with ConnectionID=\''.$config->getConnectionID()."'. Please check your settings in application.xml and ensure your database connection is set up first.\n";
  602. }
  603. }
  604. protected function generateProperty($field,$column)
  605. {
  606. $prop = '';
  607. $name = '$'.$field;
  608. $type = $column->getPHPType();
  609. if($this->_soap)
  610. {
  611. $prop .= <<<EOD
  612. /**
  613. * @var $type $name
  614. * @soapproperty
  615. */
  616. EOD;
  617. }
  618. $prop .= "\tpublic $name;";
  619. return $prop;
  620. }
  621. protected function generateClass($properties, $tablename, $class)
  622. {
  623. $props = implode("\n", $properties);
  624. $date = date('Y-m-d h:i:s');
  625. return <<<EOD
  626. <?php
  627. /**
  628. * Auto generated by prado-cli.php on $date.
  629. */
  630. class $class extends TActiveRecord
  631. {
  632. const TABLE='$tablename';
  633. $props
  634. public static function finder(\$className=__CLASS__)
  635. {
  636. return parent::finder(\$className);
  637. }
  638. }
  639. ?>
  640. EOD;
  641. }
  642. }
  643. //run PHP shell
  644. if(class_exists('PHP_Shell_Commands', false))
  645. {
  646. class PHP_Shell_Extensions_ActiveRecord implements PHP_Shell_Extension {
  647. public function register()
  648. {
  649. $cmd = PHP_Shell_Commands::getInstance();
  650. if(Prado::getApplication() !== null)
  651. {
  652. $cmd->registerCommand('#^generate#', $this, 'generate', 'generate <table> <output> [soap]',
  653. 'Generate Active Record skeleton for <table> to <output> file using'.PHP_EOL.
  654. ' application.xml in [directory]. May also generate [soap] properties.');
  655. }
  656. }
  657. public function generate($l)
  658. {
  659. $input = explode(" ", trim($l));
  660. if(count($input) > 2)
  661. {
  662. $app_dir = '.';
  663. if(Prado::getApplication()!==null)
  664. $app_dir = dirname(Prado::getApplication()->getBasePath());
  665. $args = array($input[0],$input[1], $input[2],$app_dir);
  666. if(count($input)>3)
  667. $args = array($input[0],$input[1], $input[2],$app_dir,'soap');
  668. $cmd = new PradoCommandLineActiveRecordGen;
  669. $cmd->performAction($args);
  670. }
  671. else
  672. {
  673. echo "\n Usage: generate table_name Application.pages.RecordClassName\n";
  674. }
  675. }
  676. }
  677. $__shell_exts = PHP_Shell_Extensions::getInstance();
  678. $__shell_exts->registerExtensions(array(
  679. "active-record" => new PHP_Shell_Extensions_ActiveRecord)); /* the :set command */
  680. include_once(dirname(__FILE__).'/3rdParty/PhpShell/php-shell-cmd.php');
  681. }
  682. ?>