Search
Blade Parser

Blade Parser

Workspaces

Documents make it easier to work with the many types of nodes produced by the parser, and Workspaces make it easier to interact with many documents simultaneously. Using workspaces, we can compile multiple documents at once or look for specific nodes and features across the workspace.

1<?php
2 
3use Stillat\BladeParser\Document\Document;
4use Stillat\BladeParser\Workspaces\Workspace;
5 
6$workspace = new Workspace();
7$workspace->addDirectory(resource_path('views'))->getDocuments()->each(function (Document $doc) {
8 // Interact with documents in the workspace.
9});
10 
11// Find all components in the workspace.
12$components = $workspace->getComponents();

#Available Methods

#addDirectory

Recursively adds all Blade templates to the workspace discovered within the provided directory.

Argument

Description

$directory

The path.

1<?php
2 
3use Stillat\BladeParser\Workspaces\Workspace;
4 
5public function addDirectory(
6 string $directory
7): Workspace;

#addFile

Adds a single Blade template to the workspace.

If the provided path does not end with a configured Blade extension, the file will not be added to the workspace.

Argument

Description

$path

The file path.

1<?php
2 
3use Stillat\BladeParser\Workspaces\Workspace;
4 
5public function addFile(
6 string $path
7): Workspace;

#getDocuments

Returns a collection of Document instances.

1<?php
2 
3use Illuminate\Support\Collection;
4 
5public function getDocuments(): Collection;

#getFileCount

Returns the number of template files discovered in the workspace.

1<?php
2 
3public function getFileCount(): int;

#findDirectivesByName

Returns all directives with the provided name in the workspace.

Argument

Description

$name

The directive name to search

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function findDirectivesByName(
6 string $name
7): NodeCollection;

#getComments

Retrieves all Blade comments in the workspace.

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function getComments(): NodeCollection;

#hasAnyComments

Returns a value indicating if the workspace has any Blade comments.

1<?php
2 
3public function hasAnyComments(): bool;

#getEchoes

Returns all Blade echo nodes in the workspace.

Retrieves all Blade echo statements from the workspace. Echo nodes are created after parsing the following types of syntax:

  • Normal echo: {{ $variable }}

  • Triple echo: {{{ $variable }}}

  • Raw Echo: {!! $variable !!}

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function getEchoes(): NodeCollection;

#getPhpBlocks

Returns all Blade php/endphp blocks within the workspace.

Retrieves all raw PHP blocks within the workspace templates that were created using Blade's @php/@endphp directives. Raw @php directives that contain arguments will not be converted to instances of PhpBlockNode, and will instead become instances of DirectiveNode.

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function getPhpBlocks(): NodeCollection;

#getPhpTags

Returns all PHP tags within the workspace.

PHP tags are created after parsing raw PHP regions within a template. PHP tags will be created whenever the following types of PHP tags are encountered:

  • PHP Short Echo: <?= ?>

  • PHP Open: <?php ?>
    If you are looking to retrieve PHP blocks created using Blade's @php @endphp directives you should refer to the getPhpBlocks method instead.
    Note: The PHP short echo tags will be parsed even if short tags have been disabled in the PHP configuration.

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function getPhpTags(): NodeCollection;

#getVerbatimBlocks

Returns all verbatim blocks within the workspace.

Returns all valid @verbatim/@endverbatim regions within a source document. Unpaired verbatim directives that could not be converted into a valid VerbatimNode instance will either become part of the document's literal content (in the case of the @verbatim directive), or will become a standalone DirectiveNode (in the case of @endverbatim).

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function getVerbatimBlocks(): NodeCollection;

#getLiterals

Returns all literal content nodes within the workspace.

Returns the source template's content that could not be parsed into a valid Blade construct. If a document contains no Blade code, the parser will return a single instance of LiteralNode.

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function getLiterals(): NodeCollection;

#getDirectives

Returns all directives within the workspace.

Returns all directives from a source document. Some directive pairs, such as the @php/@endphp and @verbatim/@endverbatim pairs will not result in an instance of DirectiveNode, as they are handled by the parser directly.

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function getDirectives(): NodeCollection;

#hasAnyDirectives

Tests if the workspace contains any Blade directives.

1<?php
2 
3public function hasAnyDirectives(): bool;

#getComponents

Returns all component tags within workspace document.

This method will return all component tags within a document, including closing tags and self-closing tags.

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function getComponents(): NodeCollection;

#getOpeningComponentTags

Returns all self-closing or opening component tags within the workspace.

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function getOpeningComponentTags(): NodeCollection;

#findComponentsByTagName

Finds all components with the provided tag name.

This method will return all component tags that match the provided name, including closing tags.

Argument

Description

$tagName

The tag name to filter on.

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function findComponentsByTagName(
6 string $tagName
7): NodeCollection;

#hasAnyComponents

Returns a value indicating if the workspace has any component tags.

1<?php
2 
3public function hasAnyComponents(): bool;

#hasDirective

Tests if the workspace contains a directive with the provided name.

Argument

Description

$name

The directive name.

1<?php
2 
3public function hasDirective(
4 string $name
5): bool;

#allOfType

Finds all nodes of the provided type.

Argument

Description

$type

The type to search.

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function allOfType(
6 string $type
7): NodeCollection;

#allNotOfType

Finds all nodes that are not of the provided type.

Argument

Description

$type

The type to search.

1<?php
2 
3use Stillat\BladeParser\Nodes\NodeCollection;
4 
5public function allNotOfType(
6 string $type
7): NodeCollection;

#hasAnyOfType

Tests if the workspace contains any node of the provided type.

Argument

Description

$type

The desired type.

1<?php
2 
3public function hasAnyOfType(
4 string $type
5): bool;

#getAllStructures

Returns all the workspace structures.

This method automatically performs structural analysis on a document.

1<?php
2 
3use Illuminate\Support\Collection;
4 
5public function getAllStructures(): Collection;

#getRootStructures

Returns the direct workspace structures.

This method automatically performs structural analysis. Only structures that are at the root of a document, without any parent node, will be returned.

1<?php
2 
3use Illuminate\Support\Collection;
4 
5public function getRootStructures(): Collection;

#getAllSwitchStatements

Returns all the workspaces' switch statements.

This method automatically performs structural analysis.

1<?php
2 
3use Illuminate\Support\Collection;
4 
5public function getAllSwitchStatements(): Collection;

#getRootSwitchStatements

Returns all the direct switch statements.

This method automatically performs structural analysis. Only @switch statements that appear at the root of a document, without any parent nodes, will be returned.

1<?php
2 
3use Illuminate\Support\Collection;
4 
5public function getRootSwitchStatements(): Collection;

#getAllConditions

Returns all the workspace conditions.

This method automatically performs structural analysis.

1<?php
2 
3use Illuminate\Support\Collection;
4 
5public function getAllConditions(): Collection;

#getRootConditions

Returns all the root workspace conditions.

This method automatically performs structural analysis. Only structures that appear at the root of the document, without any parent node, will be returned.

1<?php
2 
3use Illuminate\Support\Collection;
4 
5public function getRootConditions(): Collection;

#getAllForElse

Returns all the workspace for-else blocks.

This method automatically performs structural analysis.

1<?php
2 
3use Illuminate\Support\Collection;
4 
5public function getAllForElse(): Collection;

#getRootForElse

Returns the direct for-else blocks.

This method automatically performs structural analysis. Only nodes that appear at the root of a document, without any parent nodes, will be returned.

1<?php
2 
3use Illuminate\Support\Collection;
4 
5public function getRootForElse(): Collection;

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.