ITPub博客

首页 > IT职业 > IT职场 > BladeCompiler-1.php

BladeCompiler-1.php

IT职场 作者:zzfh 时间:2021-09-09 09:16:41 0 删除 编辑

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]

                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.


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4313/viewspace-2801678/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-12

  • 博文量
    0
  • 访问量
    655