I'm not much familiar with Laravel Service provider and I have a question about it.
Example: I have three classes SystemProfiler, SurveyProfiler and OfferProfiler which implements ProfilerInterface. And also I have ProfilerService class which inject ProfilerInterface in the constructor. I need to create different ProfilerService services with injection of each of that profilers.
ProfilerService:
class ProfilerService {
$this->profiler;
function __construct(ProfilerInterface $profiler) {
$this->profiler = profiler;
}
}
I know how to do that in symfony2 framework:
system_profiler:
class: App\MyBundle\Profiles\SystemProfiler
survey_profiler:
class: App\MyBundle\Profiles\SurveyProfiler
offer_profiler:
class: App\MyBundle\Profiles\OfferProfiler
system_profile_service:
class: App\MyBundle\Services\ProfilerService
arguments:
- system_profiler
survey_profile_service:
class: App\MyBundle\Services\ProfilerService
arguments:
- survey_profiler
offer_profile_service:
class: App\MyBundle\Services\ProfilerService
arguments:
- offer_profiler
and then just call $this->container->get() with alias of ProfilerService realization
But Laravel documentation said that "there is no need to bind classes into the container if they do not depend on any interfaces.". And ProfilerService not depend on interface. So I can bind each profiler to interface like so:
$this->app->bind('App\MyBundle\Contracts\ProfilerInterface','App\MyBundle\Profiles\SystemProfiler');
or
$this->app->bind('App\MyBundle\Contracts\ProfilerInterface','App\MyBundle\Profiles\SurveyProfiler');
or
$this->app->bind('App\MyBundle\Contracts\ProfilerInterface','App\MyBundle\Profiles\OfferProfiler');
but how I should bind which of the Profilers should be injected to the ProfilerService and when???
I would appreciate any help and explanations
Here it goes (read the docs):
// ServiceProvider
public function register()
{
// Simple binding
$this->app->bind('some_service.one', \App\ImplOne::class);
$this->app->bind('some_service.two', \App\ImplTwo::class);
// Aliasing interface - container will inject some_service.one
// whenever interface is required...
$this->app->alias('some_service.one', \App\SomeInterface::class);
// ...except for the Contextual Binding:
$this->app->when(\App\DependantTwo::class)
->needs(\App\SomeInterface::class)
->give('some_service.two');
}
USAGE:
$ php artisan tinker
// Aliases
>>> app('some_service.one')
=> App\ImplOne {#669}
>>> app('some_service.two')
=> App\ImplTwo {#671}
// Aliased interface
>>> app('App\SomeInterface')
=> App\ImplOne {#677}
>>> app('App\DependantOne')->dependency
=> App\ImplOne {#677}
// Contextual
>>> app('App\DependantTwo')->dependency
=> App\ImplOne {#676}
Given this setup:
namespace App;
class ImplOne implements SomeInterface {}
class ImplTwo implements SomeInterface {}
class DependantOne
{
public function __construct(SomeInterface $dependency)
{
$this->dependency = $dependency;
}
}
class DependantTwo
{
public function __construct(SomeInterface $dependency)
{
$this->dependency = $dependency;
}
}
The constructor of your ProfilerService typehints an interface, which means that your ProfilerService does depend on an interface.
Without any additional setup, if you attempted to App::make('App\MyBundle\Services\ProfilerService');, you would get an error because Laravel wouldn't know how to resolve the interface dependency.
When you then do $this->app->bind('App\MyBundle\Contracts\ProfilerInterface','App\MyBundle\Profiles\SystemProfiler'); in your service provider, you're telling Laravel "whenever you need to resolve a ProfilerInterface, create a new SystemProfiler".
With that binding setup, if you then attempted to App::make('App\MyBundle\Services\ProfilerService');, Laravel would create a new ProfilerService instance, and inject a new SystemProfiler instance in the constructor.
However, this isn't exactly what you want, since you have three different implementations of the ProfilerInterface. You don't want Laravel always injecting just one. In this case, you would create custom bindings, similar to what you've done in Symfony.
In your service provide, your bindings would look something like this:
$this->app->bind('system_profile_service', function($app) {
return $app->make('App\MyBundle\Services\ProfilerService', [$app->make('App\MyBundle\Profiles\SystemProfiler')]);
});
$this->app->bind('survey_profile_service', function($app) {
return $app->make('App\MyBundle\Services\ProfilerService', [$app->make('App\MyBundle\Profiles\SurveyProfiler')]);
});
$this->app->bind('offer_profile_service', function($app) {
return $app->make('App\MyBundle\Services\ProfilerService', [$app->make('App\MyBundle\Profiles\OfferProfiler')]);
});
Now, with those bindings setup, you resolve your custom bindings from the IOC whenever you need one.
$systemProfiler = App::make('system_profiler_service');
$surveyProfiler = App::make('survey_profile_service');
$offerProfiler = App::make('offer_profile_service');
Related
I have a factory class that creates different objects and I have a lot of loggers defined for each type of object.
And I want to get all loggers collection in my factory. Before I just used a ContainerInterface in my factory constructor, but since Symfony 5.1 container autowiring is deprecated.
Now I can not find a way to get a collection of loggers. I tried to use
!tagged_iterator { tag: 'monolog.logger' }
and also tried to set a tag for LoggerInterface and get a tagged_iterator for it, but it didn't work. I suggest that it is because loggers are not real classes.
This might be overkill but as you say the logger services are a bit unusual. Seems like they should be tagged or be taggable by LoggerInterface but I ran some test and could not get it to work. Here is a brute force approach which relies on logger services having ids of monolog.logger.name:
# Start with a service locator class
namespace App\Service;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ServiceLocator;
class LoggerLocator extends ServiceLocator
{
// Just to specify the return type to keep IDEs happy
public function get($id) : LoggerInterface
{
return parent::get($id);
}
}
...
# now make the kernel into a compiler pass
# src/Kernel.php
class Kernel extends BaseKernel implements CompilerPassInterface
...
public function process(ContainerBuilder $container)
{
$loggerServices = [];
foreach($container->getServiceIds() as $id) {
if (!strncmp($id,'monolog.logger.',15)) {
//echo 'Logger ' . $id . "\n";
$loggerServices[$id] = new Reference($id);
}
}
$loggerLocator = $container->getDefinition(LoggerLocator::class);
$loggerLocator->setArguments([$loggerServices]);
}
# and now we can inject the locator where it is needed
class SomeController {
public function index(Request $request, LoggerLocator $loggerLocator)
{
dump($loggerLocator);
$logger = $loggerLocator->get('monolog.logger.' . $name);
Seems like there should be a way to do this just through configuring but a pass is easy enough I guess.
I am struggling to get a specific service via class name from group of injected tagged services.
Here is an example:
I tag all the services that implement DriverInterface as app.driver and bind it to the $drivers variable.
In some other service I need to get all those drivers that are tagged app.driver and instantiate and use only few of them. But what drivers will be needed is dynamic.
services.yml
_defaults:
autowire: true
autoconfigure: true
public: false
bind:
$drivers: [!tagged app.driver]
_instanceof:
DriverInterface:
tags: ['app.driver']
Some other service:
/**
* #var iterable
*/
private $drivers;
/**
* #param iterable $drivers
*/
public function __construct(iterable $drivers)
{
$this->drivers = $drivers;
}
public function getDriverByClassName(string $className): DriverInterface
{
????????
}
So services that implements DriverInterface are injected to $this->drivers param as iterable result. I can only foreach through them, but then all services will be instantiated.
Is there some other way to inject those services to get a specific service via class name from them without instantiating others?
I know there is a possibility to make those drivers public and use container instead, but I would like to avoid injecting container into services if it's possible to do it some other way.
You no longer (since Symfony 4) need to create a compiler pass to configure a service locator.
It's possible to do everything through configuration and let Symfony perform the "magic".
You can make do with the following additions to your configuration:
services:
_instanceof:
DriverInterface:
tags: ['app.driver']
lazy: true
DriverConsumer:
arguments:
- !tagged_locator
tag: 'app.driver'
The service that needs to access these instead of receiving an iterable, receives the ServiceLocatorInterface:
class DriverConsumer
{
private $drivers;
public function __construct(ServiceLocatorInterface $locator)
{
$this->locator = $locator;
}
public function foo() {
$driver = $this->locator->get(Driver::class);
// where Driver is a concrete implementation of DriverInterface
}
}
And that's it. You do not need anything else, it just workstm.
Complete example
A full example with all the classes involved.
We have:
FooInterface:
interface FooInterface
{
public function whoAmI(): string;
}
AbstractFoo
To ease implementation, an abstract class which we'll extend in our concrete services:
abstract class AbstractFoo implements FooInterface
{
public function whoAmI(): string {
return get_class($this);
}
}
Services implementations
A couple of services that implement FooInterface
class FooOneService extends AbstractFoo { }
class FooTwoService extends AbstractFoo { }
Services' consumer
And another service that requires a service locator to use these two we just defined:
class Bar
{
/**
* #var \Symfony\Component\DependencyInjection\ServiceLocator
*/
private $service_locator;
public function __construct(ServiceLocator $service_locator) {
$this->service_locator = $service_locator;
}
public function handle(): string {
/** #var \App\Test\FooInterface $service */
$service = $this->service_locator->get(FooOneService::class);
return $service->whoAmI();
}
}
Configuration
The only configuration needed would be this:
services:
_instanceof:
App\Test\FooInterface:
tags: ['test_foo_tag']
lazy: true
App\Test\Bar:
arguments:
- !tagged_locator
tag: 'test_foo_tag'
Alternative to FQCN for service names
If instead of using the class name you want to define your own service names, you can use a static method to define the service name. The configuration would change to:
App\Test\Bar:
arguments:
- !tagged_locator
tag: 'test_foo_tag'
default_index_method: 'fooIndex'
where fooIndex is a public static method defined on each of the services that returns a string. Caution: if you use this method, you won't be able to get the services by their class names.
A ServiceLocator will allow accessing a service by name without instantiating the rest of them. It does take a compiler pass but it's not too hard to setup.
use Symfony\Component\DependencyInjection\ServiceLocator;
class DriverLocator extends ServiceLocator
{
// Leave empty
}
# Some Service
public function __construct(DriverLocator $driverLocator)
{
$this->driverLocator = $driverLocator;
}
public function getDriverByClassName(string $className): DriverInterface
{
return $this->driverLocator->get($fullyQualifiedClassName);
}
Now comes the magic:
# src/Kernel.php
# Make your kernel a compiler pass
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class Kernel extends BaseKernel implements CompilerPassInterface {
...
# Dynamically add all drivers to the locator using a compiler pass
public function process(ContainerBuilder $container)
{
$driverIds = [];
foreach ($container->findTaggedServiceIds('app.driver') as $id => $tags) {
$driverIds[$id] = new Reference($id);
}
$driverLocator = $container->getDefinition(DriverLocator::class);
$driverLocator->setArguments([$driverIds]);
}
And presto. It should work assuming you fix any syntax errors or typos I may have introduced.
And for extra credit, you can auto register your driver classes and get rid of that instanceof entry in your services file.
# Kernel.php
protected function build(ContainerBuilder $container)
{
$container->registerForAutoconfiguration(DriverInterface::class)
->addTag('app.driver');
}
Hi I'm getting an error of should be an instance of interface
App\Repositories\Traits\PhotoService::__construct() must be an instance of AwsServiceInterface, instance
Here's what I have so far
namespace App\Repositories\Interfaces;
interface AwsServiceInterface
{
//...
}
Now I have this class
namespace App\Repositories\Interfaces;
use App\Repositories\Interfaces\AwsServiceInterface;
class CloudFrontService implements AwsServiceInterface
{
public function __construct()
{
}
}
Now I'm using a dependency injection on this class
namespace App\Repositories\Traits;
use App\Repositories\Interfaces\AwsServiceInterface;
class PhotoService
{
protected $service;
public function __construct(AwsServiceInterface $service)
{
$this->service = $service;
}
public function getAuthParams($resource, $search_key = '')
{
// Execute a function from a class that implements AwsServiceInterface
}
And I'm calling the PhotoService class like this
$photo_service = new PhotoService(new CloudFrontService());
echo $photo_service->getAuthParams($resource);
But somehow I'm getting this error
FatalThrowableError: Type error: Argument 1 passed to App\Repositories\Traits\PhotoService::__construct() must be an instance of AwsServiceInterface, instance of App\Repositories\Interfaces\CloudFrontService given
In your App\Providers\AppServiceProvider class add following code in register() method:
$this->app->bind(
'App\Repositories\Interfaces\AwsServiceInterface',
'App\Repositories\Interfaces\CloudFrontService'
);
and then you can use it as:
$photo_service = app(PhotoService::class);
echo $photo_service->getAuthParams($resource);
You are having a problem with namespacing. The typehint that you are using isn't complete for what you are looking for.
Just guessing but I think that you want to change the typehint to be:
public function __construct(App\Repositories\Interfaces\AwsServiceInterface $service)
http://php.net/manual/en/language.namespaces.basics.php
You need to bind implementation to that interface. Here's an example.
Laravel itself does not know what implementation you want to use for this interface, you need to specify that yourself.
I solved my problem. For those of you having the same issue make sure you do this step.
1. as mentioned by #Amit double check on the service provider that the service is bind under register function
2. Make sure to do
php artisan clear-compile
php artisan cache:clear
php artisan config:clear
Say I have an interface CrawlerInterface with implementation PageCrawler and FeedCrawler; if we happen to need both classes in a controller, how can that be achieved with constructor injection?
Previously we use a central ServiceProvider to register (i.e. App::bind) such classes, but in most cases we only have 1 implementation of an interface, so said problem hasn't occured to us yet.
PS: I also wonder if this problem suggests we should split the controller.
Updates:
Thanks for the comments and response, to explain, said interface has only one public method: crawl($uri), and both page/feed crawler implements it as given a resource identifier, return resource.
My follow up question:
Say we are in a calculator scenario where Addition, Subtraction and Multiplication share the same interface Operation, which has only 1 public method run, at some point we will still encounter this problem right? How do we handle situation like these in general with ServiceProvider?
If each crawler exists for a different reason, you can use arbitrary names for your instances, for example:
App::bind('crawler.allArticles', 'PageCrawler');
App::bind('crawler.latestArticles', 'FeedCrawler');
For the controller:
App::bind('CrawlerController', function($app) {
return new CrawlerController(
App::make('crawler.allArticles'),
App::make('crawler.latestArticles')
);
});
Your controller code would then use each crawler differently:
public function showLatestArticlesAction()
$latestArticles = $this->latestArticlesCrawler->crawl();
// ...
}
public function showAllArticlesAction()
$allArticles = $this->allArticlesCrawler->crawl();
// ...
}
If you just have a list of crawlers where each is used for the same thing, you probably want to do something like:
App::bind('crawlers', function($app) {
return [
App::make('PageCrawler'),
App::make('FeedCrawler'),
];
});
In your controller, you'll get a list of "crawlers" by configuring it like so:
App::bind('CrawlerController', function($app) {
return new CrawlerController(App::make('crawlers'));
});
Your controller code could be something like this:
public function showArticlesAction()
$allArticles = array();
foreach ($this->crawlers as $crawler) {
$allArticles = array_merge($allArticles, $this->crawler->crawl());
}
// ...
}
Ok lets assume you have a CrawlerController
class CrawlerController extends BaseController
{
protected $crawler1;
protected $crawler2;
public function __construct(CrawlerInterface $c1, CrawlerInterface $c2)
{
$this->crawler1 = $c1;
$this->crawler2 = $c2;
}
}
an interface
interface CrawlerInterface{}
and concrete implementations of that intefrace called PageCrawler and FeedCrawler
class PageCrawler implements CrawlerInterface{}
class FeedCrawler implements CrawlerInterface{}
You would inject the dependencies by writing a service locator like
App::bind('CrawlerController', function($app) {
$controller = new CrawlerController(
new PageCrawler,
new FeedCrawler
);
return $controller;
});
But as suggested by others you should rethink your logic, use it only if this kind
of architecture is unavoidable
I think that the interface won't help you in this case.
By doing:
App::bind('CrawlerInterface', '<implementation>');
You need to choose one:
App::bind('CrawlerInterface', 'PageCrawler');
or
App::bind('CrawlerInterface', 'FeedCrawler');
And then Laravel will inject it:
class CrawlerController {
public function __construct(CrawlerInterface $crawler)
{
}
}
To have both you have 2 options
-Have 2 different interfaces
-Inject the implementations directly:
class CrawlerController {
public function __construct(PageCrawler $pageCrawler, FeedCrawler $feedCrawler)
{
}
}
But I also think that, if you need something like this, you better rethink your logic.
I'm trying to emulate the behavior of Route Symfony annotation(documentation), which extends Symfony\Component\Routing\Annotation\Route adding the service property:
class Route extends BaseRoute
{
protected $service;
public function setService($service)
{
$this->service = $service;
}
// ...
}
It adds the service property in order to set the _controller parameter to servicename:method when controller is actually a service. This is done in the AnnotatedRouteControllerLoader class:
protected function configureRoute(Route $route, \ReflectionClass $class,
\ReflectionMethod $method, $annot)
{
// ...
if ($classAnnot && $service = $classAnnot->getService()) {
$route->setDefault('_controller', $service.':'.$method->getName());
} else {
// Not a service ...
}
// ...
}
My question is how/when the setService($service) is invoked?
I've tried to define my custom MyCustomRoute annotation (with the above service property), loop each container service and call setService($serviceId) to "notify" that the controller is actually a service:
foreach ($container->getServiceIds() as $serviceId) {
if ($container->hasDefinition($serviceId)) {
$definition = $container->getDefinition($serviceId);
$reflector = new \ReflectionClass($definition->getClass());
// If the service is a controller then flag it for the
// AnnotatedRouteControllerLoader
if ($annot = $reader->getClassAnnotation($reflector,
'My\CustomAnnotations\MyCustomRoute')) {
$annot->setServiceName($serviceId);
}
}
}
Here $container is Symfony service container, $reader is doctrine annotation reader.
This is not working because annotation is read again in AnnotatedRouteControllerLoader resulting in a different instance, loosing the service property.
I'm using the routing component alone (without the entire Symfony framework).
The Route class is declared as a service, refering to the doc, you can inject dependencies by controller, but also with "setter injection". take a look here:
http://symfony.com/doc/current/book/service_container.html#optional-dependencies-setter-injection
So you can declare your service as:
my_custom.router:
class: "Acme\MyBundle\MyServices\MyRouter"
calls:
- [setService, ["#service_key"]]