PHP code example of spatie / php-structure-discoverer

1. Go to this page and download the library: Download spatie/php-structure-discoverer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / php-structure-discoverer example snippets


use Spatie\StructureDiscoverer\Discover;

// PostModel::class, Collection::class, ...
Discover::in(__DIR__)->classes()->implementing(Arrayable::class)->get(); 

return [
    /*
     *  A list of files that should be ignored during the discovering process.
     */
    'ignored_files' => [

    ],

    /**
     * The directories where the package should search for structure scouts
     */
    'structure_scout_directories' => [
        app_path(),
    ],

    /*
     *  Configure the cache driver for discoverers
     */
    'cache' => [
        'driver' => \Spatie\StructureDiscoverer\Cache\LaravelDiscoverCacheDriver::class,
        'store' => null,
    ]
];

Discover::in(__DIR__)->...

Discover::in(app_path('models'), app_path('enums'))->...

Discover::in(__DIR__)->get();

Discover::in(__DIR__)->classes()->get();

Discover::in(__DIR__)->interfaces()->get();

Discover::in(__DIR__)->enums()->get();

Discover::in(__DIR__)->traits()->get();

Discover::in(__DIR__)->named('MyAwesomeClass')->get();

Discover::in(__DIR__)->extending(Model::class)->get();

Discover::in(__DIR__)->implementing(Arrayable::class)->get();

Discover::in(__DIR__)->withAttribute(Cast::class)->get();

Discover::in(__DIR__)
    ->custom(fn(DiscoveredStructure $structure) => $structure->namespace === 'App')
    ->get()

class AppDiscoverCondition extends DiscoverCondition 
{
    public function satisfies(DiscoveredStructure $discoveredData): bool
    {
        return $structure->namespace === 'App';
    }
};

Discover::in(__DIR__)
    ->custom(new AppDiscoverCondition())
    ->get()

Discover::in(__DIR__)->classes()->implementing(Arrayable::class)->get();

Discover::in(__DIR__)
    ->any(
        ConditionBuilder::create()->classes(),
        ConditionBuilder::create()->enums()
    )
    ->get();

Discover::in(__DIR__)
    ->any(
        ConditionBuilder::create()->exact(
            ConditionBuilder::create()->classes(),
            ConditionBuilder::create()->implementing(Arrayble::class),
        ),
        ConditionBuilder::create()->exact(
            ConditionBuilder::create()->enums(),
            ConditionBuilder::create()->implementing(Stringable::class),
        )
    )
    ->get();

Discover::in(__DIR__)
    ->any(
        ConditionBuilder::create()->exact(
            ConditionBuilder::create()->classes()->implementing(Arrayble::class),
        ),
        ConditionBuilder::create()->exact(
            ConditionBuilder::create()->enums()->implementing(Stringable::class),
        )
    )
    ->get();

use Spatie\StructureDiscoverer\Enums\Sort;

Discover::in(__DIR__)->sortBy(Sort::Name)->get();

use Spatie\StructureDiscoverer\Enums\Sort;

Discover::in(__DIR__)->sortBy(Sort::Name);
Discover::in(__DIR__)->sortBy(Sort::Size);
Discover::in(__DIR__)->sortBy(Sort::Type);
Discover::in(__DIR__)->sortBy(Sort::Extension);
Discover::in(__DIR__)->sortBy(Sort::ChangedTime);
Discover::in(__DIR__)->sortBy(Sort::ModifiedTime);
Discover::in(__DIR__)->sortBy(Sort::AccessedTime);
Discover::in(__DIR__)->sortBy(Sort::CaseInsensitiveName);

class EnumsStructureScout extends StructureScout
{
    protected function definition(): Discover|DiscoverConditionFactory
    {
        return Discover::in(__DIR__)->enums();
    }

    public function cacheDriver(): DiscoverCacheDriver
    {
        return new FileDiscoverCacheDriver('/path/to/temp/directory');
    }
}

EnumsStructureScout::create()->get();

StructureScoutManager::cache([__DIR__]); 

StructureScoutManager::clear([__DIR__]); 

StructureScoutManager::add(SettingsStructureScout::class); 

interface DiscoverCacheDriver
{
    public function has(string $id): bool;

    public function get(string $id): array;

    public function put(string $id, array $discovered): void;

    public function forget(string $id): void;
}

Discover::in(__DIR__)
   ->withCache(
      'Some identifier',
      new FileDiscoverCacheDriver('/path/to/temp/directory);
   )
    ->get();

Discover::in(__DIR__)->parallel()->get();

Discover::in(__DIR__)->parallel(100)->get();

class Request
{
}

class FormRequest extends Request
{
}

class UserFormRequest extends FormRequest
{
}

Discover::in(__DIR__)->extending(Request::class)->get();

Discover::in(__DIR__)->extendingWithoutChain(Request::class)

Discover::in(__DIR__)->implementingWithoutChain(Request::class)

Discover::in(__DIR__)->withoutChains()->extending(Request::class)->get();

Discover::in(__DIR__)->full()->get();

class DiscoveredClass extends DiscoveredStructure
{
    public function __construct(
        string $name,
        string $file,
        string $namespace,
        public bool $isFinal,
        public bool $isAbstract,
        public bool $isReadonly,
        public ?string $extends,
        public array $implements,
        public array $attributes,
        public ?array $extendsChain = null,
        public ?array $implementsChain = null,
    ) {
    }
}

class DiscoveredInterface extends DiscoveredStructure
{
    public function __construct(
        string $name,
        string $file,
        string $namespace,
        public array $extends,
        public array $attributes,
        public ?array $extendsChain = null,
    ) {
    }

class DiscoveredEnum extends DiscoveredStructure
{
    public function __construct(
        public string $name,
        public string $namespace,
        public string $file,
        public DiscoveredEnumType $type,
        public array $implements,
        public array $attributes,
        public ?array $implementsChain = null,
    ) {
    }
}

class DiscoveredTrait extends DiscoveredStructure
{
    public function __construct(
        public string $name,
        public string $namespace,
        public string $file,
    ) {
    }
}

Discover::in(__DIR__)
   ->useReflection(
      basePath: '/path/to/project/root',
      rootNamespace: null
   )
   ->get();

Discover::in(__DIR__)
   ->useReflection(basePath: base_path())
   ->get();
bash
composer 
bash
php artisan vendor:publish --tag="structure-discoverer-config"
`bash
php artisan structure-scouts:cache
`bash
php artisan structure-scouts:clear