PHP code example of pollora / discovery

1. Go to this page and download the library: Download pollora/discovery 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/ */

    

pollora / discovery example snippets


use Pollora\Discovery\Domain\Contracts\DiscoveryInterface;
use Pollora\Discovery\Domain\Contracts\DiscoveryLocationInterface;
use Pollora\Discovery\Domain\Contracts\ReflectionCacheInterface;
use Pollora\Discovery\Domain\Services\IsDiscovery;
use Spatie\StructureDiscoverer\Data\DiscoveredClass;
use Spatie\StructureDiscoverer\Data\DiscoveredStructure;

final class ShortcodeDiscovery implements DiscoveryInterface
{
    use IsDiscovery;

    public function discover(DiscoveryLocationInterface $location, DiscoveredStructure $structure, ?ReflectionCacheInterface $reflectionCache = null): void
    {
        if (! $structure instanceof DiscoveredClass) {
            return;
        }

        $class = $structure->namespace.'\\'.$structure->name;

        foreach ($reflectionCache->getClassAttributes($class, Shortcode::class) as $attribute) {
            $this->getItems()->add($location, [$attribute->newInstance()->tag, $class]);
        }
    }

    public function apply(): void
    {
        foreach ($this->getItems() as [$tag, $class]) {
            add_shortcode($tag, [new $class, 'render']);
        }
    }
}

use Pollora\Discovery\Domain\Models\DiscoveryLocation;
use Pollora\Discovery\Infrastructure\Services\DiscoveryEngine;

(new DiscoveryEngine)
    ->addLocation(new DiscoveryLocation('MyPlugin\\', __DIR__.'/src'))
    ->addDiscovery('shortcodes', new ShortcodeDiscovery)
    ->run();

use Pollora\Discovery\Infrastructure\Cache\Psr16DiscoverCacheDriver;
use Pollora\Discovery\Infrastructure\Services\DiscoveryCacheManager;

$engine = new DiscoveryEngine(
    cacheManager: new DiscoveryCacheManager(
        cacheDriver: new Psr16DiscoverCacheDriver($psr16Cache, ttl: DAY_IN_SECONDS),
        logger: $logger,
    ),
);

use Pollora\Attributes\SkipDiscovery;

#[SkipDiscovery]                                  // no discovery sees it
#[SkipDiscovery(except: [HookDiscovery::class])]  // only the ones listed do
final class InternalHelper {}