PHP code example of php-di / invoker

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

    

php-di / invoker example snippets


$app->get('/project/{project}/issue/{issue}', function ($project, $issue) {
    // ...
});

$app->command('greet [name] [--yell]', function ($name, $yell) {
    // ...
});

$app->get('/hello/:name', function ($name) {
    // ...
});

$app->get('/hello/{name}', function (Silex\Application $app, $name) {
    // ...
});

$app->command('greet [name]', function ($name, OutputInterface $output) {
    // ...
});

$container->call(function (Logger $logger, EntityManager $em) {
    // ...
});

$invoker = new Invoker\Invoker;

$invoker->call(function () {
    echo 'Hello world!';
});

// Simple parameter array
$invoker->call(function ($name) {
    echo 'Hello ' . $name;
}, ['John']);

// Named parameters
$invoker->call(function ($name) {
    echo 'Hello ' . $name;
}, [
    'name' => 'John'
]);

// Use the default value
$invoker->call(function ($name = 'world') {
    echo 'Hello ' . $name;
});

// Invoke any PHP callable
$invoker->call(['MyClass', 'myStaticMethod']);

// Using Class::method syntax
$invoker->call('MyClass::myStaticMethod');

    $invoker->call(function (Psr\Logger\LoggerInterface $logger) {
        // ...
    });
    

    $invoker->call(function ($twig) {
        // ...
    });
    

// $container must be an instance of Psr\Container\ContainerInterface
$container = ...

$containerResolver = new TypeHintContainerResolver($container);
// or
$containerResolver = new ParameterNameContainerResolver($container);

$invoker = new Invoker\Invoker;
// Register it before all the other parameter resolvers
$invoker->getParameterResolver()->prependResolver($containerResolver);

class MyHandler
{
    public function __invoke()
    {
        // ...
    }
}

// By default this doesn't work: an instance of the class should be provided
$invoker->call('MyHandler');

// If we set up the container to use
$invoker = new Invoker\Invoker(null, $container);
// Now 'MyHandler' is resolved using the container!
$invoker->call('MyHandler');

class WelcomeController
{
    public function home()
    {
        // ...
    }
}

// By default this doesn't work: home() is not a static method
$invoker->call(['WelcomeController', 'home']);

// If we set up the container to use
$invoker = new Invoker\Invoker(null, $container);
// Now 'WelcomeController' is resolved using the container!
$invoker->call(['WelcomeController', 'home']);
// Alternatively we can use the Class::method syntax
$invoker->call('WelcomeController::home');
js
angular.controller('MyController', ['dep1', 'dep2', function(dep1, dep2) {
    // ...
}]);