A simple tool to resolve the fully qualified class name and an instance of the class given the path to the file.
$ composer require envorradev/file-class-resolver
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;
ClassResolver::resolve($filename);
Returns
'SomeNamespace\SomeFolder\SomeClass'
ClassResolver::make($someOtherClassPath);
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']);
ClassResolver::resolver($filename);
Returns a ClassResolver
Instance.
In the below examples:
$resolver = ClassResolver::resolve($filename);
Gets the fully qualified class name.
Same as ClassResolver::resolve($filename)
and $resolver->getFullyQualifiedClassName()
$resolver->getClass();
Returns
'SomeNamespace\SomeFolder\SomeClass'
Get an instance of the class.
Same as ClassResolver::make($filename, $parameters)
$resolver->getClassInstance(['string']);
Returns an instance of SomeClass
Gets onlt the name of the class.
$resolver->getClassName();
Returns
'SomeClass'
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: [],
}
Gets the fully qualified class name.
Same as ClassResolver::resolve($filename)
and $resolver->getClass()
$resolver->getFullyQualifiedClassName();
Returns
'SomeNamespace\SomeFolder\SomeClass'
Gets only the namespace of the class.
$resolver->getNamespace();
Returns
'SomeNamespace\SomeFolder'
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: [],
},
],
}