(* Temporarily not split front controller and application controller, all integrated in command class implementation)
1 Registry mode//Registry mode//Registry mode is used to provide a system level object, accessible anywhere (can use singleton mode) class registry{private static $instance; Private $request; Private Function __construct () {} static function instance () {if (!isset (self:: $instance)) {self:: $inst Ance=new self (); } return Self:: $instance; } function Getrequest () {$this->request; The function setrequest (Request $request) {$this->request= $request; }}class request{} 23 Scopes Registry namespace Woo\controller;class Request{}class complex{}//Create a scoped registry mode// Request Level Registry namespace Woo\base;use Woo;abstract class registry{abstract protected function get ($key); Abstract protected function set ($key, $val); }class Requestregistry extends registry{private $values =array (); private static $instance; Private Function __construct () {}//return unique instance static function instance () {if (!isset (self:: $instance)) { Self:: $instance =new self (); } return Self:: $instance; } Protected function Get ($key) {if (Isset ($this->values[$key])) {return isset ($this->values[$key]); } return null; } protected function set ($key, $val) {$this->values[$key]= $val; } static function Getrequest () {return self::instance ()->get (' request '); } static function Setrequest (Woo\controller\request $request) {return self::instance ()->set (' Request ', $reque ST); }}//Session-Level registry class Sessionregistry extends registry{private static $instance; Private Function __construct () {session_start (); }//Returns a unique instance of static function instance () {if (!isset (self:: $instance)) {self:: $instance the =new self (); } return Self:: $instance; } protected function Get ($key) {if (Isset ($_session[__class__][$key])) {return isset ($_SESSION[__CLA ss__][$key]); } return null; } protected function set ($key, $val) {$_session[__class__][$key]= $vAl } static function Getcomplex () {return self::instance ()->get (' Complex '); } static function Setrequest (Woo\controller\complex $request) {return self::instance ()->set (' Complex ', $reque ST); }}//Application-Level registry class Applicationregistry extends registry{private static $instance; Private $freezedir = ' Data '; Private $values =array (); Private $mtimes =array (); Private Function __construct () {session_start (); }//Returns a unique instance of static function instance () {if (!isset (self:: $instance)) {self:: $instance the =new self (); } return Self:: $instance; The//get,set are saved separately from a $key to a file protected function get ($key) {$path = $this->freezedir. Directory_separator. $key; if (file_exists ($path)) {Clearstatcache (); Get file modification time $mtime =filemtime ($path); if (!isset ($this->mtimes[$key])) {$this->mtimes[$key]=0; }//If the file is modified if($mtime > $this->mtimes[$key]) {$data =file_get_contents ($path); $this->mtimes[$key]= $mtime; Return ($this->values[$key]=unserialize ($data)); }} if (Isset ($this->values[$key]) {return $this->values[$key]; } return null; } protected function set ($key, $val) {$this->values[$key]= $val; $path = $this->freezedir. Directory_separator. $key; Files that do not exist will automatically create file_put_contents ($path, serialize ($val)); $this->mtimes[$key]=time (); } static function Getdsn () {return self:: $instance ()->get (' DSN '); } static function Setdsn ($DSN) {return self:: $instance ()->set (' DSN ', $DSN); }}3 Front-end controller controllers----combine registry mode with Command mode to create a unified portal framework <?phpnamespace woo\command;include_once ("./woo/controller/ request.php ")//command class//* Command object has the ability to handle views independently abstract class command{//No subclasses can overwrite the method (never parameters) final function __construc T () {}//the method passed in aThe request object, so you have the ability to access the request data function execute (\woo\controller\request $request) {//can do other things//...//. Call D again. Oexecute method $this->doexecute ($request); The function DisPlay ($request) {//Get cmd, used to decide which page to tune $cmd = $request->getproperty (' cmd '); The wording is not very good .... The character print $cmd before the command is intercepted; $VIEWURL = "./woo/view/". substr ($cmd, 0,strlen ($cmd)-(7)). " View.html "; Include_once ($VIEWURL); } abstract function Doexecute (\woo\controller\request $request);} <?phpnamespace woo\command;include_once ("./woo/controller/request.php"); Include_once ("./Woo/Command/ Defaultcommand.php ");//commandresolver command parser class commandresolver{private static $base _cmd; private static $default _cmd; function __construct () {if (!self:: $base _cmd) {self:: $base _cmd=new \reflectionclass ("\woo\command\command "); Self:: $default _cmd=new DefaultCommand (); }} function GetCommand (\woo\controller\request $request) {Specify the command key to be called cmd $cmd = $request->getproperty (' cmd '); $sep =directory_separator; When the specified CMD data is not found, returns the default CMD instance if (! $cmd) {return clone self:: $default _cmd; } $cmd =str_replace (Array ('. ', $sep), "", $cmd); $filePath = ". \woo{$sep}command{$sep} {$cmd}.php"; $className = "\woo\\command\\{$cmd}"; if (file_exists ($filePath)) {require_once ("$filePath"); Determines whether the passed-in class exists, whether it is a subclass of Base_cmd if (class_exists ($className)) {$cmd _class=new \reflectionclass ($class Name); if ($cmd _class->issubclassof (self:: $base _cmd)) {return $cmd _class->newinstance (); }else{//Parse failed, jump to default page $request->addfeedback ("command ' $cmd ' is not a command"); Return clone self:: $default _cmd; }}}else{$request->addfeedback ("command ' $cmd ' is not found");Return clone self:: $default _cmd; }}}<?phpnamespace woo\command;include_once ("./woo/controller/request.php"); Include_once ("./Woo/Command/ Command.php "), class DefaultCommand extends command{function doexecute (\woo\controller\request $request) {$reque St->addfeedback ("Welcome to Woo!"); $feedbacks = $request->getfeedback (); foreach ($feedbacks as $key = = $val) {print $val; print "<br>"; } include_once ("woo/view/main.html"); }}<?phpnamespace woo\command;include_once ("./woo/controller/request.php"); Include_once ("./Woo/Command/ Command.php "), class MyCommand extends command{function doexecute (\woo\controller\request $request) {$request-&G T;addfeedback ("Welcome to Woo!"); $feedbacks = $request->getfeedback (); foreach ($feedbacks as $key = = $val) {print $val; print "<br>"; } $this->display ($request); }}<?phpnamespace Woo\base;inclUde_once ("./woo/base/registry.php");//Application-level registry//*dns Data Source Nameclass Applicationregistry extends registry{ private static $instance; Private $freezedir = './woo/cache '; Private $values =array (); Private $mtimes =array (); Private Function __construct () {session_start (); }//Returns a unique instance of static function instance () {if (!isset (self:: $instance)) {self:: $instance the =new self (); } return Self:: $instance; The//get,set are saved separately from a $key to a file protected function get ($key) {$path = $this->freezedir. Directory_separator. $key; if (file_exists ($path)) {Clearstatcache (); Get file modification time $mtime =filemtime ($path); if (!isset ($this->mtimes[$key])) {$this->mtimes[$key]=0; }//If the file is modified if ($mtime > $this->mtimes[$key]) {$data =file_get_contents ($path); $this->mtimes[$key]= $mtime; Return ($this-≫values[$key]=unserialize ($data)); }} if (Isset ($this->values[$key]) {return $this->values[$key]; } return null; } protected function set ($key, $val) {$this->values[$key]= $val; $path = $this->freezedir. Directory_separator. $key; if (!is_dir ($this->freezedir)) {mkdir ($this->freezedir); }//file does not exist will automatically create file_put_contents ($path, serialize ($val)); $this->mtimes[$key]=time (); } static function Getdsn () {return self::instance ()->get (' DSN '); } static function Setdsn ($DSN) {return self::instance ()->set (' DSN ', $DSN); }}<?phpnamespace woo\base;//Create a scope-scoped registry mode//request-level Registry abstract class registry{abstract protected function get ($key) ; Abstract protected function set ($key, $val);} <?phpnamespace woo\base;include_once ("./woo/base/registry.php"); Include_once ("./woo/controller/request.php") ;//Request Level Registry class Requestregistry extends registry{private $values =array (); private static $instance; Private Function __construct () {}//return unique instance static function instance () {if (!isset (self:: $instance)) { Self:: $instance =new self (); } return Self:: $instance; } protected function Get ($key) {if (Isset ($this->values[$key])) {return isset ($this->values[$key ]); } return null; } protected function set ($key, $val) {$this->values[$key]= $val; } static function Getrequest () {return self::instance ()->get (' request '); } static function Setrequest (\woo\controller\request $request) {return self::instance ()->set (' Request ', $requ EST); }}<?phpnamespace woo\base;include_once ("./woo/base/registry.php");//session-Level Registry class Sessionregistry extends registry{private static $instance; Private Function __construct () {session_start (); }//Returns a unique instance of static function instance () {if (!isset (self:: $instance)) {self:: $instance =new self (); } return Self:: $instance; } protected function Get ($key) {if (Isset ($_session[__class__][$key])) {return isset ($_SESSION[__CLA ss__][$key]); } return null; } protected function set ($key, $val) {$_session[__class__][$key]= $val; } static function Getcomplex () {return self::instance ()->get (' Complex '); } static function Setrequest (Woo\controller\complex $request) {return self::instance ()->set (' Complex ', $reque ST); }}<?phpnamespace woo\controller;include_once ("./woo/base/applicationregistry.php");//ApplicationHelper Application Helper Class--is responsible for reading the configuration file data, provided to the client code//* This class uses the caching mechanism, improves the performance class applicationhelper{private static $instance; Private $config = "./woo/config/woo_options.xml"; Use singleton mode to get the configuration private function __construct () {} static function instance () {if (!self:: $instance) { Self:: $instance =new self (); } return SELF:: $instance; } function init () {//To see if the cached data exists $DSN =\WOO\BASE\APPLICATIONREGISTRY::GETDSN (); if (!is_null ($DSN)) {//$DSN returns return directly when not empty; } $this->getoptions (); The method is called only if the cached data does not exist private function getoptions () {$this->ensure (file_exists ($this->config), "Could not find options file!"); $options =simplexml_load_file ($this->config); Print Get_class ($options); $DSN = $options->dsn; $this->ensure ($DSN, "No DSN found!"); After the value is obtained, it is stored in the application-level registry, which facilitates the cache use//conversion of an array to facilitate serialization of \WOO\BASE\APPLICATIONREGISTRY::SETDSN ($dsn->__tostrin g ())); Set other values//...} Private function ensure ($expr, $message) {if (! $expr) {throw new \exception ($message); }}}<?phpnamespace woo\controller;class complex{}<?phpnamespace woo\controller;//introduced all required files include_once ("./Wo O/controller/applicationhelper.php "); inclUde_once ("./woo/command/commandresolver.php"); Include_once ("./woo/command/command.php");//controller class controller{private $applicationHelper; Private Function __construct () {}//This class can only get instance static function run () {$instance =new Controller () by the Run method; In theory, init only needs to be called when the application first starts (but PHP is an interpreted language, so it must be called every time) $instance->init (); Theoretically speaking, HandleRequest needs to run $instance->handlerequest () when each request arrives; } function init () {//Get a singleton for global configuration $applicationHelper =applicationhelper::instance (); $applicationHelper->init (); }//Each request needs to call function HandleRequest () {$request =new request (); $cmd _r=new \woo\command\commandresolver (); According to request, generate the corresponding command abstract class (interface) Subclass instance//To use command to perform related actions $cmd = $cmd _r->getcommand ($request); $cmd->execute ($request); }}<?phpnamespace woo\controller;include_once ("./woo/base/requestregistry.php");//For storing data that needs to be exchanged with the view layer class Request {Private $propErties; Private $feedback =array (); function __construct () {$this->init (); \woo\base\requestregistry::setrequest ($this); }//supports both HTTP request and command-line arguments (available for debugger use)//* used to populate parameters into the Properties property in the function init () {//Form submission method if (Isset ($_serve r[' Request_method ')) {//For collecting data submitted by Form $this->properties=$_request; Return }//$_server[' argv '] the arguments passed to the script foreach ($_server[' argv ') as $arg) {//search string where the first occurrence occurs if ( Strpos ($arg, ' = ')) {list ($key, $val) =explode ("=", $arg); $this->setproperty ($key, $val); }}} function GetProperty ($key) {if (Isset ($this->properties[$key]) {return $this->p roperties[$key]; } return null; } function SetProperty ($key, $val) {$this->properties[$key]= $val; } function Addfeedback ($msg) {Array_push ($this->feedback, $msg); } function Getfeedback () { return $this->feedback; } function getfeedbackstring ($separator = "\ n") {return implode ($separator, $this->feedback); }}//woo_options.xml<?xml version= "1.0" encoding= "UTF-8"? ><data><dsn>dsn</dsn><dsn2 >DSN</dsn2></data>//Frame entry Index.php<?phpuse woo\controller\controller;include_once ("./woo/ Controller/controller.php ");//print_r ($_get);//Frame entry controller::run ();
Eight.. PHP mode design----Enterprise Mode (1)