From PHP5 to PHP7 self encapsulation mongodb and smooth upgrade ____php

Source: Internet
Author: User
Tags manual mongoclient mongodb

I. Preamble

There are many enterprise-class users using Php+mongodb because MongoDB is convenient for storing unstructured data. In PHP5 and before, the official provided two extensions, MONGO and MongoDB, which MONGO is to mongoclient and other core classes based on the operation of the cluster, encapsulation is very convenient, so basically will choose MONGO extension, see the official manual for details:

http://php.net/manual/en/class.mongoclient.php
1

But with PHP5 upgrade to PHP7, the official no longer support MONGO extension, only support MongoDB, and PHP7 performance is huge, people can not give up, so how to replace MONGO into a mongodb becomes an urgent problem to be solved. MongoDB introduces namespaces, but functionality encapsulation is very poor, and if you want to use native extensions, it almost means writing native MONGO statements. This idea is very contrary to the problem of ORM simplifying the syntax of DBIO operations and focusing on logical optimization. Details can also be found in the Official handbook;

http://php.net/manual/en/class.mongodb-driver-manager.php
1

In this case, MONGODB official could not help, in order to facilitate the use, increase market share, the introduction of the library based on MongoDB expansion, details see:

Https://github.com/mongodb/mongo-php-library
1

In fact, when we use the process, always want to be able to achieve as much decoupling as possible, so layered clarity becomes particularly important. Because the official library does not realize the separation and the specific functional needs of the author, so the author made a wheel.

Two. Self-encapsulated Mongodbclient class

1. Constructor
The author hopes that the constructor can have two ways, one is to implement the model layer inheritance parameter construction in single example mode, the other is simply directly constructed to realize the extensive application of code and encapsulation class.

Public $_client;
Public $_manager;
Public $_db;

Public $_collection;
        Public Function __construct () {$config = $this->getdbconnection (); if (!empty ($config [' Server ']) &&!empty ($config [' DB '])} {$uri = $config [' Server ']. "
            /". $config [' DB '];
            if (Isset ($config [' urioptions '])) {$urioptions = $config [' urioptions '];
            }else{$urioptions =array ();
            } if (Isset ($config [' driveroptions ']) {$driveroptions = $config [' driveroptions '];
            }else{$driveroptions =array ();
            $this->setclient ($uri, $urioptions, $driveroptions);
        $this->setdatabase ($config [' db ']);
        $collectionName = $this->collectionname ();
        if ($this->getdatabase ()) {$this->setcollection ($collectionName);
    The Public Function CollectionName () {return ';
 Public Function getdbconnection () {       return Array ();
 }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

The above is a method of constructing a single example pattern, apparently defining two functions that have no meaning to get the arguments, in fact the initialized entry should be overridden in an inherited subclass. Each set function passes the instance to the object's properties to save and call later.

Public function InitInstance ($uri, $db, $collectionName)
    {
        //$config = $this->getmongoconfig ();
        $tempStr = ' mongodb://'. $config [' username ']. ': $config [' Password ']. ' @ '. $config [' Host ']. ' /'. $config [' db '];
        $mongodbclient =new mongodbclient ();
        $this->setclient ($uri);
        $this->setdatabase ($db);
        $this->setcollection ($collectionName);
    }
1 2 3 4 5 6 7 8 9

Here for simple direct initialization. And the design of the constructor determines that the two are not conflicting, and the reason why we should design both initialization methods is to support the clear layering better.

Construction of 2.filter filters
from the official MongoDB of the original to the PHP official extension to the MongoDB library, the method of constructing the filter is very rude, is to let people write directly to the MongoDB of the filter's native statement, This is very different from the idea of ORM simplifying syntax coupling. In order to facilitate others to use the complex filter, the author has simplified the construction of the filter. The idea is to think of a semantic filter as a formula, a set of conditions is considered a number, the connector is treated as an operator, the parentheses are implemented by the infix expression suffix expression, and then the suffix expression is implemented to achieve the grammaticalization of the semantically connected characters, thus simplifying the cost of the developer at the business level. Details are as follows:

Public Function Filterconstructor ($key, $operator, $value, $connector =array ()) {$filter =array ();
            $subfilter =array ();
                    Switch ($operator) {case ' = ': $subfilter =array ($key => $value);
                Break
                    Case ' > ': $subfilter =array ($key =>array (' $gt ' => $value));
                Break
                    Case ' >= ': $subfilter =array ($key =>array (' $gte ' => $value));
                Break
                    Case ' < ': $subfilter =array ($key =>array (' $lt ' => $value));
                Break
                    Case ' <= ': $subfilter =array ($key =>array (' $lte ' => $value));
                Break
                    Case '!= ': $subfilter =array ($key =>array (' $ne ' => $value));
                Break
                    Default:die ();
        Break$filter =array_merge ($filter, $subfilter);
    return $filter; } * * Construct a Easy-and filter with double arrays via key-value input * @param (array) $trible 1 (array) $t Rible2 * @return An array of mongo-dialect filter * @author Wangyang/Public Function Andfilterconstr
        Uctor ($trible 1, $trible 2) {$ret 1= $this->filterconstructor ($trible 1[0], $trible 1[1], $trible 1[2]);
        $ret 2= $this->filterconstructor ($trible 2[0], $trible 2[1], $trible 2[2]);
        Array_merge ($ret 1, $ret 2);
    return $ret 1; } * * Construct a easy-or filter with double arrays via key-value input * @param (array) $trible 1 (array) $tr Ible2 * @return An array of mongo-dialect filter * @author Wangyang/Public Function Orfilterconstruc
        Tor ($trible 1, $trible 2) {$ret 1= $this->filterconstructor ($trible 1[0], $trible 1[1], $trible 1[2]);
   $ret 2= $this->filterconstructor ($trible 2[0], $trible 2[1], $trible 2[2]);     $ret =array (' $or ' =>array ());
        Array_push ($ret [' $or '], $ret 1);
        Array_push ($ret [' $or '], $ret 2);
    return $ret; } * * Construct a easy-and filter with double filters * @param (array) $query 1 (array) $query 2 * @retur n an array of mongo-dialect filter * @author Wangyang */Public Function Onlyandfilterconstructor ($query 1, $q
        Uery2) {$query 1=array_merge_recursive ($query 1, $query 2);
    return $query 1; } * * Construct a easy-or filter with double filters * @param (array) $query 1 (array) $query 2 * @return An array of mongo-dialect filter * @author Wangyang/Public Function Onlyorfilterconstructor ($query 1, $quer
        y2) {$query =array (' $or ' =>array ());
        Array_push ($query [' $or '], $query 1);
        Array_push ($query [' $or '], $query 2);
    return $query; /* * Resolve the complicated connectors set filter * @param (Array) $query e.g. Array (Filterarray1 (), $connEctor,filterarray2 ()) * e.g. Array (arr1 (), ' or ', ' (', arr2 (), ' and ', ARR3 (), ') ') * @return an array of Mongo-diale CT Filter * @author Wangyang */Public Function Queryfilterconstructor ($query) {$priority =array (' =
        >3, ' and ' =>2, ' or ' =>2, ') ' =>1);
        $stack 1=array ();
        $stack 2=array (); Transfer nifix expression to postfix expression foreach ($query as $key => $value) {if Is_array (
            $value)) {Array_push ($stack 2, $value); }elseif ($value = = ' (' | |
            Empty ($stack 1)) {Array_push ($stack 1, $value); }elseif ($value = = ') ') {while ($top =array_pop ($stack 1))!== ' (') {Array_push ($stack 2, $top
                );
            }elseif (End ($stack 1) = = ' (') {Array_push ($stack 1, $value);
               }else{while ($priority [$value]< $priority [End ($stack 1)]) {$top =array_pop ($stack 1);     Array_push ($stack 2, $top);
            } array_push ($stack 1, $value);
            } while (!empty ($stack 1)) {$top =array_pop ($stack 1);
        Array_push ($stack 2, $top); foreach ($stack 2 as $key => $value) {if (Is_array ($value)) {$stack 2[$key]= $this-&
            Gt;filterconstructor ($value [0], $value [1], $value [2]); }//compute the Postfix expression foreach ($stack 2 as $key => $value) {if (Is_ar
            Ray ($value)) {Array_push ($stack 1, $value);
                }else{$top =array_pop ($stack 1);
                $subtop =array_pop ($stack 1);
                    if ($value = = = ") {$ret = $this->onlyandfilterconstructor ($top, $subtop);
                Array_push ($stack 1, $ret);
                    }elseif ($value = = ' or ') {$ret = $this->onlyorfilterconstructor ($top, $subtop); ArRay_push ($stack 1, $ret);
                }else{die (' Undefined connector ');
        $ret =array_pop ($stack 1);

    return $ret; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138-139

In the processing of the idea of using the stack, in PHP to replace the use of arrays, in fact, the PHP array function is quite fit the stack of ideas, such as

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.