| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * TShellAction class file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Brad Anderson <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @link https://github.com/pradosoft/prado | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license https://github.com/pradosoft/prado/blob/master/LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @package Prado\Shell | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Prado\Shell; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Prado\IO\ITextWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Prado\Prado; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Base class for command line actions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author Wei Zhuo <weizhuo[at]gmail[dot]com> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @package Prado\Shell | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @since 3.0.5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | abstract class TShellAction extends \Prado\TComponent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	protected $action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	protected $parameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	protected $optional; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	protected $description; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	protected $_outWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * @return ITextWriter the writer for the class | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 	public function getWriter(): ITextWriter | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 		return $this->_outWriter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * @@param ITextWriter $writer the writer for the class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	public function setWriter(ITextWriter $writer) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		$this->_outWriter = $writer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * Execute the action. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * @param array $args command line parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * @return bool true if action was handled | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	abstract public function performAction($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * Creates a directory and sets its mode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 * @param string $dir directory name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 * @param int $mask directory mode mask suitable for chmod() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	protected function createDirectory($dir, $mask) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		if (!is_dir($dir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			mkdir($dir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			$this->_outWriter->writeLine("creating $dir"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		if (is_dir($dir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 			chmod($dir, $mask); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * Creates a file and fills it with content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * @param string $filename file name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @param int $content file contents | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	protected function createFile($filename, $content) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		if (!is_file($filename)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			file_put_contents($filename, $content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			$this->_outWriter->writeLine("creating $filename"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	 * Checks if specified parameters are suitable for the specified action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * @param array $args parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	public function isValidAction($args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		return 0 == strcasecmp($args[0], $this->action) && | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			count($args) - 1 >= count($this->parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	public function renderHelp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		$params = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		foreach ($this->parameters as $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			$params[] = '<' . $v . '>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 		$parameters = implode(' ', $params); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		$options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		foreach ($this->optional as $v) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 			$options[] = '[' . $v . ']'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		$optional = (strlen($parameters) ? ' ' : '') . implode(' ', $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		$description = $this->getWriter()->wrapText($this->description, 5); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		$action = $this->getWriter()->format($this->action, [TShellWriter::BLUE, TShellWriter::BOLD]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		$parameters = $this->getWriter()->format($parameters, [TShellWriter::BLUE, TShellWriter::BOLD]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		$optional = $this->getWriter()->format($optional, [TShellWriter::BLUE]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		$description = $this->getWriter()->format($description, TShellWriter::DARK_GRAY); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		return <<<EOD | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  - {$action} {$parameters}{$optional} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      {$description} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | EOD; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 * Initalize a Prado application inside the specified directory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * @param string $directory directory name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 * @return false|TApplication | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	protected function initializePradoApplication($directory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 		$_SERVER['SCRIPT_FILENAME'] = $directory . '/index.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		$app_dir = realpath($directory . '/protected/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 		if ($app_dir !== false && is_dir($app_dir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 			if (Prado::getApplication() === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 				$app = new TShellApplication($app_dir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 				$app->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 				$dir = substr(str_replace(realpath('./'), '', $app_dir), 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 				TShellInterpreter::getInstance()->printGreeting(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 				$this->_outWriter->writeLine('** Loaded PRADO appplication in directory "' . $dir . "\"."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			return Prado::getApplication(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			TShellInterpreter::getInstance()->printGreeting(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 			$this->_outWriter->writeLine('+' . str_repeat('-', 77) . "+"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 			$this->_outWriter->writeLine('** Unable to load PRADO application in directory "' . $directory . "\"."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 			$this->_outWriter->writeLine('+' . str_repeat('-', 77) . "+"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	 * @param string $app_dir application directory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	 * @return false|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	protected function getAppConfigFile($app_dir) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 		if (false !== ($xml = realpath($app_dir . '/application.xml')) && is_file($xml)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 			return $xml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		if (false !== ($xml = realpath($app_dir . '/protected/application.xml')) && is_file($xml)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 			return $xml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		if (false !== ($php = realpath($app_dir . '/application.php')) && is_file($php)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 			return $php; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 		if (false !== ($php = realpath($app_dir . '/protected/application.php')) && is_file($php)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 			return $php; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		$this->_outWriter->writeLine('** Unable to find application.xml or application.php in ' . $app_dir . "."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 174 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |  |