这个控制架构脚本看起来如何?

时间:2010-09-24 14:02:46

标签: php oop coding-style feedback

我正在寻找有关我的控制架构脚本(包括在下面)的反馈。具体来说,我正在寻找有关脚本的设计,组织,评论和格式的反馈。我喜欢PHP编程作为一种爱好,我希望了解我可以改进代码的地方。

提前致谢!

class FrontController extends ActionController {

//Declaring variable(s)
private static $instance;
protected $controller;

//Class construct method
public function __construct() {}

//Starts new instance of this class with a singleton pattern
public static function getInstance() {
    if(!self::$instance) {
        self::$instance = new self();
    }
    return self::$instance;
}

public function dispatch($throwExceptions = false) {

    /* Checks for the GET variables $module and $action, and, if present,
     * strips them down with a regular expression function with a white
     * list of allowed characters, removing anything that is not a letter,
     * number, underscore or hyphen.
     */
    $regex  = '/[^-_A-z0-9]+/';
    $module = isset($_GET['module']) ? preg_replace($regex, '', $_GET['module']) : 'home';
    $action = isset($_GET['action']) ? preg_replace($regex, '', $_GET['action']) : 'frontpage';

    /* Generates Actions class filename (example: HomeActions) and path to
     * that class (example: home/HomeActions.php), checks if $file is a
     * valid file, and then, if so, requires that file.
     */
    $class = ucfirst($module) . 'Actions';
    $file  = $this->pageDir . '/' . $module . '/' . $class . '.php';

    try {

        //Checks for existance of file
        if (!is_file($file)) {
            throw new Exception('File not found!');
        }

        //Includes file
        require_once $file;

        /* Creates a new instance of the Actions class (example: $controller
         * = new HomeActions();), and passes the registry variable to the
         * ActionController class.
         */
        $controller = new $class();
        $controller->setRegistry($this->registry);

        //Trys the setModule method in the ActionController class
        $controller->setModule($module);

        /* The ActionController dispatchAction method checks if the method
         * exists, then runs the displayView function in the
         * ActionController class.
         */    
        $controller->dispatchAction($action);

    } catch(Exception $error) {

        /* An exception has occurred, and will be displayed if
         * $throwExceptions is set to true.
         */
        if($throwExceptions) {
            echo $error;
        }
    }
}
}

abstract class ActionController {

//Declaring variable(s)
protected $registry;
protected $module;
protected $registryItems = array();

//Class construct method
public function __construct(){}

public function setRegistry($registry) {

    //Sets the registry object
    $this->registry = $registry;

    /* Once the registry is loaded, the controller root directory path is
     * set from the registry.  This path is needed for the controller
     * classes to work properly.
     */
    $this->setPageDir();
}

//Sets the controller root directory from the value stored in the registry
public function setPageDir() {
    $this->pageDir = $this->registry->get('pageDir');
}

//Sets the module
public function setModule($module) {
    $this->module = $module;
}

//Gets the module
public function getModule() {
    return $this->module;
}

/* Checks for actionMethod in the Actions class (example: doFrontpage()
 * within home/HomeActions.php) with the method_exists function and, if
 * present, the actionMethod and displayView functions are executed.
 */  
public function dispatchAction($action) {
    $actionMethod = 'do' . ucfirst($action);
    if (!method_exists($this, $actionMethod)) {
        throw new Exception('Action not found!');
    }
    $this->$actionMethod();
    $this->displayView($action);
}

public function displayView($action) {
    if (!is_file($this->pageDir . '/' . $this->getModule() . '/' . $action . 'View.php')) {
        throw new Exception('View not found!');
    }

    //Sets $this->actionView to the path of the action View file
    $this->actionView = $this->pageDir . '/' . $this->getModule() . '/' . $action . 'View.php';

    //Sets path of the action View file into the registry
    $this->registry->set('actionView', $this->actionView);

    //Includes template file within which the action View file is included
    require_once $this->pageDir . '/default.tpl';
}
}

class Registry {

//Declaring variables
private $store;

//Class constructor
public function __construct() {}

//Sets registry variable
public function set($label, $object) {
    $this->store[$label] = $object;
}

//Gets registry variable    
public function get($label) {
    if(isset($this->store[$label])) {
        return $this->store[$label];
    } else {
        return false;          
    }
}

//Adds outside array of registry values to $this->store array
public function addRegistryArray($registryItems) {
    foreach ($registryItems as $key => $value) {
        $this->set($key, $value);
    }
}

//Returns registry array
public function getRegistryArray() {
    return $this->store;
}
}

