PHP code example of teofanis / hook-press

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

    

teofanis / hook-press example snippets


return [
     // Namespaces to consider as your application roots
    'roots' => [
        'App\\',
    ],

    // Optional: trait grouping
    'traits' => [
        'enabled'    => true,
        'namespaces' => ['App\\Traits\\'],
        'group_key'  => 'traits', // where the trait map will be stored
    ],

    // Define your discovery “maps”
    'maps' => [
        'payout_methods' => [
            'namespaces' => ['App\\Classes\\PayoutMethods\\'],
            'conditions' => [
                'isInstantiable',
                'implementsInterface' => 'App\\Interfaces\\PayoutMethod',
            ],
        ],
    ],

    // Exclusions (exact class, namespace prefix, or regex)
    'exclusions' => [
        'classes'    => [],
        'namespaces' => [],
        'regex'      => [],
    ],

    // Where the computed map is stored
    'store' => [
        'driver' => 'file', // 'file' or 'cache'
        'file'   => ['path'  => 'bootstrap/cache/hook-press.php'],
        'cache'  => ['store' => null, 'key' => 'hookpress:map', 'ttl' => null],
    ],

    // Where HookPress reads the Composer classmap from (leave as default in apps)
    'composer' => [
        'classmap_path'   => 'vendor/composer/autoload_classmap.php',
        'artisan_command' => 'hook-press:build',
    ],
];

use HookPress\Facades\HookPress;
// Entire map (all keys)
$all = HookPress::map();
// Specific map
$methods = HookPress::map('payout_methods');

// Classes that use a trait
$searchables = HookPress::classesUsing(\App\Traits\Searchable::class);

// Rebuild on demand (usually done via Composer hook)
HookPress::refresh();

// Clear the stored map
HookPress::clear();


'models' => [
    'namespaces' => ['App\\Models\\'],
    'conditions' => [
        'extends' => \Illuminate\Database\Eloquent\Model::class,
        'isInstantiable'// removes abstracts if-any
    ],
],

'invokables' => [
    'namespaces' => ['App\\Actions\\', 'App\\Jobs\\'],
    'conditions' => [
        'isInstantiable',
        'hasMethod' => ['name' => '__invoke', 'public' => true],
    ],
],


'controllers' => [
    'namespaces' => ['App\\Http\\Controllers\\'],
    'conditions' => [
        'isInstantiable',
        'nameMatches' => '/Controller$/',
    ],
],

namespace App\HookPress;

use HookPress\Contracts\Condition;
use ReflectionClass;

class ConstructorTakesLogger implements Condition
{
    public function passes(ReflectionClass $ref, mixed $arg = null): bool
    {
        $ctor = $ref->getConstructor();
        if (! $ctor) return false;

        foreach ($ctor->getParameters() as $p) {
            $t = $p->getType();
            if ($t && ltrim((string) $t, '\\') === 'Psr\\Log\\LoggerInterface') {
                return true;
            }
        }
        return false;
    }
}


'services_requiring_logger' => [
    'namespaces' => ['App\\Services\\'],
    'conditions' => [
        \App\HookPress\ConstructorTakesLogger::class => null,
    ],
],
bash
php artisan vendor:publish --tag="hook-press-config"
json
"post-install-cmd": ["@php artisan hook-press:build --no-ansi --no-interaction"],
"post-update-cmd":  ["@php artisan hook-press:build --no-ansi --no-interaction"]

bash
php artisan hook-press:build        # compute and store the map
php artisan hook-press:show         # print the map
php artisan hook-press:show payout_methods
php artisan hook-press:clear
php artisan hook-press:build --no-traits  # skip trait grouping