[Li Jingshan php] every day tp5-20161207| Loader.php-5

Source: Internet
Author: User
Tags explode vars

/** *  instantiation (tiering) model  *  @param  string  $name           model name  *  @param  string  $layer          Business layer name  *  @param  bool    $appendSuffix   Add class name suffix  *  @param  string   $common         Public module name  *  @return  object * @ Throws classnotfoundexception */public static function model ($name  =  ",   $layer  =  ' model ',  $appendSuffix  = false,  $common  =  ' common ') {//  this is get model    //  get the corresponding information     if  ( Isset (self:: $instance [$name  .  $layer])  {        return  self:: $instance [$name  .  $layer];    }//  different   layers     different   Name   You can determine   different  model    //  corresponds to   different  key    if  (Strpos ( $name,  '/')  {//  if your name is not pure   then you will be treated         list ($ module,  $name)  = explode ('/',  $name,  2);//  By default is  list  mode      } else {         $module  = request:: Instance ()->module ();//  otherwise   should be automatically loaded, this position is not the place to initialize the call     }      $class  = self::p arseclass ($module,  $layer,  $name,  $appendSuffix);//  get the corresponding     Names  , this class name    if  (Class_exists ($class))  {//  find this class variable, if it's a  class , use him          $model  = new  $class (); //  Good usage     } else {          $class  = str_replace (' \ \ '  .  $module  .  ' \ \ ',  ' \ \ '  .  $common  .  ' \ \ ',  $class);//  processing   This  class         if  (Class_exists ($class))  {//  if available               $model  = new  $class ();//  execute him          } else {//  otherwise   throws   classes   exceptions              //  Here you can add that,  should actually be  model class not exists:             throw new classnotfoundexception (' Class  not exists: '  .  $class,  $class);         }     }    self:: $instance [$name  .  $layer] =  $model ;//  Deposit  key value  keyword     return  $model;//  return   data}/** *  instantiation (tiering) controller   format: [ Module name/] Controller name  *  @param  string  $name           Resource Address  *  @param  string  $layer          control Layer name  * @ param bool    $appendSuffix   Add class name suffix  *  @param  string  $empty           Empty controller name  *  @return  Object|false *  @throws  classnotfoundexception */public static function controller ($name,  $layer  =   ' controller ',  $appendSuffix  = false,  $empty  =  ') {       First step:  get the corresponding  model        //  by   different   ways  1        //  Way 2    if  (Strpos ($ name,  '/'))   {        list ($module,  $name)  = explode ('/',  $name );    } else {         $module  =  request::instance ()->module ();    }    //  The second step:  get  class     $class  = self::p arseclass ($module,  $layer,  $name,  $appendSuffix);    //  Step three:   verification of acquired class , existence,     if   (Class_exists ($class))  {        return new  $class (Request::instance ());//  back     } elseif  ($empty  && class_ Exists ($emptyClass  = self::p arseclass ($module,  $layer,  $empty,  $appendSuffix))   {        return new  $emptyClass (Request::instance ());//   Return to empty         //  seems to be improving, no error, haha     }}/** *  instantiation Verification class   Format: [module name/] Authenticator name  *  @param  string  $name           Resource address  *  @param  string  $layer          validation layer name  *   @param  bool    $appendSuffix   Add class name suffix  *  @param  string $ common        Public module name  *  @return  object|false * @ Throws classnotfoundexception */public static function validate ($name  =  ",   $layer  =  ' Validate ',  $appendSuffix  = false,  $common  =  ' common ') {   Validation, validation type:    //  name   layer name   prefix      $name  =   $name  ?: config::get (' default_validate ');//  get validation rules   address     if   (Empty ($name))  {// empty  $name   If the file is empty, return directly to         return new  validate;//  new   Validation classes  Validate  new validation classes     }    if  (Isset (self:: $instance [$name  .  $layer])  {//  presence directly returns          return self:: $instance [$name  .  $layer];    }//  if already   Verified     if  (Strpos ($name,  '/'))  {//         list ($module,  $name)  = explode ('/',  $name);     } else  {         $module  = request::instance ()->module ();     }    //  Get classes      $class  = self:: Parseclass ($module,  $layer,  $name,  $appendSuffix);    if  (class_exists ($class))  {//  if the class is storedIn          $validate  = new  $class;//  generation   New classes     } else {         $class  =  Str_replace (' \ \ '  .  $module  .  ' \ \ ',  ' \ \ '  .  $common  .  ' \ \ ',  $ Class);//  if   get class         if  (Class_exists ($class))   {             $validate  = new  $class ;//  return to new   class         } else {             throw new classnotfoundexception (' Class not  exists: '  .  $class,  $class);        }     }//  you know, the   class doesn't exist.     self:: $instance [$name  .  $layer] =   $validate;//  Archive  Query     return  $validate;} /** *  Database Initialize   Get DB class instance  *  @param  mixed           $config   Database configuration  *  @param  bool|string    $name   Connection Identification  true   Force reconnect  *  @return  \think\db\connection */public static function db ($ config = [],  $name  = false) {    return db::connect ($config,   $name);}   Database   Initialize   and get   database   Instance/** *  Remote Call module operation method   parameter Format  [module/controller/] Operation  *   @param  string        $url             Call address  *  @param  string|array  $vars            Call parameters   support strings and arrays  *  @param  string         $layer          control layer to invokeName  *  @param  bool          $appendSuffix   Add class name suffix  *  @return  mixed */public static function action ($url,  $vars  = [],  $layer  =  ' controller ',  $appendSuffix  = false) {      $info    = pathinfo ($url);//  get  url  address information      $action  =  $info [' basename '];//  get  action  way      $module  =   '. '  !=  $info [' dirname '] ?  $info [' DirName '] : request::instance ()->controller ();     //  get   Intermediate values  module     $class   = self:: Controller ($module,  $layer,  $appendSuffix);//  get  class  name     if   ($class)  {//  if present  class        if  (is_scalar ($vars))  {//  If it is standard volume             if  (strpos ($vars,  ' = '))  {// strpos                 parse_str ($vars,  $vars);//  parsing string              } else {                  $vars  = [$vars];//  This strong conversion   pretty              }        }         return app::invokemethod ([$class,  $action  . config::get (' Action_ Suffix ')],  $vars);    }//return app::  Verify that the file exists}/** *  string naming style conversion  * type 0  convert Java style to C style  1  convert C-style to Java style  *  @param  string    $name   Strings  *  @param  integer  $type   conversion types  *  @return  string */public static  function parsename ($name,  $type  = 0) {//  convert string   style   A small function, if 1  is capitalized, Otherwise, lowercase     if  ($type)  {        return  ucfirst (Preg_replace_callback ('/_ ([a-za-z])/', function  ($match)  {             return strtoupper ($match [1]);         },  $name));    } else {         return strtolower (Trim (preg_replace ("/[a-z]/",  "_\\0",  $name),   "_"));    }}/** *  parse the class name of the application class  *  @param  string  $module   Module name  *  @param  string  $layer    layer name  controller model ... *   @param  string  $name     class name  *  @param  bool    $appendSuffix  *  @return  string  */public static function parseclass ($module,  $layer,  $name,  $appendSuffix  = false) {//  parsing  Class  class name      $name   = str_replace ([ '/',  '. '],  ' \ \ ',  $name);//  Replace class name      $array  = explode (' \ \ ',   $name);//  parsing class resources      $class  = self::p arsename (Array_pop ($array),  1 )  .  (App:: $suffix  | |   $appendSuffix  ? ucfirst ($layer)  :  ');     $path   =   $array  ? implode (' \ \ ',  $array)  .  ' \ \ '  :  ';     Return app:: $namespace  .  ' \ \ '  .  ($module  ?  $module  .  ' \ \ '  :   ")  .  $layer  .  ' \ \ '  .  $path  .  $class;}   Analysis  Class name/** *  instance of initialization class  *  @return  void */public static function  Clearinstance () {    self:: $instance  = [];}   initialization, that is,   empty instance


This article is from the "Focus on PHP Group number: 414194301" blog, please be sure to keep this source http://jingshanls.blog.51cto.com/3357095/1859258

[Li Jingshan php] every day tp5-20161207| Loader.php-5

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.