栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

BladeCompiler-1.php

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

BladeCompiler-1.php

namespace IlluminateViewCompilers;

 

use IlluminateSupportArr;

use IlluminateSupportStr;

// namespace is so cool

class BladeCompiler extends Compiler implements CompilerInterface

{// has father class and api

   

    protected $extensions = [];// all of the registered extension.

 

   

    protected $customDirectives = [];// All custom "directive" handlers

 

   

    protected $path;// The file currently being compiled.

 

   

    protected $compilers = [

        'Extensions',

        'Statements',

        'Comments',

        'Echos',

    ];// All of the available compiler functions

   // a function collection

 

   

    protected $rawTags = ['{!!', '!!}'];// Array of opening and closing tags for raw echos.

 

   

    protected $contentTags = ['{{', '}}'];//Array of opening and closing tags for regular echos.

 

   

    protected $escapedTags = ['{{{', '}}}'];//Array of opening and closing tags for escaped echos.

 

   

    protected $echoFormat = 'e(%s)';// The "regular" /legacy echo string format.

 

   

    protected $footer = [];// Array of footer lines to the added to template.

 

   

    protected $forelseCounter = 0;//Counter to keep track of nested for else statements.

 

   

    public function compile($path = null)

    {//Compile the view at the given path.

        if ($path) {// $path

            $this->setPath($path);// path

        }

 

        $contents = $this->compileString($this->files->get($this->getPath()));// contents

       // get the contents

 

        if (! is_null($this->cachePath)) {

            $this->files->put($this->getCompiledPath($this->getPath()), $contents);

        }// if not null

    }// return it,

 

   

    public function getPath()

    {

        return $this->path;

    }// big get just get you want

 

   

    public function setPath($path)

    {

        $this->path = $path;

    }// set Path

 

   

    public function compileString($value)

    {//Compile the given Blade template contents.

        $result = '';// this result

 

        $this->footer = [];// this footer

 

        // Here we will loop through all of the tokens returned by the Zend lexer and

        // parse each one into the corresponding valid PHP. We will then have this

        // template as the correctly rendered PHP that can be rendered natively.

        foreach (token_get_all($value) as $token) {

            $result .= is_array($token) ? $this->parseToken($token) : $token;

        }// token_get_all

       //loop the token_get_all

 

        // If there are any footer lines that need to get added to a template we will

        // add them here at the end of the template. This gets used mainly for the

        // template inheritance via the extends keyword that should be appended.

        if (count($this->footer) > 0) {

            $result = ltrim($result, PHP_EOL)

                    .PHP_EOL.implode(PHP_EOL, array_reverse($this->footer));

        }// append something

 

        return $result;//return result

    }

 

   

    protected function parseToken($token)

    {//Parse the tokens from the template.

        list($id, $content) = $token;//get list

 

        if ($id == T_INLINE_HTML) {// a special id

            foreach ($this->compilers as $type) {// loop compile

                $content = $this->{"compile{$type}"}($content);// this is a good use

               //$content = $this=>{"compile{$type}"}($content);

            }

        }

 

        return $content;//return this type

    }// a classic function type

 

   

    protected function compileExtensions($value)

    {

        foreach ($this->extensions as $compiler) {

            $value = call_user_func($compiler, $value, $this);

        }// loop extensions and call_user_func

 

        return $value;

    }//Execute the user defined extensions

 

   

    protected function compileComments($value)

    {

        $pattern = sprintf('/%s--((.|s)*?)--%s/', $this->contentTags[0], $this->contentTags[1]);

// get the pattern

       // a very good function type

       // just comments

        return preg_replace($pattern, '', $value);

    }//compile Blade comments into valid PHP

 

   

    protected function compileEchos($value)

    {//compile Blade echos into valid PHP.

        foreach ($this->getEchoMethods() as $method => $length) {

            $value = $this->$method($value);//get the value

        }// foreach

 

        return $value;

    }// return $value

 

   

    protected function getEchoMethods()

    {//Get the echo methods in the proper order for compilation.

        $methods = [

            'compileRawEchos' => strlen(stripcslashes($this->rawTags[0])),

            'compileEscapedEchos' => strlen(stripcslashes($this->escapedTags[0])),

            'compileRegularEchos' => strlen(stripcslashes($this->contentTags[0])),

        ];// set some regular method to use

 

        uksort($methods, function ($method1, $method2) use ($methods) {

           // use user define method to sort the array

            // Ensure the longest tags are processed first

            if ($methods[$method1] > $methods[$method2]) {

                return -1;

            }

            if ($methods[$method1] < $methods[$method2]) {

                return 1;

            }

 

            // Otherwise give preference to raw tags (assuming they've overridden)

            if ($method1 === 'compileRawEchos') {

                return -1;

            }

            if ($method2 === 'compileRawEchos') {

                return 1;

            }

 

            if ($method1 === 'compileEscapedEchos') {

                return -1;

            }

            if ($method2 === 'compileEscapedEchos') {

                return 1;

            }

        });

 

        return $methods;

    }// The methods.


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/230148.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号