class FrontController extends ActionController { //Declaring variable(s) private static $instance; protected $controller; //Class construct method public function __construct() {} //Starts new instance of this class with a singleton pattern public static function getInstance() { if(!self::$instance) { self::$instance = new self(); } return self::$instance; } public function dispatch($throwExceptions = false) { /* Checks for the GET variables $module and $action, and, if present, * strips them down with a regular expression function with a white * list of allowed characters, removing anything that is not a letter, * number, underscore or hyphen. */ $regex = '/[^-_A-z0-9]+/'; $module = isset($_GET['module']) ? preg_replace($regex, '', $_GET['module']) : 'home'; $action = isset($_GET['action']) ? preg_replace($regex, '', $_GET['action']) : 'frontpage'; /* Generates Actions class filename (example: HomeActions) and path to * that class (example: home/HomeActions.php), checks if $file is a * valid file, and then, if so, requires that file. */ $class = ucfirst($module) . 'Actions'; $file = $this->pageDir . '/' . $module . '/' . $class . '.php'; try { //Checks for existance of file if (!is_file($file)) { throw new Exception('File not found!'); } //Includes file require_once $file; /* Creates a new instance of the Actions class (example: $controller * = new HomeActions();), and passes the registry variable to the * ActionController class. */ $controller = new $class(); $controller->setRegistry($this->registry); //Trys the setModule method in the ActionController class $controller->setModule($module); /* The ActionController dispatchAction method checks if the method * exists, then runs the displayView function in the * ActionController class. */ $controller->dispatchAction($action); } catch(Exception $error) { /* An exception has occurred, and will be displayed if * $throwExceptions is set to true. */ if($throwExceptions) { echo $error; } } } } abstract class ActionController { //Declaring variable(s) protected $registry; protected $module; protected $registryItems = array(); //Class construct method public function __construct(){} public function setRegistry($registry) { //Sets the registry object $this->registry = $registry; /* Once the registry is loaded, the controller root directory path is * set from the registry. This path is needed for the controller * classes to work properly. */ $this->setPageDir(); } //Sets the controller root directory from the value stored in the registry public function setPageDir() { $this->pageDir = $this->registry->get('pageDir'); } //Sets the module public function setModule($module) { $this->module = $module; } //Gets the module public function getModule() { return $this->module; } /* Checks for actionMethod in the Actions class (example: doFrontpage() * within home/HomeActions.php) with the method_exists function and, if * present, the actionMethod and displayView functions are executed. */ public function dispatchAction($action) { $actionMethod = 'do' . ucfirst($action); if (!method_exists($this, $actionMethod)) { throw new Exception('Action not found!'); } $this->$actionMethod(); $this->displayView($action); } public function displayView($action) { if (!is_file($this->pageDir . '/' . $this->getModule() . '/' . $action . 'View.php')) { throw new Exception('View not found!'); } //Sets $this->actionView to the path of the action View file $this->actionView = $this->pageDir . '/' . $this->getModule() . '/' . $action . 'View.php'; //Sets path of the action View file into the registry $this->registry->set('actionView', $this->actionView); //Includes template file within which the action View file is included require_once $this->pageDir . '/default.tpl'; } } class Registry { //Declaring variables private $store; //Class constructor public function __construct() {} //Sets registry variable public function set($label, $object) { $this->store[$label] = $object; } //Gets registry variable public function get($label) { if(isset($this->store[$label])) { return $this->store[$label]; } else { return false; } } //Adds outside array of registry values to $this->store array public function addRegistryArray($registryItems) { foreach ($registryItems as $key => $value) { $this->set($key, $value); } } //Returns registry array public function getRegistryArray() { return $this->store; } }

3 个答案:

答案 0 :(得分:1)

没有仔细查看代码:

尝试使用有意义的函数和变量名来编写不言自明的代码。仅在代码的用途或功能不明确的情况下使用注释。例如。

//Declaring variable(s)
//Class construct method
//Checks for existance of file
//Includes file

是无用的评论,因为代码本身已经足够清楚了。

值得一读的书:Clean Code

答案 1 :(得分:1)

我在closevoting之间徘徊,因为它过于本地化并且想要对代码发表评论。此外,现在涉及的代码太多了,所以我只会评论一些事情:

1)文档样式

为什么不使用既定的文档格式,例如PHPDoc

2)格式化

就我所知,

是一致的,但我建议使用广泛使用的编码约定,如PEAR或ZF(基于PEAR),而不是自己做(你的)无论如何都接近PEAR,所以你不妨完全采用它。

3)单身人士模式

为了Singleton能够工作,它必须有一个私有的__contruct和__clone方法。但我建议不要使用它。许多人认为Singleton是AntiPattern。关于单身人士模式的缺点,有很多关于SO的问题,所以请浏览一下。如果应该只有一个实例,那么就不要实例化第二个实例。如果您需要访问其他类中的FrontController inject it

4)方法长度

我可能会尝试缩短dispatch方法。基本上,如果你描述一个方法的作用,你必须使用这样做,那部分应该进入它自己的方法。尝试将方法制作成小的离散单位。这将使UnitTesting更容易。

5)关注点分离

我不确定为什么FrontController从ActionController扩展。没有其他类可以扩展它,但我认为FrontController实例化的类也是ActionController的子类。但FrontController虽然命名为控制器,但它与PageControllers做的不同,所以我可能会将它们分开。

在旁注中,如果您有兴趣提高代码质量,请查看http://phpqatools.org/

中给出的幻灯片和工具。

答案 2 :(得分:0)

您的代码非常让人感到CakePhp。我建议检查一下,它使用App :: import()和一个包装文件系统的File类来完成所有这些工作。它由不同版本和操作系统的社区测试,实际上可以节省您的时间。