Home  >  Article  >  Backend Development  >  PHP architecture registry

PHP architecture registry

巴扎黑
巴扎黑Original
2016-11-12 11:26:241538browse

What is registry mode? it's actually really easy!

The role of the registry is to provide system-level object access functions. We often take "global variables are bad" as an article of faith when coding. However, everything has two sides, and global data access is very attractive.

Here comes the problem:

Most systems are divided into several layers, and each layer only communicates with adjacent layers through pre-defined channels. Sharing layers makes the program flexible, and replacing or modifying each layer can minimize the impact on other parts of the system. But what about when you need information in one layer that is not adjacent to another layer?

Option 1: Pass contextual information from one object to another required object through the connection between the layers of the system: pass this information from one object to another object in the system, from a controller responsible for processing the request The object is passed to the object of the business logic layer, and then passed to the object responsible for talking to the database. Of course, you can also pass the ApplicationHelper object, or a specific Context object.

Option 2: The interfaces of all objects must be modified to determine whether the context objects are what they need. Obviously, sometimes this approach breaks loose coupling.

Option 3: Through registry mode. Registry classes provide static methods (or instantiation methods of singleton objects) to allow other objects to access the data in them (usually objects). Every object in the entire system can access these data objects.

Think about the scope of PHP before implementing it:

Scope is usually used to describe the visible program of an object or value in the code. The life cycle of a variable can be measured in time. There are 3 levels of variable scope.

1. HTTP request scope

refers to the cycle from the beginning to the end of an HTTP request.

2. Session scope

PHP has built-in support for session variables. At the end of a request, the session variables are serialized and stored in the file system or database, and then retrieved at the beginning of the next request. The session ID stored in the cookie and passed in the query string are used to track the owner of the session. Therefore, you can think of certain variables as having session-level scope. Using this, you can store objects between several requests and save traces of user access to the database. Of course, be careful not to hold different versions of the same object, so when you save a session object to the database, you need to consider using certain locking strategies.

3. Application Scope

In other languages, especially JAVA, there is a cache pool, which is the concept of "application scope". Variables in memory can be accessed by all object instances in the program. PHP does not have such functionality, but in large-scale applications it can be useful to access application-level data in order to access configuration variables.

The following uses the registry to implement these three scopes. The class diagram is as follows:

PHP architecture registry

values[$key] ) ) {
            return $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 );
    }
}
class SessionRegistry extends Registry {
    private static $instance;
    private function __construct() {
        session_start();
    }
    static function instance() {
        if ( ! isset(self::$instance) ) { self::$instance = new self(); }
        return self::$instance;
    }
    protected function get( $key ) {
        if ( isset( $_SESSION[__CLASS__][$key] ) ) {
            return $_SESSION[__CLASS__][$key];
        }
        return null;
    }
    protected function set( $key, $val ) {
        $_SESSION[__CLASS__][$key] = $val;
    }
    function setComplex( Complex $complex ) {
        self::instance()->set('complex', $complex);
    }
    function getComplex( ) {
        return self::instance()->get('complex');
    }
}
class ApplicationRegistry extends Registry {
    private static $instance;
    private $freezedir = "/tmp/data";
    private $values = array();
    private $mtimes = array();
    private function __construct() { }
    static function instance() {
        if ( ! isset(self::$instance) ) { self::$instance = new self(); }
        return self::$instance;
    }
    protected function get( $key ) {
        $path = $this->freezedir . DIRECTORY_SEPARATOR . $key;
        if ( file_exists( $path ) ) {
            clearstatcache();
            $mtime=filemtime( $path );
            if ( ! isset($this->mtimes[$key] ) ) { $this->mtimes[$key]=0; }
            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;
        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);
    }
    static function setControllerMap( \woo\controller\ControllerMap $map  ) {
        self::instance()->set( 'cmap', $map );
    }
    static function getControllerMap() {
        return self::instance()->get( 'cmap' );
    }
    static function appController() {
        $obj = self::instance();
        if ( ! isset( $obj->appController ) ) {
            $cmap = $obj->getControllerMap();
            $obj->appController = new \woo\controller\AppController( $cmap );
        }
        return $obj->appController;
    }
}
//如果你安装了PHP的shm扩展,就可以使用该扩展中函数来实现应用程序注册表
class MemApplicationRegistry extends Registry {
    private static $instance;
    private $values=array();
    private $id;
    const DSN=1;
    private function __construct() {
        $this->id = @shm_attach(55, 10000, 0600);
        if ( ! $this->id ) {
            throw new Exception("could not access shared memory");
        }
    }
    static function instance() {
        if ( ! isset(self::$instance) ) { self::$instance = new self(); }
        return self::$instance;
    }
    protected function get( $key ) {
        return shm_get_var( $this->id, $key );
    }
    protected function set( $key, $val ) {
        return shm_put_var( $this->id, $key, $val );
    }
    static function getDSN() {
        return self::instance()->get(self::DSN);
    }
    static function setDSN( $dsn ) {
        return self::instance()->set(self::DSN, $dsn);
    }
}
?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn