Detailed Explanation of ThinkPHP Application Mode Extension

  • 2021-07-09 07:24:12
  • OfStack

ThinkPHP's application model makes it easier for developers to transform the core framework than before, and can make your application adapt to more environments and different needs. Each application pattern has its own pattern definition file, Compared with ThinkPHP version 3.1, ThinkPHP version 3.2 extends the application mode more clearly and clearly. Cli, Lite, Thin, AMF, PHPRPC and REST modes are defined in ThinkPHP version 3.1, which are similar to those in ThinkPHP version 3.2. If necessary, you can refer to the modification, in which Cli mode is built into ThinkPHP framework, and can be used normally without defining Cli mode separately. ThinkPHP also provides a convenient mode switching mode between development environment and formal environment. Let's analyze the mystery of ThinkPHP's application pattern extension as it runs.

1. Use of application patterns

Before studying the application pattern extension, let's take a look at how to use the application pattern. 1 by defining the constant APP_MODE as the application mode name in the entry file, But when parsing the ThinkPHP framework entry file, It is known that the default mode of the framework is normal mode (common), and it can automatically identify sae environment. Of course, the premise is that APP_MODE constant is not defined. Of course, ThinkPHP can automatically identify CLI and CGI modes, and running ThinkPHP framework in CLI and CGI environments automatically makes fine adjustments to these two environments in the default mode, and of course, it can also extend these two application modes by itself.


if(function_exists('saeAutoLoader')){//  Automatic identification SAE Environment 
  defined('APP_MODE')   or define('APP_MODE',   'sae');
  defined('STORAGE_TYPE') or define('STORAGE_TYPE', 'Sae');
}else{
  defined('APP_MODE')   or define('APP_MODE',    'common'); //  Application mode   Default to normal mode   
  defined('STORAGE_TYPE') or define('STORAGE_TYPE',  'File'); //  Storage type   Default to File  
}

2. Apply schema definitions

In ThinkPHP framework, except ThinkPHP framework entry and framework boot class, basically all other functions can be changed and extended through application mode. If we want to add an application mode, we only need to define a mode definition file under ThinkPHP\ Mode directory, and we can learn by analyzing common mode. The code for this file is as follows:


// File path: ThinkPHP/Mode/common.php
/**
 * ThinkPHP  Common schema definition 
 *  Definition 1 Schema files, only need to return 1 A schema contains an array of files 
 *  In the array, it mainly contains 4 List of extended files: 
 *   config  Load profile list for default 
 *   alias  Configure list for core class library aliases 
 *   core  List of core functions and class files to be loaded 
 *   tags  Behavior configuration list 
 *
 *  If you load in the application pattern definition 1 Custom class whose namespace must be Think
 */
return array(
  //  Configuration file 
  'config'  => array(
    THINK_PATH.'Conf/convention.php',  //  System convention configuration 
    CONF_PATH.'config.php',   //  Apply common configuration 
  ),
 
  //  Alias definition 
  'alias'   => array(
    'Think\Log'        => CORE_PATH . 'Log'.EXT,
    'Think\Log\Driver\File'  => CORE_PATH . 'Log/Driver/File'.EXT,
    'Think\Exception'     => CORE_PATH . 'Exception'.EXT,
    'Think\Model'       => CORE_PATH . 'Model'.EXT,
    'Think\Db'        => CORE_PATH . 'Db'.EXT,
    'Think\Template'     => CORE_PATH . 'Template'.EXT,
    'Think\Cache'       => CORE_PATH . 'Cache'.EXT,
    'Think\Cache\Driver\File' => CORE_PATH . 'Cache/Driver/File'.EXT,
    'Think\Storage'      => CORE_PATH . 'Storage'.EXT,
  ),
 
  //  Function and class files 
  'core'   => array(
    THINK_PATH.'Common/functions.php',
    COMMON_PATH.'Common/function.php',
    CORE_PATH . 'Hook'.EXT,
    CORE_PATH . 'App'.EXT,
    CORE_PATH . 'Dispatcher'.EXT,
    //CORE_PATH . 'Log'.EXT,
    CORE_PATH . 'Route'.EXT,
    CORE_PATH . 'Controller'.EXT,
    CORE_PATH . 'View'.EXT,
    BEHAVIOR_PATH . 'BuildLiteBehavior'.EXT,
    BEHAVIOR_PATH . 'ParseTemplateBehavior'.EXT,
    BEHAVIOR_PATH . 'ContentReplaceBehavior'.EXT,
  ),
  //  Behavior extension definition 
  'tags' => array(
    'app_init'   => array(
      'Behavior\BuildLiteBehavior', //  Build run Lite Documents 
    ),    
    'app_begin'   => array(
      'Behavior\ReadHtmlCacheBehavior', //  Read static cache 
    ),
    'app_end'    => array(
      'Behavior\ShowPageTraceBehavior', //  Page Trace Display 
    ),
    'view_parse'  => array(
      'Behavior\ParseTemplateBehavior', //  Template parsing   Support PHP Built-in template engine and 3 Square template engine 
    ),
    'template_filter'=> array(
      'Behavior\ContentReplaceBehavior', //  Template output replacement 
    ),
    'view_filter'  => array(
      'Behavior\WriteHtmlCacheBehavior', //  Write to static cache 
    ),
  ),
);

After seeing this common application pattern code, we are a little clear about the application pattern extension of ThinkPHP, but we still know it but don't know why. How does defining a load file list and configuration change the core of the framework? The secret lies in the ThinkPHP boot class. Let's review the following again!


// Judge whether it exists or not core.php Configuration file (this is the operation mode temporarily defined by the development environment, as I understand it) 
     // No-load APP_MODE Defined schema file 
     $mode  =  include is_file(CONF_PATH.'core.php')?CONF_PATH.'core.php':MODE_PATH.APP_MODE.'.php';
     // In load mode core Defined core file list 
     foreach ($mode['core'] as $file){
       if(is_file($file)) {
        include $file;
        if(!APP_DEBUG) $content  .= compile($file);
       }
     }
 
     // Object defined in the load mode config Profile list 
     foreach ($mode['config'] as $key=>$file){
       is_numeric($key)?C(include $file):C($key,include $file);
     }
 
     //  Read the configuration file corresponding to the current application mode 
     if('common' != APP_MODE && is_file(CONF_PATH.'config_'.APP_MODE.'.php'))
       C(include CONF_PATH.'config_'.APP_MODE.'.php'); 
 
     //  In load mode alias Alias list definition 
     if(isset($mode['alias'])){
       self::addMap(is_array($mode['alias'])?$mode['alias']:include $mode['alias']);
     }
 
     //  Load the application alias definition file 
     if(is_file(CONF_PATH.'alias.php'))
       self::addMap(include CONF_PATH.'alias.php');
 
     //  In load mode tags Behavior definition 
     if(isset($mode['tags'])) {
       Hook::import(is_array($mode['tags'])?$mode['tags']:include $mode['tags']);
     }
 
     //  Load application behavior definition 
     if(is_file(CONF_PATH.'tags.php'))
       //  Allow applications to add development mode configuration definitions 
       Hook::import(include CONF_PATH.'tags.php'); 
 
     //  Loading Framework Underlying Language Pack 
     L(include THINK_PATH.'Lang/'.strtolower(C('DEFAULT_LANG')).'.php');

Through this code in ThinkPHP:: start (), the meaning and implementation method of the schema definition file are perfectly and seamlessly related.

3. Define a simple operation mode

There is an example of mode extension in the manual, which can be obtained here for analysis. 1. Define a simple operation mode of lite. First, create a new lite. php file under ThinkPHP/Mode directory. The contents are defined as follows:


return array(  
//  Configuration file  
'config'  => array(
        THINK_PATH.'Conf/convention.php',   //  System convention configuration 
        CONF_PATH.'config.php',   //  Apply common configuration  
 ),
 
 //  Alias definition   
 'alias'   => array(
       'Think\Exception'     => CORE_PATH . 'Exception'.EXT,
       'Think\Model'       => CORE_PATH . 'Model'.EXT, 
       'Think\Db'        => CORE_PATH . 'Db'.EXT,
       'Think\Cache'       => CORE_PATH . 'Cache'.EXT,
       'Think\Cache\Driver\File' => CORE_PATH . 'Cache/Driver/File'.EXT,
       'Think\Storage'      => CORE_PATH . 'Storage'.EXT,
 ),
 
 //  Function and class files  
 'core'   => array(
       MODE_PATH.'Lite/functions.php', 
       COMMON_PATH.'Common/function.php',
       MODE_PATH . 'Lite/App'.EXT, 
       MODE_PATH . 'Lite/Dispatcher'.EXT, 
       MODE_PATH . 'Lite/Controller'.EXT,
       MODE_PATH . 'Lite/View'.EXT,
       CORE_PATH . 'Behavior'.EXT,
 ),
 
 //  Behavior extension definition   
 'tags' => array(
       'view_parse'  => array(
           'Behavior\ParseTemplate', //  Template parsing   Support PHP Built-in template engine and 3 Square template engine 
        ),
        'template_filter'=> array(
           'Behavior\ContentReplace', //  Template output replacement 
        ),
 ),
);

From the above configuration, we found that most of the core files in core have been replaced. Of course, these program functions that need to be replaced need to be implemented by ourselves, but it is recommended that we directly copy the core files defined in the common mode to modify them. Next, we will implement the following core class library extension file App. class. php in ThinkPHP application development

Establish an Lite directory under ThinkPHP/Mode directory and establish App. class. php file under lite directory. The following is the implementation of the program file:


// The schema extension class library must be Think Namespace 
namespace Think;
 
/**
 * ThinkPHP  Application class   Execute application process management  Lite Schema extension class 
 *  Realization ThinkPHP When extending the core class library, imitate the original class library implementation as much as possible (unless the ThinkPHP Special understanding of framework source code) 
 *  Because some method in the extended core class file may be called in other core files that are not extended, unless you intend to extend them all 
 */
class App{
/**
 *  Application initialization 
 * @access public
 * @return void
 */
static public function init() {
    // Concrete reality 
}
 
/**
 *  Execute the application 
 * @access public
 * @return void
 */
static public function exec() {
    // Concrete realization 
}
 
/**
 *  Run an application instance   Shortcuts to use for entry files 
 * @access public
 * @return void
 */
static public function run() {
    // Concrete realization 
}
 
static public function logo(){
    // Concrete realization 
}
}

After the implementation of all the extended files, the APP_MODE constant can be defined as lite in the framework entry file.

In addition, we should pay attention to one point. The MODE_NAME constant required in the manual to change the operation mode is the method of defining the operation mode in the previous version 3.1, and users who use the new version should pay attention to this.


Related articles: