Eight.. PHP mode design----Enterprise Mode (1) _php tutorial

Source: Internet
Author: User
Tags dsn

Eight.. PHP mode design----Enterprise Mode (1)



(* 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----unify the portal framework with Registry mode and command mode
 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);}
 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; }    }}
 Addfeedback ("Welcome to Woo!");        $feedbacks = $request->getfeedback ();            foreach ($feedbacks as $key = = $val) {print $val; Print "
"; } include_once ("woo/view/main.html"); }} Addfeedback ("Welcome to Woo!"); $feedbacks = $request->getfeedback (); foreach ($feedbacks as $key = = $val) {print $val; Print "
"; } $this->display ($request); }} 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); }} 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); }} Get (' complex '); } static function Setrequest (Woo\controller\complex $request) {return self::instance ()->set (' Complex ', $reque ST); }} 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); } }} 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); }} 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 DSN DSN Frame Entry index.php

http://www.bkjia.com/PHPjc/925225.html www.bkjia.com true http://www.bkjia.com/PHPjc/925225.html techarticle eight.. PHP mode design----Enterprise Mode (1) (* Temporarily not split front controller and application controller, all integrated in command class implementation) 1 Registry mode//Registry mode//Registry mode for ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.