PHP code example of improvframework / service-provisioning
1. Go to this page and download the library: Download improvframework/service-provisioning 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/ */
improvframework / service-provisioning example snippets
// File: container.php
// Implements \PSR\Container\ContainerInterface
// as well as \ArrayAccess (e.g. \Pimple\Container)
$container = new \Some\Container();
$container['routing.table'] = function (ContainerInterface $container) {
return new RoutingTable($container->get('config'));
}
$container['routing.router'] = function (ContainerInterface $container) {
return new Router($container->get('routing.table'));
};
$container['config'] = function () {
return new Configuration(new ConfigLoader('/config.yml'));
};
$container['application'] = function (ContainerInterface $container) {
return new Application($container->get('routing.router'));
};
$container['db'] = function (ContainerInterface $container) {
return new DatabaseFactory::create(Database::TYPE_PDO, $container->get('config'));
};
$container['repository.blog'] = function (ContainerInterface $container) {
return new BlogRepository($container->get('db'));
};
$container['repository.user'] = function (ContainerInterface $container) {
return new UserRepository($container->get('db'));
};
$container['service.blog'] = function (ContainerInterface $container) {
return new BlogService($container->get('repository.blog'))
};
$container['service.user'] = function (ContainerInterface $container) {
return new UserService($container->get('repository.user'))
};
$container['controller.blog'] = function (ContainerInterface $container) {
return new BlogController($container->get('service.blog'));
};
$container['controller.user'] = function (ContainerInterface $container) {
return new UserController($container->get('service.user'));
};
// etc.
return $container;
// File public/index.php
$container = new ContainerInterface();
(new Custom\App\ServiceLoader())->loadServices($container);
$container->get('application')->run();
// Build a map of classes to search for and instantiate
$map = [
SomeServiceProvider::class,
AnotherServiceProvider::class,
// etc
];
// Create the loader, providing the map and a callable which
// will operate on each of the above classes in some way.
$service_loader = new ClassNameServiceLoader($map, function ($subject, ContainerInterface $container) {
$subject->registerServicesInto($container);
} );
// Invoke the loading action. This will iterate the classes
// from $map and apply the callback to each. After this call,
// services are available to be drawn via $container->get(...)
$service_loader->loadServices($container);
class CustomServiceInvoker
{
public function __invoke(CustomServiceInterface $subject, ContainerInterface $container)
{
$subject->registerServicesInto($container);
}
}
class UserModuleServiceProvider implements ServiceProviderInterface
{
public function register(ContainerInterface $container)
{
$container['repository.user'] = function (Container $container) {
return new UserRepository($container->get('db'));
};
$container['service.user'] = function (Container $container) {
return new UserService($container->get('repository.user'))
};
$container['controller.user'] = function (Container $container) {
return new UserController($container->get('service.user'));
};
}
}
// After this call, the User-related services are now
// available to be retrieved from the container.
(new UserModuleServiceProvider())->register($container);
// Build a map of service providers, each of which implement this
// package's \Improv\ServiceProvisioning\ServiceProviderInterface
$map = [
CoreServiceProvider::class,
PersistenceServiceProvider::class,
UserModuleServiceProvider::class,
BlogModuleServiceProvider::class,
// etc
];
// Instantiate the loader with the map and this package's invoker
$service_loader = new ClassNameServiceLoader($map, new ServiceProviderInvoker() );
$service_loader->loadServices($container);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.