envorradev/file-class-resolver

0.1 2022-09-10 14:31 UTC

This package is auto-updated.

Last update: 2024-11-14 04:22:20 UTC


README

A simple tool to resolve the fully qualified class name and an instance of the class given the path to the file.

Install

$ composer require envorradev/file-class-resolver

Usage

The following examples will use the following sample class:

namespace SomeNamespace\SomeFolder;

class SomeClass {
    public function __construct(
        public string $aRequiredString,
        public int $anOptionalInt = 5,
    ) {}
}
$filename = __DIR__.'/SomeFolder/SomeClass.php';

They also assume you have imported the resolver class via:

use Envorra\FileClassResolver\ClassResolver;

Resolve a Fully Qualified Class Name

ClassResolver::resolve($filename);

Returns

'SomeNamespace\SomeFolder\SomeClass'

Make an Instance

Class with No Required Parameters

ClassResolver::make($someOtherClassPath);

Class with Required Parameters

Pass the parameters as an array as the parameter for the make method:

ClassResolver::make($filename, ['string value', 10]);

Only the required parameters need to be passed:

ClassResolver::make($filename, ['string value']);

You can pass the parameters as named parameters:

ClassResolver::make($filename, ['aRequiredString' => 'string value', 'anOptionalInt' => 7]);

When using named parameters, the order does not matter:

ClassResolver::make($filename, ['anOptionalInt' => 7, 'aRequiredString' => 'string value']);

Get the Resolver Instance

ClassResolver::resolver($filename);

Returns a ClassResolver Instance.

Available ClassResolver Methods

In the below examples:

$resolver = ClassResolver::resolve($filename);

getClass(): ?string

Gets the fully qualified class name.

Same as ClassResolver::resolve($filename) and $resolver->getFullyQualifiedClassName()

$resolver->getClass();

Returns

'SomeNamespace\SomeFolder\SomeClass'

getClassInstance(array $parameters = []): ?object

Get an instance of the class.

Same as ClassResolver::make($filename, $parameters)

$resolver->getClassInstance(['string']);

Returns an instance of SomeClass

getClassName(): ?string

Gets onlt the name of the class.

$resolver->getClassName();

Returns

'SomeClass'

getClassNode(): \PhpParser\Node\Stmt\Class_

Gets the \PhpParser\Node\Stmt\Class_ node.

See: nikic/php-parser and Class_

$resolver->getClassNode();

Returns something like:

PhpParser\Node\Stmt\Class_ {#8563
    +name: PhpParser\Node\Identifier {#8550
        +name: "SomeClass",
    },
    +stmts: [
        PhpParser\Node\Stmt\ClassMethod {#8562
            +flags: 1,
            +byRef: false,
            +name: PhpParser\Node\Identifier {#8551
                +name: "__construct",
            },
            +params: [
                PhpParser\Node\Param {#8554
                    +type: PhpParser\Node\Identifier {#8553
                        +name: "string",
                    },
                    +byRef: false,
                    +variadic: false,
                    +var: PhpParser\Node\Expr\Variable {#8552
                        +name: "aRequiredString",
                    },
                    +default: null,
                    +flags: 1,
                    +attrGroups: [],
                },
                PhpParser\Node\Param {#8557
                    +type: PhpParser\Node\Identifier {#8556
                        +name: "int",
                    },
                    +byRef: false,
                    +variadic: false,
                    +var: PhpParser\Node\Expr\Variable {#8555
                        +name: "anOptionalInt",
                    },
                    +default: null,
                    +flags: 1,
                    +attrGroups: [],
                },
            ],
            +returnType: null,
            +stmts: [],
            +attrGroups: [],
        },
    ],
    +attrGroups: [],
    +namespacedName: null,
    +flags: 0,
    +extends: null,
    +implements: [],
}

getFullyQualifiedClassName(): ?string

Gets the fully qualified class name.

Same as ClassResolver::resolve($filename) and $resolver->getClass()

$resolver->getFullyQualifiedClassName();

Returns

'SomeNamespace\SomeFolder\SomeClass'

getNamespace(): ?string

Gets only the namespace of the class.

$resolver->getNamespace();

Returns

'SomeNamespace\SomeFolder'

getNamespaceNode(): \PhpParser\Node\Stmt\Namespace_

Gets the \PhpParser\Node\Stmt\Namespace_ node.

See: nikic/php-parser and Namespace_

$resolver->getNamespaceNode();

Returns something like:

PhpParser\Node\Stmt\Namespace_ {#8549
    +name: PhpParser\Node\Name {#8548
        +parts: [
            "SomeNamespace",
            "SomeFolder",
        ],
    },
    +stmts: [
        PhpParser\Node\Stmt\Class_ {#8563
            +name: PhpParser\Node\Identifier {#8550
                +name: "SomeClass",
            },
            +stmts: [
                PhpParser\Node\Stmt\ClassMethod {#8562
                    +flags: 1,
                    +byRef: false,
                    +name: PhpParser\Node\Identifier {#8551
                        +name: "__construct",
                    },
                    +params: [
                        PhpParser\Node\Param {#8554
                            +type: PhpParser\Node\Identifier {#8553
                                +name: "string",
                            },
                            +byRef: false,
                            +variadic: false,
                            +var: PhpParser\Node\Expr\Variable {#8552
                                +name: "aRequiredString",
                            },
                            +default: null,
                            +flags: 1,
                            +attrGroups: [],
                        },
                        PhpParser\Node\Param {#8557
                            +type: PhpParser\Node\Identifier {#8556
                                +name: "int",
                            },
                            +byRef: false,
                            +variadic: false,
                            +var: PhpParser\Node\Expr\Variable {#8555
                                +name: "anOptionalInt",
                            },
                            +default: null,
                            +flags: 1,
                            +attrGroups: [],
                        },
                    ],
                    +returnType: null,
                    +stmts: [],
                    +attrGroups: [],
                },
            ],
            +attrGroups: [],
            +namespacedName: null,
            +flags: 0,
            +extends: null,
            +implements: [],
        },
    ],
}