Browse Source

...

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
b51aa4ed65
  1. 37
      framework/base/Controller.php
  2. 43
      framework/base/RenderEvent.php
  3. 136
      framework/caching/DirectoryDependency.php

37
framework/base/Controller.php

@ -16,9 +16,7 @@ namespace yii\base;
* *
* 1. [[authorize]] * 1. [[authorize]]
* 2. [[beforeAction]] * 2. [[beforeAction]]
* 3. [[beforeRender]] * 3. [[afterAction]]
* 4. [[afterRender]]
* 5. [[afterAction]]
* *
* @property array $actionParams the request parameters (name-value pairs) to be used for action parameter binding * @property array $actionParams the request parameters (name-value pairs) to be used for action parameter binding
* @property string $route the route (module ID, controller ID and action ID) of the current request. * @property string $route the route (module ID, controller ID and action ID) of the current request.
@ -283,40 +281,9 @@ class Controller extends Component implements Initable
$this->trigger(__METHOD__, new ActionEvent($action)); $this->trigger(__METHOD__, new ActionEvent($action));
} }
/**
* This method is invoked right before an action renders its result using [[render()]].
* @param string $view the view to be rendered
* @return boolean whether the action should continue to render.
*/
public function beforeRender($view)
{
$event = new RenderEvent($view);
$this->trigger(__METHOD__, $event);
return $event->isValid;
}
/**
* This method is invoked right after an action renders its result using [[render()]].
* @param string $view the view just rendered
* @param string $content the content to be displayed
* @return string the content to be displayed. This may be the same as the input content or the one
* modified by event handlers.
*/
public function afterRender($view, $content)
{
$event = new RenderEvent($view, $content);
$this->trigger(__METHOD__, $event);
return $event->content;
}
public function render($view, $params = array()) public function render($view, $params = array())
{ {
if ($this->beforeRender($view)) { return $this->createView()->render($view, $params);
$content = $this->createView()->render($view, $params);
$content = $this->afterRender($view, $content);
return $content;
}
return '';
} }
public function renderText($text) public function renderText($text)

43
framework/base/RenderEvent.php

@ -1,43 +0,0 @@
<?php
/**
* RenderEvent class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* RenderEvent represents the event parameter used for when calling [[Controller::render()]].
*
* By setting the [[isValid]] property, one may control whether to continue the rendering.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class RenderEvent extends Event
{
/**
* @var string the view currently being rendered
*/
public $view;
/**
* @var string the content to be displayed
*/
public $content;
/**
* @var boolean whether the action is in valid state and its life cycle should proceed.
*/
public $isValid = true;
/**
* Constructor.
* @param string $view the view currently being rendered
*/
public function __construct($view)
{
$this->view = $view;
}
}

136
framework/caching/DirectoryDependency.php

@ -1,136 +0,0 @@
<?php
/**
* DirectoryDependency class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\caching;
/**
* DirectoryDependency represents a dependency based on change of a directory.
*
* DirectoryDependency performs dependency checking based on the
* modification time of the files contained in the specified directory.
* The directory being checked is specified via {@link directory}.
*
* By default, all files under the specified directory and subdirectories
* will be checked. If the last modification time of any of them is changed
* or if different number of files are contained in a directory, the dependency
* is reported as changed. By specifying {@link recursiveLevel},
* one can limit the checking to a certain depth of the directory.
*
* Note, dependency checking for a directory is expensive because it involves
* accessing modification time of multiple files under the directory.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class DirectoryDependency extends Dependency
{
/**
* @var string the directory whose change is used to determine if the dependency has been changed.
* If any of the files under the directory is changed, the dependency is considered as changed.
*/
public $directory;
/**
* @var integer the depth of the subdirectories to be recursively checked.
* If the value is less than 0, it means unlimited depth.
* If the value is 0, it means checking the files directly under the specified directory.
*/
public $recursiveLevel = -1;
/**
* @var string the regular expression matching valid file/directory names.
* Only the matching files or directories will be checked for changes.
* Defaults to null, meaning all files/directories will qualify.
*/
public $namePattern;
/**
* Constructor.
* @param string $directory the directory to be checked
*/
public function __construct($directory = null)
{
$this->directory = $directory;
}
/**
* Generates the data needed to determine if dependency has been changed.
* This method returns the modification timestamps for files under the directory.
* @return mixed the data needed to determine if dependency has been changed.
*/
protected function generateDependencyData()
{
if ($this->directory !== null) {
return $this->generateTimestamps($this->directory);
} else {
throw new CException(Yii::t('yii', 'DirectoryDependency.directory cannot be empty.'));
}
}
/**
* Determines the last modification time for files under the directory.
* This method may go recursively into subdirectories if {@link recursiveLevel} is not 0.
* @param string $directory the directory name
* @param integer $level level of the recursion
* @return array list of file modification time indexed by the file path
*/
protected function generateTimestamps($directory, $level = 0)
{
if (($dir = @opendir($directory)) === false) {
throw new CException(Yii::t('yii', '"{path}" is not a valid directory.',
array('{path}' => $directory)));
}
$timestamps = array();
while (($file = readdir($dir)) !== false) {
$path = $directory . DIRECTORY_SEPARATOR . $file;
if ($file === '.' || $file === '..') {
continue;
}
if ($this->namePattern !== null && !preg_match($this->namePattern, $file)) {
continue;
}
if (is_file($path)) {
if ($this->validateFile($path)) {
$timestamps[$path] = filemtime($path);
}
} else {
if (($this->recursiveLevel < 0 || $level < $this->recursiveLevel) && $this->validateDirectory($path)) {
$timestamps = array_merge($timestamps, $this->generateTimestamps($path, $level + 1));
}
}
}
closedir($dir);
return $timestamps;
}
/**
* Checks to see if the file should be checked for dependency.
* This method is invoked when dependency of the whole directory is being checked.
* By default, it always returns true, meaning the file should be checked.
* You may override this method to check only certain files.
* @param string $fileName the name of the file that may be checked for dependency.
* @return boolean whether this file should be checked.
*/
protected function validateFile($fileName)
{
return true;
}
/**
* Checks to see if the specified subdirectory should be checked for dependency.
* This method is invoked when dependency of the whole directory is being checked.
* By default, it always returns true, meaning the subdirectory should be checked.
* You may override this method to check only certain subdirectories.
* @param string $directory the name of the subdirectory that may be checked for dependency.
* @return boolean whether this subdirectory should be checked.
*/
protected function validateDirectory($directory)
{
return true;
}
}
Loading…
Cancel
Save