PHPackages                             webiny/service-manager - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. webiny/service-manager

ActiveLibrary

webiny/service-manager
======================

Webiny Service Manager Component

v1.6.1(8y ago)17217MITPHPPHP ^7

Since Sep 20Pushed 8y ago9 watchersCompare

[ Source](https://github.com/Webiny/ServiceManager)[ Packagist](https://packagist.org/packages/webiny/service-manager)[ Docs](http://www.webiny.com/)[ RSS](/packages/webiny-service-manager/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (1)Dependencies (4)Versions (23)Used By (7)

Service Manager
===============

[](#service-manager)

Install the component
---------------------

[](#install-the-component)

The best way to install the component is using Composer.

```
composer require webiny/service-manager
```

For additional versions of the package, visit the [Packagist page](https://packagist.org/packages/webiny/service-manager).

Usage
-----

[](#usage)

Available service configuration parameters are:

- `Class`
- `Arguments` (`Object` &amp; `ObjectArguments`)
- `Abstract`
- `Calls`
- `Scope`

Extra parameters, for factory services, are:

- `Factory` - class or service
- `Static` (Optional) - defaults to TRUE, means that `Method` will be called statically on `Factory` object
- `Method` - a method to call on `Factory` object
- `MethodArguments` (Optional) - method arguments for `Method`

There are 2 possible types of scope:

- `container` (Default) - only 1 instance of service is created, and is re-used on each subsequent request of service
- `prototype` - a new instance of service is created each time a service is requested

Service definition
------------------

[](#service-definition)

To register a service you need to create a service config object (using `ConfigObject` class) and pass it to `ServiceManager`

You can either create your configuration files in YAML or use plain PHP and build arrays straight from your code. If using PHP - call `new ConfigObject($configArray)` to build a proper config object. If using YAML - call `$config = Config::getInstance()->yaml($pathToYourConfigFile);`

Basic service definition takes only `Class` parameter:

```
$config = [
    'Class' => '\My\Service\Class'
];

ServiceManager::getInstance()->registerService('MyService', new ConfigObject($config));

// Now get your service
$myService = ServiceManager::getInstance()->getService('MyService');
```

You can group your services in logic groups, by creating an array of service definitions and then registering them as a group:

```
$config = [
    'MyLogger' => [
        'Class' => '\My\Service\Class'
    ],
    'MyMailer' => [
        'Class' => '\My\Mailer\Class'
    ]
];

ServiceManager::getInstance()->registerServices('MyServiceGroup', new ConfigObject($config));

// Now get your specific service
$myMailer = ServiceManager::getInstance()->getService('MyServiceGroup.MyMailer');
```

Constructor arguments
---------------------

[](#constructor-arguments)

You can provide constructor arguments to your service class, by using `Arguments` parameter. Argument can be any value, including class name (it will be instantiated and passed to constructor as a PHP object) and a reference to another service (enter service reference using `@` character):

```
$config = [
    'Class' => '\My\Service\Class'
    'Arguments' => ['FirstArgument', [1,2,3], '\This\Class\Will\Be\Instantiated', '@someOtherService']
];
```

In case you need to provide constructor parameters to your argument class or service, you will need to use an extended arguments syntax (here is a config example written in YAML):

```
Class: \My\Service\Class
Arguments:
    name: FirstArgument
    ids: [1,2,3]
    some_instance:
        Object: \This\Class\Will\Be\Instantiated
        ObjectArguments: [Name, Y-m-d]
    some_service:
        Object: @some.other.service
        ObjectArguments: [Name]
```

Creating service from a YAML config file
----------------------------------------

[](#creating-service-from-a-yaml-config-file)

To create a `ConfigObject` out of your YAML file and register a new service, simply call:

```
$config = Config::getInstance()->yaml($pathToYourConfigFile);
ServiceManager::getInstance()->registerService('MyNewService', $config);
```

Service object method calls
---------------------------

[](#service-object-method-calls)

In case you need to call some methods on your service instance, you can specify them using `Calls` parameter:

```
Class: \My\Service\Class
Arguments: [FirstArgument, [1,2,3], \This\Class\Will\Be\Instantiated, @some.other.service]
Calls:
    - [yourMethod]
    - [yourMethodWithArguments, [Arg1, 123]]
    - [yourMethodWithClassArgument, [\Some\Class\That\Will\Be\Instantiated]]
    - [yourMethodWithServiceArgument, [@some_service]]
```

Abstract services and parameters
--------------------------------

[](#abstract-services-and-parameters)

Service manager also supports abstract services. When you have 2 or more services sharing similar functionality, you can extract common stuff into an abstract service. In the following example we also use `Parameters`. Parameters are like variables, define them once, and reuse whenever you need them.

Parameters config file:

```
# Definition of parameters
logger.class: \Webiny\Component\Logger\Logger
logger.driver.class: \Webiny\Component\Logger\Drivers\Webiny
logger.handler.udp.class: \Webiny\Component\Logger\Drivers\Webiny\Handlers\UDPHandler
```

> NOTE: At the moment, parameters can not be used in ObjectArguments.

Services config file (this can also be defined in plain PHP array):

```
UdpHandler:
    Class: %logger.handler.udp.class%
TrayLoggerAbstract:
    Abstract: true
    Class: %logger.class%
    Calls:
      - [addHandler, [@UdpHandler]]
WebinySystem:
    Parent: @TrayLoggerAbstract
    Arguments: [System, %logger.driver.class%]
WebinyEcommerce:
    Parent: @TrayLoggerAbstract
    Arguments: [Ecommerce, %logger.driver.class%]
```

Now we need to register parameters and services with `ServiceManager`:

```
// Registering multiple parameters at once
$parameters = Config::getInstance()->yaml($pathToYourParametersConfigFile);
ServiceManager::getInstance()->registerParameters($parameters);

// Registering one parameter
ServiceManager::getInstance()->registerParameter('someClassName', '\Webiny\Some\Class\Name');

// Registering your services
$servicesConfig = Config::getInstance()->yaml($pathToYourServicesConfigFile);
ServiceManager::getInstance()->registerServices('Logger', new ConfigObject($servicesConfig));
```

In this example we defined and abstract service `TrayLoggerAbstract` and 2 real loggers that extend the abstract service, `WebinySystem` and `WebinyEcommerce`. These 2 loggers share same class and method calls, but have different constructor arguments.

You can also specify arguments in abstract class and later override them in the real class. Also, you can add more method calls from child service:

```
# Services
UdpHandler:
    Class: %logger.handler.udp.class%
TrayLoggerAbstract:
    Abstract: true
    Class: %logger.class%
    Arguments: [Default, %logger.driver.class%]
    Calls:
      - [addHandler, [@UdpHandler]]
WebinySystem:
    Parent: @TrayLoggerAbstract
    Calls:
    - [setSomething, [someParameter]]
WebinyEcommerce:
    Parent: @TrayLoggerAbstract
    Arguments: [Ecommerce, %logger.driver.class%]
```

In this last example, `WebinySystem` service will be constructed using the arguments from parent service and will also add an extra method call. `WebinyEcommerce` will provide it's own arguments to the parent constructor and will inherit the parent's `Calls`.

If you need to replace a method in `Calls` parameter, specify the third argument in call definition with the index of method to replace. In the following example, child method `setSomething` will replace the parent method at index 0, which is `addHandler`:

```
UdpHandler:
    Class: %logger.handler.udp.class%
TrayLoggerAbstract:
    Abstract: true
    Class: %logger.class%
    Arguments: [Default, %logger.driver.class%]
    Calls:
      - [addHandler, [@UdpHandler]]
WebinySystem:
    Parent: @TrayLoggerAbstract
    Calls:
    - [setSomething, [someParameter], 0]
```

If you want to replace all of the parent `Calls`, put an exclamation mark in front of the `Calls` key, and make it look like this - `!Calls`:

```
UdpHandler:
    Class: %logger.handler.udp.class%
TrayLoggerAbstract:
    Abstract: true
    Class: %logger.class%
    Arguments: [Default, %logger.driver.class%]
    Calls:
      - [addHandler, [@UdpHandler]]
WebinySystem:
    Parent: @TrayLoggerAbstract
    !Calls:
    - [setSomething, [someParameter]]
```

In this case, child service `Calls` will completely replace parent `Calls`.

Accessing services from PHP
---------------------------

[](#accessing-services-from-php)

To use `ServiceManager` in your code, the easiest way is to simply use `ServiceManagerTrait`. This will give you access to `$this->service()`.

```
class YourClass{
    use ServiceManagerTrait;

    public function yourMethod(){
        $service = $this->service('YourServiceName');
    }
}
```

If you do need to access ServiceManager class directly, use it like this:

```
ServiceManager::getInstance()->getService('YourServiceName')
```

Accessing services by tags
--------------------------

[](#accessing-services-by-tags)

You can group services by using tags and load all of related services using single call. To achieve that, you need to add `tags` key to your service configuration:

```
WebinySystem:
    Parent: @TrayLoggerAbstract
    !Calls:
    - [setSomething, [someParameter]]
    Tags: [logger]
WebinyCustom:
    Parent: @TrayLoggerAbstract
    Tags: [logger, custom_logger]
```

Now execute the following piece of code. The result will be an array containing two services: `WebinySystem` and `WebinyCustom`:

```
class YourClass{
    use ServiceManagerTrait;

    public function yourMethod(){
        $services = $this->servicesByTag('logger');
    }
}
```

You can also tell `ServiceManager` to filter the services using a given interface or a class name. It fill first fetch all services containing the requested tag and then filter them using the given class or interface name, before returning the final result set to you. This way you are sure you only get what you need and don't have to make checks yourself, resulting in a cleaner code:

```
class YourClass{
    use ServiceManagerTrait;

    public function yourMethod(){
        $services = $this->servicesByTag('cms_plugin', '\Your\Expected\Class\Or\Interface');
    }
}
```

Resources
---------

[](#resources)

To run unit tests, you need to use the following command:

```
$ cd path/to/Webiny/Component/ServiceManager/
$ composer.phar install
$ phpunit

```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~52 days

Recently: every ~5 days

Total

22

Last Release

3153d ago

PHP version history (3 changes)v1.1.0PHP &gt;=5.4.0

v1.2.1PHP &gt;=5.5.9

1.5.x-devPHP ^7

### Community

Maintainers

![](https://www.gravatar.com/avatar/4440afa738ed146b05c06073a90345e0464c4f4d042b039532d881ca24859d77?d=identicon)[SvenAlHamad](/maintainers/SvenAlHamad)

---

Top Contributors

[![SvenAlHamad](https://avatars.githubusercontent.com/u/3808420?v=4)](https://github.com/SvenAlHamad "SvenAlHamad (17 commits)")

---

Tags

dependency-injectionservice-manager

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/webiny-service-manager/health.svg)

```
[![Health](https://phpackages.com/badges/webiny-service-manager/health.svg)](https://phpackages.com/packages/webiny-service-manager)
```

###  Alternatives

[pimple/pimple

Pimple, a simple Dependency Injection Container

2.7k130.5M1.4k](/packages/pimple-pimple)[php-di/php-di

The dependency injection container for humans

2.8k48.9M994](/packages/php-di-php-di)[laminas/laminas-servicemanager

Factory-Driven Dependency Injection Container

15955.1M694](/packages/laminas-laminas-servicemanager)[php-di/invoker

Generic and extensible callable invoker

26857.8M56](/packages/php-di-invoker)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
