Browse Source
* 'master' of git.yiisoft.com:yii2: Added Command::batchInsert() exception cleanup. error handling cleanup. console app cleanup. MVC cleanup MVC WIP MVC WIP MVC WIP cleanup. MVC WIP refactored logging. MVC WIP MVC WIP patched controller and action creation todo updates. Conflicts: framework/console/View.phptags/2.0.0-beta
Carsten Brandt
12 years ago
42 changed files with 1470 additions and 1616 deletions
@ -1,90 +0,0 @@
|
||||
<?php |
||||
/** |
||||
* ActionFilter class file. |
||||
* |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright © 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\base; |
||||
|
||||
/** |
||||
* ActionFilter is the base class for all action filters. |
||||
* |
||||
* A filter can be applied to a controller action at different stages of its life cycle. In particular, |
||||
* it responds to the following events that are raised when an action is being executed: |
||||
* |
||||
* 1. authorize |
||||
* 2. beforeAction |
||||
* 3. beforeRender |
||||
* 4. afterRender |
||||
* 5. afterAction |
||||
* |
||||
* Derived classes may respond to these events by overriding the corresponding methods in this class. |
||||
* For example, to create an access control filter, one may override the [[authorize()]] method. |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class ActionFilter extends Behavior |
||||
{ |
||||
/** |
||||
* @var Controller the owner of this behavior. For action filters, this should be a controller object. |
||||
*/ |
||||
public $owner; |
||||
/** |
||||
* @var array IDs of actions that this filter applies to. |
||||
* If this property is empty or not set, it means this filter applies to all actions. |
||||
* Note that if an action appears in [[except]], the filter will not apply to this action, even |
||||
* if the action also appears in [[only]]. |
||||
* @see exception |
||||
*/ |
||||
public $only; |
||||
/** |
||||
* @var array IDs of actions that this filter does NOT apply to. |
||||
*/ |
||||
public $except; |
||||
|
||||
public function init() |
||||
{ |
||||
$this->owner->on('authorize', array($this, 'handleEvent')); |
||||
$this->owner->on('beforeAction', array($this, 'handleEvent')); |
||||
$this->owner->on('beforeRender', array($this, 'handleEvent')); |
||||
$this->owner->getEventHandlers('afterRender')->insertAt(0, array($this, 'handleEvent')); |
||||
$this->owner->getEventHandlers('afterAction')->insertAt(0, array($this, 'handleEvent')); |
||||
} |
||||
|
||||
public function authorize($event) |
||||
{ |
||||
} |
||||
|
||||
public function beforeAction($event) |
||||
{ |
||||
} |
||||
|
||||
public function beforeRender($event) |
||||
{ |
||||
} |
||||
|
||||
public function afterRender($event) |
||||
{ |
||||
} |
||||
|
||||
public function afterAction($event) |
||||
{ |
||||
} |
||||
|
||||
public function handleEvent($event) |
||||
{ |
||||
if ($this->applyTo($event->action)) { |
||||
$this->{$event->name}($event); |
||||
} |
||||
} |
||||
|
||||
public function applyTo(Action $action) |
||||
{ |
||||
return (empty($this->only) || in_array($action->id, $this->only, false) !== false) |
||||
&& (empty($this->except) || in_array($action->id, $this->except, false) === false); |
||||
} |
||||
} |
@ -0,0 +1,33 @@
|
||||
<?php |
||||
/** |
||||
* InvalidRouteException class file. |
||||
* |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright © 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\base; |
||||
|
||||
/** |
||||
* InvalidRouteException represents an exception caused by an invalid route. |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class InvalidRouteException extends \Exception |
||||
{ |
||||
/** |
||||
* @var boolean whether this exception is caused by end user's mistake (e.g. wrong URL) |
||||
*/ |
||||
public $causedByUser = true; |
||||
|
||||
/** |
||||
* @return string the user-friendly name of this exception |
||||
*/ |
||||
public function getName() |
||||
{ |
||||
return \Yii::t('yii', 'Invalid Route'); |
||||
} |
||||
} |
||||
|
@ -1,149 +0,0 @@
|
||||
<?php |
||||
/** |
||||
* ShellController class file. |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright © 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\console\controllers; |
||||
|
||||
use yii\console\Controller; |
||||
|
||||
/** |
||||
* This command executes the specified Web application and provides a shell for interaction. |
||||
* |
||||
* @property string $help The help information for the shell command. |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class ShellController extends Controller |
||||
{ |
||||
/** |
||||
* @return string the help information for the shell command |
||||
*/ |
||||
public function getHelp() |
||||
{ |
||||
return <<<EOD |
||||
USAGE |
||||
yiic shell [entry-script | config-file] |
||||
|
||||
DESCRIPTION |
||||
This command allows you to interact with a Web application |
||||
on the command line. It also provides tools to automatically |
||||
generate new controllers, views and data models. |
||||
|
||||
It is recommended that you execute this command under |
||||
the directory that contains the entry script file of |
||||
the Web application. |
||||
|
||||
PARAMETERS |
||||
* entry-script | config-file: optional, the path to |
||||
the entry script file or the configuration file for |
||||
the Web application. If not given, it is assumed to be |
||||
the 'index.php' file under the current directory. |
||||
|
||||
EOD; |
||||
} |
||||
|
||||
/** |
||||
* Execute the action. |
||||
* @param array $args command line parameters specific for this command |
||||
*/ |
||||
public function run($args) |
||||
{ |
||||
if(!isset($args[0])) |
||||
$args[0]='index.php'; |
||||
$entryScript=isset($args[0]) ? $args[0] : 'index.php'; |
||||
if(($entryScript=realpath($args[0]))===false || !is_file($entryScript)) |
||||
$this->usageError("{$args[0]} does not exist or is not an entry script file."); |
||||
|
||||
// fake the web server setting |
||||
$cwd=getcwd(); |
||||
chdir(dirname($entryScript)); |
||||
$_SERVER['SCRIPT_NAME']='/'.basename($entryScript); |
||||
$_SERVER['REQUEST_URI']=$_SERVER['SCRIPT_NAME']; |
||||
$_SERVER['SCRIPT_FILENAME']=$entryScript; |
||||
$_SERVER['HTTP_HOST']='localhost'; |
||||
$_SERVER['SERVER_NAME']='localhost'; |
||||
$_SERVER['SERVER_PORT']=80; |
||||
|
||||
// reset context to run the web application |
||||
restore_error_handler(); |
||||
restore_exception_handler(); |
||||
Yii::setApplication(null); |
||||
Yii::setPathOfAlias('application',null); |
||||
|
||||
ob_start(); |
||||
$config=require($entryScript); |
||||
ob_end_clean(); |
||||
|
||||
// oops, the entry script turns out to be a config file |
||||
if(is_array($config)) |
||||
{ |
||||
chdir($cwd); |
||||
$_SERVER['SCRIPT_NAME']='/index.php'; |
||||
$_SERVER['REQUEST_URI']=$_SERVER['SCRIPT_NAME']; |
||||
$_SERVER['SCRIPT_FILENAME']=$cwd.DIRECTORY_SEPARATOR.'index.php'; |
||||
Yii::createWebApplication($config); |
||||
} |
||||
|
||||
restore_error_handler(); |
||||
restore_exception_handler(); |
||||
|
||||
$yiiVersion=Yii::getVersion(); |
||||
echo <<<EOD |
||||
Yii Interactive Tool v1.1 (based on Yii v{$yiiVersion}) |
||||
Please type 'help' for help. Type 'exit' to quit. |
||||
EOD; |
||||
$this->runShell(); |
||||
} |
||||
|
||||
protected function runShell() |
||||
{ |
||||
// disable E_NOTICE so that the shell is more friendly |
||||
error_reporting(E_ALL ^ E_NOTICE); |
||||
|
||||
$_runner_=new CConsoleCommandRunner; |
||||
$_runner_->addCommands(dirname(__FILE__).'/shell'); |
||||
$_runner_->addCommands(Yii::getPathOfAlias('application.commands.shell')); |
||||
if(($_path_=@getenv('YIIC_SHELL_COMMAND_PATH'))!==false) |
||||
$_runner_->addCommands($_path_); |
||||
$_commands_=$_runner_->commands; |
||||
$log=\Yii::$application->log; |
||||
|
||||
while(($_line_=$this->prompt("\n>>"))!==false) |
||||
{ |
||||
$_line_=trim($_line_); |
||||
if($_line_==='exit') |
||||
return; |
||||
try |
||||
{ |
||||
$_args_=preg_split('/[\s,]+/',rtrim($_line_,';'),-1,PREG_SPLIT_NO_EMPTY); |
||||
if(isset($_args_[0]) && isset($_commands_[$_args_[0]])) |
||||
{ |
||||
$_command_=$_runner_->createCommand($_args_[0]); |
||||
array_shift($_args_); |
||||
$_command_->init(); |
||||
$_command_->run($_args_); |
||||
} |
||||
else |
||||
echo eval($_line_.';'); |
||||
} |
||||
catch(Exception $e) |
||||
{ |
||||
if($e instanceof ShellException) |
||||
echo $e->getMessage(); |
||||
else |
||||
echo $e; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
class ShellException extends CException |
||||
{ |
||||
} |
@ -1,103 +0,0 @@
|
||||
<?php |
||||
/** |
||||
* ReflectionHelper class file. |
||||
* |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright © 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\util; |
||||
|
||||
use yii\base\Exception; |
||||
|
||||
/** |
||||
* ReflectionHelper |
||||
* |
||||
* @author Qiang Xue <qiang.xue@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class ReflectionHelper |
||||
{ |
||||
/** |
||||
* Prepares parameters so that they can be bound to the specified method. |
||||
* This method converts the input parameters into an array that can later be |
||||
* passed to `call_user_func_array()` when calling the specified method. |
||||
* The conversion is based on the matching of method parameter names |
||||
* and the input array keys. For example, |
||||
* |
||||
* ~~~ |
||||
* class Foo { |
||||
* function bar($a, $b) { ... } |
||||
* } |
||||
* $object = new Foo; |
||||
* $params = array('b' => 2, 'c' => 3, 'a' => 1); |
||||
* var_export(ReflectionHelper::extractMethodParams($object, 'bar', $params)); |
||||
* // output: array('a' => 1, 'b' => 2); |
||||
* ~~~ |
||||
* |
||||
* @param object|string $object the object or class name that owns the specified method |
||||
* @param string $method the method name |
||||
* @param array $params the parameters in terms of name-value pairs |
||||
* @return array parameters that are needed by the method only and |
||||
* can be passed to the method via `call_user_func_array()`. |
||||
* @throws Exception if any required method parameter is not found in the given parameters |
||||
*/ |
||||
public static function extractMethodParams($object, $method, $params) |
||||
{ |
||||
$m = new \ReflectionMethod($object, $method); |
||||
$ps = array(); |
||||
foreach ($m->getParameters() as $param) { |
||||
$name = $param->getName(); |
||||
if (array_key_exists($name, $params)) { |
||||
$ps[$name] = $params[$name]; |
||||
} elseif ($param->isDefaultValueAvailable()) { |
||||
$ps[$name] = $param->getDefaultValue(); |
||||
} else { |
||||
throw new Exception(\Yii::t('yii', 'Missing required parameter "{name}".', array('{name}' => $name))); |
||||
} |
||||
} |
||||
return $ps; |
||||
} |
||||
|
||||
/** |
||||
* Initializes an object with the given parameters. |
||||
* Only the public non-static properties of the object will be initialized, and their names must |
||||
* match the given parameter names. For example, |
||||
* |
||||
* ~~~ |
||||
* class Foo { |
||||
* public $a; |
||||
* protected $b; |
||||
* } |
||||
* $object = new Foo; |
||||
* $params = array('b' => 2, 'c' => 3, 'a' => 1); |
||||
* $remaining = ReflectionHelper::bindObjectParams($object, $params); |
||||
* var_export($object); // output: $object->a = 1; $object->b = null; |
||||
* var_export($remaining); // output: array('b' => 2, 'c' => 3); |
||||
* ~~~ |
||||
* |
||||
* @param object $object the object whose properties are to be initialized |
||||
* @param array $params the input parameters to be used to initialize the object |
||||
* @return array the remaining unused input parameters |
||||
*/ |
||||
public static function initObjectWithParams($object, $params) |
||||
{ |
||||
if (empty($params)) { |
||||
return array(); |
||||
} |
||||
|
||||
$class = new \ReflectionClass(get_class($object)); |
||||
foreach ($params as $name => $value) { |
||||
if ($class->hasProperty($name)) { |
||||
$property = $class->getProperty($name); |
||||
if ($property->isPublic() && !$property->isStatic()) { |
||||
$object->$name = $value; |
||||
unset($params[$name]); |
||||
} |
||||
} |
||||
} |
||||
|
||||
return $params; |
||||
} |
||||
} |
Loading…
Reference in new issue