Вход Регистрация
Файл: vendor/spatie/laravel-ignition/src/Views/BladeSourceMapCompiler.php
Строк: 174
<?php

namespace SpatieLaravelIgnitionViews;

use 
IlluminateViewCompilersBladeCompiler;
use 
Throwable;

class 
BladeSourceMapCompiler
{
    protected 
BladeCompiler $bladeCompiler;

    public function 
__construct()
    {
        
$this->bladeCompiler app('blade.compiler');
    }

    public function 
detectLineNumber(string $filenameint $compiledLineNumber): int
    
{
        
$map $this->compileSourcemap((string)file_get_contents($filename));

        return 
$this->findClosestLineNumberMapping($map$compiledLineNumber);
    }

    protected function 
compileSourcemap(string $value): string
    
{
        try {
            
$value $this->addEchoLineNumbers($value);

            
$value $this->addStatementLineNumbers($value);

            
$value $this->addBladeComponentLineNumbers($value);

            
$value $this->bladeCompiler->compileString($value);

            return 
$this->trimEmptyLines($value);
        } catch (
Throwable $e) {
            
report($e);

            return 
$value;
        }
    }

    protected function 
addEchoLineNumbers(string $value): string
    
{
        
$echoPairs = [['{{''}}'], ['{{{''}}}'], ['{!!''!!}']];

        foreach (
$echoPairs as $pair) {
            
// Matches {{ $value }}, {!! $value !!} and  {{{ $value }}} depending on $pair
            
$pattern sprintf('/(@)?%ss*(.+?)s*%s(r?n)?/s'$pair[0], $pair[1]);

            if (
preg_match_all($pattern$value$matchesPREG_OFFSET_CAPTURE)) {
                foreach (
array_reverse($matches[0]) as $match) {
                    
$position mb_strlen(substr($value0$match[1]));

                    
$value $this->insertLineNumberAtPosition($position$value);
                }
            }
        }

        return 
$value;
    }

    protected function 
addStatementLineNumbers(string $value): string
    
{
        
// Matches @bladeStatements() like @if, @component(...), @etc;
        
$shouldInsertLineNumbers preg_match_all(
            
'/B@(@?w+(?:::w+)?)([ t]*)(( ( (?>[^()]+) | (?3) )* ))?/x',
            
$value,
            
$matches,
            
PREG_OFFSET_CAPTURE
        
);

        if (
$shouldInsertLineNumbers) {
            foreach (
array_reverse($matches[0]) as $match) {
                
$position mb_strlen(substr($value0$match[1]));

                
$value $this->insertLineNumberAtPosition($position$value);
            }
        }

        return 
$value;
    }

    protected function 
addBladeComponentLineNumbers(string $value): string
    
{
        
// Matches the start of `<x-blade-component`
        
$shouldInsertLineNumbers preg_match_all(
            
'/<s*x[-:]([w-:.]*)/mx',
            
$value,
            
$matches,
            
PREG_OFFSET_CAPTURE
        
);

        if (
$shouldInsertLineNumbers) {
            foreach (
array_reverse($matches[0]) as $match) {
                
$position mb_strlen(substr($value0$match[1]));

                
$value $this->insertLineNumberAtPosition($position$value);
            }
        }

        return 
$value;
    }

    protected function 
insertLineNumberAtPosition(int $positionstring $value): string
    
{
        
$before mb_substr($value0$position);
        
$lineNumber count(explode("n"$before));

        return 
mb_substr($value0$position)."|---LINE:{$lineNumber}---|".mb_substr($value$position);
    }

    protected function 
trimEmptyLines(string $value): string
    
{
        
$value preg_replace('/^|---LINE:([0-9]+)---|$/m'''$value);

        return 
ltrim((string)$valuePHP_EOL);
    }

    protected function 
findClosestLineNumberMapping(string $mapint $compiledLineNumber): int
    
{
        
$map explode("n"$map);

        
// Max 20 lines between compiled and source line number.
        // Blade components can span multiple lines and the compiled line number is often
        // a couple lines below the source-mapped `<x-component>` code.
        
$maxDistance 20;

        
$pattern '/|---LINE:(?P<line>[0-9]+)---|/m';
        
$lineNumberToCheck $compiledLineNumber 1;

        while (
true) {
            if (
$lineNumberToCheck $compiledLineNumber $maxDistance) {
                
// Something wrong. Return the $compiledLineNumber (unless it's out of range)
                
return min($compiledLineNumbercount($map));
            }

            if (
preg_match($pattern$map[$lineNumberToCheck] ?? ''$matches)) {
                return (int)
$matches['line'];
            }

            
$lineNumberToCheck--;
        }
    }
}
Онлайн: 1
Реклама