PHP code example of hartmann / resolve-strategy-interface

1. Go to this page and download the library: Download hartmann/resolve-strategy-interface 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/ */

    

hartmann / resolve-strategy-interface example snippets


use \Hartmann\ResolveStrategy\ResolveStrategyInterface

class ResolveRequestStrategy implements ResolveStrategyInterface
{
    /**
     * Checks wether the given class can be resolved by this strategy
     *
     * @param string $class The fully qualified namespace of the class to be resolved
     *
     * @return bool
     */
    public function suitable(string $class): bool
    {
        return method_exists($class, 'createFromEnvironment') && preg_match('/^.+?Request$/') === 1;
    }

    /**
     * A strategy allows you to resolve a dependency through a user-defined process.
     * For example, if a class is instantiated by a static method (e.g. ::createFromEnvironment), this can be resolved by a strategy.
     *
     * @param \Psr\Container\ContainerInterface $container The instance of the PSR-11 container
     * @param string                            $class     The fully qualified namespace of the class to be resolved
     *
     * @return object The instance of the requested class
     */
    public function resolve(\Psr\Container\ContainerInterface $container, string $class)
    {
        return call_user_func([$class, 'createFromEnvironment'], $container->get('environment'));
    }
}