8. PHP pattern design enterprise pattern (1)

Source: Internet
Author: User
8. PHP pattern design enterprise pattern (1) (* front-end controllers and application controllers are not split temporarily, all of which are integrated in the Command class implementation)

1 Registry mode // Registry mode is used to provide a system-level object for convenient access anywhere (the Singleton mode can be used) class Registry {private static $ instance; private $ request; private function _ construct () {} static function instance () {if (! Isset (self ::$ instance) {self ::$ instance = new self ();} return self ::$ instance;} function getRequest () {$ this-> request;} function setRequest (Request $ request) {$ this-> request = $ request ;}} class Request {} 2 registry namespace woo \ controller in three scopes; class Request {} class Complex {} // create a registry mode with a scope // Request level registry namespace woo \ base; use woo; abstract class Registry {abstract protected function get ($ key); abstract prot Ected function set ($ key, $ val);} class RequestRegistry extends Registry {private $ values = array (); private static $ instance; private function _ construct () {} // return the unique 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 s Elf: instance ()-> set ('request', $ request) ;}// session-level Registry class SessionRegistry extends Registry {private static $ instance; private function _ construct () {session_start ();} // returns the unique 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 [_ CLASS _] [$ key]);} return null;} protected function set ($ key, $ val) {$ _ SESSION [_ CLASS _] [$ key] = $ val;} static function getComplex () {return self: instance () -> get ('compute');} static function setRequest (woo \ controller \ Comp Lex $ request) {return self: instance ()-> set ('compute', $ request );}} // application-level Registry class ApplicationRegistry extends Registry {private static $ instance; private $ freezedir = 'data'; private $ values = array (); private $ mtimes = array (); private function _ construct () {session_start ();} // return the unique static function instance () {if (! Isset (self ::$ instance) {self ::$ instance = new self ();} return self ::$ instance ;}// get, set stores a $ key to the file separately. protected function get ($ key) {$ path = $ this-> freezedir. DIRECTORY_SEPARATOR. $ key; if (file_exists ($ path) {clearstatcache (); // get the file modification time $ mtime = filemtime ($ path); if (! Isset ($ this-> mtimes [$ key]) {$ this-> mtimes [$ key] = 0 ;} // if the object 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 the file does not exist, the system 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-create a unified portal framework based on the registry mode and command mode
 DoExecute ($ request);} function disPlay ($ request) {// Obtain cmd, used to decide which page to retrieve $ cmd = $ request-> getProperty ('cmd '); // The statement is not very good .... print $ cmd; $ 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; // 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 "); // Determine whether the input class exists and whether it is a sub-class of base_cmd. if (class_exists ($ className) {$ cmd_class = new \ ReflectionClass ($ className ); if ($ cmd_class-> isSubclassOf (self: $ base_cmd) {return $ cmd_class-> newInstance ();} else {// resolution failed, jump to the 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 the file modification time $ mtime = filemtime ($ path); if (! Isset ($ this-> mtimes [$ key]) {$ this-> mtimes [$ key] = 0 ;} // if the object 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);} // if the file does not exist, file_put_contents ($ path, serialize ($ val) is automatically created )); $ 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', $ request );}} Get ('compute');} static function setRequest (Woo \ Controller \ complex $ request) {return self: instance ()-> set ('compute ', $ request );}} GetOptions () ;}// this method is called only when the cached data does not exist. getOptions () {$ this-> ensure (file_exists ($ this-> config ), "cocould not find options file! "); $ Options = simplexml_load_file ($ this-> config); print get_class ($ options); $ dsn = $ options-> dsn; $ this-> ensure ($ dsn, "No DSN found! "); // After obtaining the value, store it in the application-level registry to facilitate cache usage. // first convert it into an array to facilitate serialization of \ Woo \ Base \ ApplicationRegistry :: setDSN (array ($ dsn->__ toString (); // Set other values //...} private function ensure ($ expr, $ message) {if (! $ Expr) {throw new \ Exception ($ message );}}} Init (); // theoretically, handleRequest must run $ instance-> handleRequest ();} function init () {// Obtain a singleton upon each request, used for global configuration $ applicationHelper = ApplicationHelper: instance (); $ applicationHelper-> init () ;}// you need to call function handleRequest () once each request () {$ request = new Request (); $ pai_r = new \ Woo \ Command \ CommandResolver (); // Generate the corresponding command abstract class (interface) based on the request) and then use command to execute the relevant action $ cmd = $ cmd_r-> getCommand ($ request); $ cmd-> execute ($ request );}} Init (); \ Woo \ Base \ RequestRegistry: setRequest ($ this);} // supports both HTTP request and command line parameters (for debugging programs) // * fill the parameter in the properties property. function init () {// form submission METHOD if (isset ($ _ SERVER ['request _ method']) {// used to collect the data submitted by the form $ this-> properties =$ _ REQUEST; return ;} // $ _ SERVER ['argv'] the parameter foreach passed to the script ($ _ SERVER ['argv'] as $ arg) {// position where the string appears for the first time if (strpos ($ arg, '=') {list ($ key, $ val) = explode ("= ", $ arg); $ this-> setProperty ($ key, $ val) ;}} function getProperty ($ key) {if (isset ($ this-> properties [$ key]) {return $ this-> properties [$ 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 // Framework entry index. php

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.