Provides a finder for classy constructs (classes, interfaces, and traits).
Run
$ composer require ergebnis/classy
Use Constructs::fromSource()
to collect classy constructs in source code:
<?php
use Ergebnis\Classy\Construct;
use Ergebnis\Classy\Constructs;
$source = <<<'PHP'
<?php
namespace Example;
class Foo {}
interface Bar {}
trait Baz {}
PHP;
/** @var Construct[] $constructs */
$constructs = Constructs::fromSource($source);
$names = array_map(static function (Construct $construct): string {
return $construct->name();
}, $constructs);
var_dump($names); // ['Example\Bar', 'Example\Baz', 'Example\Foo']
Use Constructs::fromDirectory()
to collect classy constructs in a directory:
<?php
use Ergebnis\Classy\Construct;
use Ergebnis\Classy\Constructs;
/** @var Construct[] $constructs */
$constructs = Constructs::fromDirectory(__DIR__ . '/example');
$names = array_map(static function (Construct $construct): string {
return $construct->name();
}, $constructs);
var_dump($names); // ['Example\Bar', 'Example\Bar\Baz', 'Example\Foo\Bar\Baz']
Please have a look at CHANGELOG.md
.
Please have a look at CONTRIBUTING.md
.
Please have a look at CODE_OF_CONDUCT.md
.
This package is licensed using the MIT License.
Please have a look at LICENSE.md
.
The algorithm for finding classes in PHP files in Constructs
has been adopted from Zend\File\ClassFileLocator
(originally licensed under BSD-3-Clause).
π¬ Subscribe to my list, and I will occasionally send you an email to let you know what I am working on.