[ Index ]
 

Code source de PRADO 3.0.6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/framework/ -> prado-cli.php (source)

   1  #!/usr/bin/env php
   2  <?php
   3  
   4  /**
   5   * Prado command line developer tools.
   6   *
   7   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
   8   * @link http://www.pradosoft.com/
   9   * @copyright Copyright &copy; 2006 PradoSoft
  10   * @license http://www.pradosoft.com/license/
  11   * @version $Id: prado-cli.php 1430 2006-09-19 22:34:28Z wei $
  12   */
  13  
  14  if(!isset($_SERVER['argv']) || php_sapi_name()!=='cli')
  15      die('Must be run from the command line');
  16  
  17  require_once(dirname(__FILE__).'/prado.php');
  18  
  19  //stub application class
  20  class PradoShellApplication extends TApplication
  21  {
  22  	public function run()
  23      {
  24          $this->initApplication();
  25      }
  26  }
  27  
  28  restore_exception_handler();
  29  
  30  //register action classes
  31  PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineCreateProject');
  32  PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineCreateTests');
  33  PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLinePhpShell');
  34  PradoCommandLineInterpreter::getInstance()->addActionClass('PradoCommandLineUnitTest');
  35  
  36  //run it;
  37  PradoCommandLineInterpreter::getInstance()->run($_SERVER['argv']);
  38  
  39  //run PHP shell
  40  if(count($_SERVER['argv']) > 1 && strtolower($_SERVER['argv'][1])==='shell')
  41  {
  42  	function __shell_print_var($shell,$var)
  43      {
  44          if(!$shell->has_semicolon) echo Prado::varDump($var);
  45      }
  46      include_once(dirname(__FILE__).'/3rdParty/PhpShell/php-shell-cmd.php');
  47  }
  48  
  49  
  50  /**************** END CONFIGURATION **********************/
  51  
  52  /**
  53   * PradoCommandLineInterpreter Class
  54   *
  55   * Command line interface, configures the action classes and dispatches the command actions.
  56   *
  57   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  58   * @version $Id: prado-cli.php 1430 2006-09-19 22:34:28Z wei $
  59   * @since 3.0.5
  60   */
  61  class PradoCommandLineInterpreter
  62  {
  63      /**
  64       * @var array command action classes
  65       */
  66      protected $_actions=array();
  67  
  68      /**
  69       * @param string action class name
  70       */
  71  	public function addActionClass($class)
  72      {
  73          $this->_actions[$class] = new $class;
  74      }
  75  
  76      /**
  77       * @return PradoCommandLineInterpreter static instance
  78       */
  79  	public static function getInstance()
  80      {
  81          static $instance;
  82          if(is_null($instance))
  83              $instance = new self;
  84          return $instance;
  85      }
  86  
  87      /**
  88       * Dispatch the command line actions.
  89       * @param array command line arguments
  90       */
  91  	public function run($args)
  92      {
  93          echo "Command line tools for Prado ".Prado::getVersion().".\n";
  94  
  95          if(count($args) > 1)
  96              array_shift($args);
  97          $valid = false;
  98          foreach($this->_actions as $class => $action)
  99          {
 100              if($action->isValidAction($args))
 101              {
 102                  $valid |= $action->performAction($args);
 103                  break;
 104              }
 105              else
 106              {
 107                  $valid = false;
 108              }
 109          }
 110          if(!$valid)
 111              $this->printHelp();
 112      }
 113  
 114      /**
 115       * Print command line help, default action.
 116       */
 117  	public function printHelp()
 118      {
 119          echo "usage: php prado-cli.php action <parameter> [optional]\n";
 120          echo "example: php prado-cli.php -c mysite\n\n";
 121          echo "actions:\n";
 122          foreach($this->_actions as $action)
 123              echo $action->renderHelp();
 124      }
 125  }
 126  
 127  /**
 128   * Base class for command line actions.
 129   *
 130   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 131   * @version $Id: prado-cli.php 1430 2006-09-19 22:34:28Z wei $
 132   * @since 3.0.5
 133   */
 134  abstract class PradoCommandLineAction
 135  {
 136      /**
 137       * Execute the action.
 138       * @param array command line parameters
 139       * @return boolean true if action was handled
 140       */
 141      abstract public function performAction($args);
 142  
 143  	protected function createDirectory($dir, $mask)
 144      {
 145          if(!is_dir($dir))
 146          {
 147              mkdir($dir);
 148              echo "creating $dir\n";
 149          }
 150          if(is_dir($dir))
 151              chmod($dir, $mask);
 152      }
 153  
 154  	protected function createFile($filename, $content)
 155      {
 156          if(!is_file($filename))
 157          {
 158              file_put_contents($filename, $content);
 159              echo "creating $filename\n";
 160          }
 161      }
 162  
 163  	public function isValidAction($args)
 164      {
 165          return strtolower($args[0]) === $this->action &&
 166                  count($args)-1 >= count($this->parameters);
 167      }
 168  
 169  	public function renderHelp()
 170      {
 171          $params = array();
 172          foreach($this->parameters as $v)
 173              $params[] = '<'.$v.'>';
 174          $parameters = join($params, ' ');
 175          $options = array();
 176          foreach($this->optional as $v)
 177              $options[] = '['.$v.']';
 178          $optional = (strlen($parameters) ? ' ' : ''). join($options, ' ');
 179          $description='';
 180          foreach(explode("\n", wordwrap($this->description,65)) as $line)
 181              $description .= '    '.$line."\n";
 182          return <<<EOD
 183    {$this->action} {$parameters}{$optional}
 184  {$description}
 185  
 186  EOD;
 187      }
 188  
 189  	protected function initializePradoApplication($directory)
 190      {
 191          $app_dir = realpath($directory.'/protected/');
 192          if($app_dir !== false)
 193          {
 194              $app = new PradoShellApplication($app_dir);
 195              $app->run();
 196              $dir = substr(str_replace(realpath('./'),'',$app_dir),1);
 197  
 198              echo '** Loaded Prado appplication in directory "'.$dir."\".\n";
 199          }
 200          else
 201              echo '** Unable to load Prado application in directory "'.$directory."\".\n";
 202      }
 203  
 204  }
 205  
 206  /**
 207   * Create a Prado project skeleton, including directories and files.
 208   *
 209   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 210   * @version $Id: prado-cli.php 1430 2006-09-19 22:34:28Z wei $
 211   * @since 3.0.5
 212   */
 213  class PradoCommandLineCreateProject extends PradoCommandLineAction
 214  {
 215      protected $action = '-c';
 216      protected $parameters = array('directory');
 217      protected $optional = array();
 218      protected $description = 'Creates a Prado project skeleton for the given <directory>.';
 219  
 220  	public function performAction($args)
 221      {
 222          $this->createNewPradoProject($args[1]);
 223          return true;
 224      }
 225  
 226      /**
 227       * Functions to create new prado project.
 228       */
 229  	protected function createNewPradoProject($dir)
 230      {
 231          if(strlen(trim($dir)) == 0)
 232              return;
 233  
 234          $rootPath = realpath(dirname(trim($dir)));
 235  
 236          $basePath = $rootPath.'/'.basename($dir);
 237          $assetPath = $basePath.'/assets';
 238          $protectedPath  = $basePath.'/protected';
 239          $runtimePath = $basePath.'/protected/runtime';
 240          $pagesPath = $protectedPath.'/pages';
 241  
 242          $indexFile = $basePath.'/index.php';
 243          $htaccessFile = $protectedPath.'/.htaccess';
 244          $defaultPageFile = $pagesPath.'/Home.page';
 245  
 246          $this->createDirectory($basePath, 0755);
 247          $this->createDirectory($assetPath,0777);
 248          $this->createDirectory($protectedPath,0755);
 249          $this->createDirectory($runtimePath,0777);
 250          $this->createDirectory($pagesPath,0755);
 251  
 252          $this->createFile($indexFile, $this->renderIndexFile());
 253          $this->createFile($htaccessFile, $this->renderHtaccessFile());
 254          $this->createFile($defaultPageFile, $this->renderDefaultPage());
 255      }
 256  
 257  	protected function renderIndexFile()
 258      {
 259          $framework = realpath(dirname(__FILE__)).'/prado.php';
 260  return '<?php
 261  $frameworkPath=\''.$framework.'\';
 262  
 263  /** The directory checks may be removed if performance is required **/
 264  $basePath=dirname(__FILE__);
 265  $assetsPath=$basePath."/assets";
 266  $runtimePath=$basePath."/protected/runtime";
 267  
 268  if(!is_file($frameworkPath))
 269      die("Unable to find prado framework path $frameworkPath.");
 270  if(!is_writable($assetsPath))
 271      die("Please make sure that the directory $assetsPath is writable by Web server process.");
 272  if(!is_writable($runtimePath))
 273      die("Please make sure that the directory $runtimePath is writable by Web server process.");
 274  
 275  
 276  require_once($frameworkPath);
 277  
 278  $application=new TApplication;
 279  $application->run();
 280  
 281  ?>';
 282      }
 283  
 284  	protected function renderHtaccessFile()
 285      {
 286          return 'deny from all';
 287      }
 288  
 289  
 290  	protected function renderDefaultPage()
 291      {
 292  return <<<EOD
 293  <h1>Welcome to Prado!</h1>
 294  EOD;
 295      }
 296  }
 297  
 298  /**
 299   * Creates test fixtures for a Prado application.
 300   *
 301   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 302   * @version $Id: prado-cli.php 1430 2006-09-19 22:34:28Z wei $
 303   * @since 3.0.5
 304   */
 305  class PradoCommandLineCreateTests extends PradoCommandLineAction
 306  {
 307      protected $action = '-t';
 308      protected $parameters = array('directory');
 309      protected $optional = array();
 310      protected $description = 'Create test fixtures in the given <directory>.';
 311  
 312  	public function performAction($args)
 313      {
 314          $this->createTestFixtures($args[1]);
 315          return true;
 316      }
 317  
 318  	protected function createTestFixtures($dir)
 319      {
 320          if(strlen(trim($dir)) == 0)
 321              return;
 322  
 323          $rootPath = realpath(dirname(trim($dir)));
 324          $basePath = $rootPath.'/'.basename($dir);
 325  
 326          $tests = $basePath.'/tests';
 327          $unit_tests = $tests.'/unit';
 328          $functional_tests = $tests.'/functional';
 329  
 330          $this->createDirectory($tests,0755);
 331          $this->createDirectory($unit_tests,0755);
 332          $this->createDirectory($functional_tests,0755);
 333  
 334          $unit_test_index = $tests.'/unit.php';
 335          $functional_test_index = $tests.'/functional.php';
 336  
 337          $this->createFile($unit_test_index, $this->renderUnitTestFixture());
 338          $this->createFile($functional_test_index, $this->renderFunctionalTestFixture());
 339      }
 340  
 341  	protected function renderUnitTestFixture()
 342      {
 343          $tester = realpath(dirname(__FILE__).'/../tests/test_tools/unit_tests.php');
 344  return '<?php
 345  
 346  include_once \''.$tester.'\';
 347  
 348  $app_directory = "../protected";
 349  $test_cases = dirname(__FILE__)."/unit";
 350  
 351  $tester = new PradoUnitTester($test_cases, $app_directory);
 352  $tester->run(new HtmlReporter());
 353  
 354  ?>';
 355      }
 356  
 357  	protected function renderFunctionalTestFixture()
 358      {
 359          $tester = realpath(dirname(__FILE__).'/../tests/test_tools/functional_tests.php');
 360  return '<?php
 361  
 362  include_once \''.$tester.'\';
 363  
 364  $test_cases = dirname(__FILE__)."/functional";
 365  
 366  $tester=new PradoFunctionalTester($test_cases);
 367  $tester->run(new SimpleReporter());
 368  
 369  ?>';
 370      }
 371  
 372  }
 373  
 374  /**
 375   * Creates and run a Prado application in a PHP Shell.
 376   *
 377   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 378   * @version $Id: prado-cli.php 1430 2006-09-19 22:34:28Z wei $
 379   * @since 3.0.5
 380   */
 381  class PradoCommandLinePhpShell extends PradoCommandLineAction
 382  {
 383      protected $action = 'shell';
 384      protected $parameters = array();
 385      protected $optional = array('directory');
 386      protected $description = 'Runs a PHP interactive interpreter. Initializes the Prado application in the given [directory].';
 387  
 388  	public function performAction($args)
 389      {
 390          if(count($args) > 1)
 391              $this->initializePradoApplication($args[1]);
 392          return true;
 393      }
 394  }
 395  
 396  /**
 397   * Runs unit test cases.
 398   *
 399   * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
 400   * @version $Id: prado-cli.php 1430 2006-09-19 22:34:28Z wei $
 401   * @since 3.0.5
 402   */
 403  class PradoCommandLineUnitTest extends PradoCommandLineAction
 404  {
 405      protected $action = 'test';
 406      protected $parameters = array('directory');
 407      protected $optional = array('testcase ...');
 408      protected $description = 'Runs all unit test cases in the given <directory>. Use [testcase] option to run specific test cases.';
 409  
 410      protected $matches = array();
 411  
 412  	public function performAction($args)
 413      {
 414          $dir = realpath($args[1]);
 415          if($dir !== false)
 416              $this->runUnitTests($dir,$args);
 417          else
 418              echo '** Unable to find directory "'.$args[1]."\".\n";
 419          return true;
 420      }
 421  
 422  	protected function initializeTestRunner()
 423      {
 424          $TEST_TOOLS = realpath(dirname(__FILE__).'/../tests/test_tools/');
 425  
 426          require_once ($TEST_TOOLS.'/simpletest/unit_tester.php');
 427          require_once ($TEST_TOOLS.'/simpletest/web_tester.php');
 428          require_once ($TEST_TOOLS.'/simpletest/mock_objects.php');
 429          require_once ($TEST_TOOLS.'/simpletest/reporter.php');
 430      }
 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  
 438          $this->initializeTestRunner();
 439          $test_dir = $this->getTestDir($dir);
 440          if($test_dir !== false)
 441          {
 442              $test =$this->getUnitTestCases($test_dir,$args);
 443              $running_dir = substr(str_replace(realpath('./'),'',$test_dir),1);
 444              echo 'Running unit tests in directory "'.$running_dir."\":\n";
 445              $test->run(new TextReporter());
 446          }
 447          else
 448          {
 449              $running_dir = substr(str_replace(realpath('./'),'',$dir),1);
 450              echo '** Unable to find test directory "'.$running_dir.'/unit" or "'.$running_dir.'/tests/unit".'."\n";
 451          }
 452      }
 453  
 454  	protected function getAppDir($dir)
 455      {
 456          $app_dir = realpath($dir.'/protected');
 457          if($app_dir !== false)
 458              return $app_dir;
 459          return realpath($dir.'/../protected');
 460      }
 461  
 462  	protected function getTestDir($dir)
 463      {
 464          $test_dir = realpath($dir.'/unit');
 465          if($test_dir !== false)
 466              return $test_dir;
 467          return realpath($dir.'/tests/unit/');
 468      }
 469  
 470  	protected function getUnitTestCases($dir,$args)
 471      {
 472          $matches = null;
 473          if(count($args) > 2)
 474              $matches = array_slice($args,2);
 475          $test=new GroupTest(' ');
 476          $this->addTests($test,$dir,true,$matches);
 477          $test->setLabel(implode(' ',$this->matches));
 478          return $test;
 479      }
 480  
 481  	protected function addTests($test,$path,$recursive=true,$match=null)
 482      {
 483          $dir=opendir($path);
 484          while(($entry=readdir($dir))!==false)
 485          {
 486              if(is_file($path.'/'.$entry) && (preg_match('/[^\s]*test[^\s]*\.php/', strtolower($entry))))
 487              {
 488                  if($match==null||($match!=null && $this->hasMatch($match,$entry)))
 489                      $test->addTestFile($path.'/'.$entry);
 490              }
 491              if($entry!=='.' && $entry!=='..' && $entry!=='.svn' && is_dir($path.'/'.$entry) && $recursive)
 492                  $this->addTests($test,$path.'/'.$entry,$recursive,$match);
 493          }
 494          closedir($dir);
 495      }
 496  
 497  	protected function hasMatch($match,$entry)
 498      {
 499          $file = strtolower(substr($entry,0,strrpos($entry,'.')));
 500          foreach($match as $m)
 501          {
 502              if(strtolower($m) === $file)
 503              {
 504                  $this->matches[] = $m;
 505                  return true;
 506              }
 507          }
 508          return false;
 509      }
 510  }
 511  
 512  ?>


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7