- Phalcon\Di
- Phalcon\Di\AbstractInjectionAware
- Phalcon\Di\DiInterface
- Phalcon\Di\Exception
- Phalcon\Di\Exception\ServiceResolutionException
- Phalcon\Di\FactoryDefault
- Phalcon\Di\FactoryDefault\Cli
- Phalcon\Di\Injectable
- Phalcon\Di\InjectionAwareInterface
- Phalcon\Di\Service
- Phalcon\Di\Service\Builder
- Phalcon\Di\ServiceInterface
- Phalcon\Di\ServiceProviderInterface
Class Phalcon\Di
| Namespace | Phalcon | | Uses | Phalcon\Di\Service, Phalcon\Di\DiInterface, Phalcon\Di\Exception, Phalcon\Di\Exception\ServiceResolutionException, Phalcon\Config\Adapter\Php, Phalcon\Config\Adapter\Yaml, Phalcon\Config\ConfigInterface, Phalcon\Di\ServiceInterface, Phalcon\Events\ManagerInterface, Phalcon\Di\InjectionAwareInterface, Phalcon\Di\ServiceProviderInterface | | Implements | DiInterface |
Phalcon\Di is a component that implements Dependency Injection/Service Location of services and it's itself a container for them.
Since Phalcon is highly decoupled, Phalcon\Di is essential to integrate the different components of the framework. The developer can also use this component to inject dependencies and manage global instances of the different classes used in the application.
Basically, this component implements the Inversion of Control
pattern. Applying this, the objects do not receive their dependencies using setters or constructors, but requesting a service dependency injector. This reduces the overall complexity, since there is only one way to get the required dependencies within a component.
Additionally, this pattern increases testability in the code, thus making it less prone to errors.
use Phalcon\Di;
use Phalcon\Http\Request;
$di = new Di();
// Using a string definition
$di->set("request", Request::class, true);
// Using an anonymous function
$di->setShared(
"request",
function () {
return new Request();
}
);
$request = $di->getRequest();
Properties¶
/**
* List of registered services
*
* @var ServiceInterface[]
*/
protected services;
/**
* List of shared instances
*/
protected sharedInstances;
/**
* Events Manager
*
* @var ManagerInterface | null
*/
protected eventsManager;
/**
* Latest DI build
*
* @var DiInterface | null
*/
protected static _default;
Methods¶
Magic method to get or set services using setters/getters Phalcon\Di constructorpublic function attempt( string $name, mixed $definition, bool $shared = bool ): ServiceInterface | bool;
And the services can be specified in the file as:
return [
'myComponent' => [
'className' => '\Acme\Components\MyComponent',
'shared' => true,
],
'group' => [
'className' => '\Acme\Group',
'arguments' => [
[
'type' => 'service',
'service' => 'myComponent',
],
],
],
'user' => [
'className' => '\Acme\User',
],
];
@link https://docs.phalcon.io/en/latest/reference/di.html
Loads services from a yaml file.$di->loadFromYaml(
"path/services.yaml",
[
"!approot" => function ($value) {
return dirname(__DIR__) . $value;
}
]
);
And the services can be specified in the file as:
myComponent:
className: \Acme\Components\MyComponent
shared: true
group:
className: \Acme\Group
arguments:
- type: service
name: myComponent
user:
className: \Acme\User
@link https://docs.phalcon.io/en/latest/reference/di.html
Check if a service is registered using the array syntax Allows to obtain a shared service using the array syntax Allows to register a shared service using the array syntax Removes a service from the services container using the array syntax Registers a service provider.use Phalcon\Di\DiInterface;
use Phalcon\Di\ServiceProviderInterface;
class SomeServiceProvider implements ServiceProviderInterface
{
public function register(DiInterface $di)
{
$di->setShared(
'service',
function () {
// ...
}
);
}
}
Abstract Class Phalcon\Di\AbstractInjectionAware
| Namespace | Phalcon\Di | | Implements | InjectionAwareInterface |
This abstract class offers common access to the DI in a class
Properties¶
Methods¶
Returns the internal dependency injector Sets the dependency injectorInterface Phalcon\Di\DiInterface
| Namespace | Phalcon\Di | | Uses | ArrayAccess | | Extends | ArrayAccess |
Interface for Phalcon\Di
Methods¶
public function attempt( string $name, mixed $definition, bool $shared = bool ): ServiceInterface | bool;
Class Phalcon\Di\Exception
| Namespace | Phalcon\Di | | Extends | \Phalcon\Exception |
Exceptions thrown in Phalcon\Di will use this class
Class Phalcon\Di\Exception\ServiceResolutionException
| Namespace | Phalcon\Di\Exception | | Extends | \Phalcon\Di\Exception |
Phalcon\Di\Exception\ServiceResolutionException
Class Phalcon\Di\FactoryDefault
| Namespace | Phalcon\Di | | Uses | Phalcon\Filter\FilterFactory | | Extends | \Phalcon\Di |
This is a variant of the standard Phalcon\Di. By default it automatically registers all the services provided by the framework. Thanks to this, the developer does not need to register each service individually providing a full stack framework
Methods¶
Phalcon\Di\FactoryDefault constructorClass Phalcon\Di\FactoryDefault\Cli
| Namespace | Phalcon\Di\FactoryDefault | | Uses | Phalcon\Di\FactoryDefault, Phalcon\Di\Service, Phalcon\Filter\FilterFactory | | Extends | FactoryDefault |
Phalcon\Di\FactoryDefault\Cli
This is a variant of the standard Phalcon\Di. By default it automatically registers all the services provided by the framework. Thanks to this, the developer does not need to register each service individually. This class is specially suitable for CLI applications
Methods¶
Phalcon\Di\FactoryDefault\Cli constructorAbstract Class Phalcon\Di\Injectable
| Namespace | Phalcon\Di | | Uses | Phalcon\Di, Phalcon\Session\BagInterface | | Implements | InjectionAwareInterface |
This class allows to access services in the services container by just only accessing a public property with the same name of a registered service
@property \Phalcon\Mvc\Dispatcher|\Phalcon\Mvc\DispatcherInterface $dispatcher @property \Phalcon\Mvc\Router|\Phalcon\Mvc\RouterInterface $router @property \Phalcon\Url|\Phalcon\Url\UrlInterface $url @property \Phalcon\Http\Request|\Phalcon\Http\RequestInterface $request @property \Phalcon\Http\Response|\Phalcon\Http\ResponseInterface $response @property \Phalcon\Http\Response\Cookies|\Phalcon\Http\Response\CookiesInterface $cookies @property \Phalcon\Filter $filter @property \Phalcon\Flash\Direct $flash @property \Phalcon\Flash\Session $flashSession @property \Phalcon\Session\ManagerInterface $session @property \Phalcon\Events\Manager|\Phalcon\Events\ManagerInterface $eventsManager @property \Phalcon\Db\Adapter\AdapterInterface $db @property \Phalcon\Security $security @property \Phalcon\Crypt|\Phalcon\CryptInterface $crypt @property \Phalcon\Tag $tag @property \Phalcon\Escaper|\Phalcon\Escaper\EscaperInterface $escaper @property \Phalcon\Annotations\Adapter\Memory|\Phalcon\Annotations\Adapter $annotations @property \Phalcon\Mvc\Model\Manager|\Phalcon\Mvc\Model\ManagerInterface $modelsManager @property \Phalcon\Mvc\Model\MetaData\Memory|\Phalcon\Mvc\Model\MetadataInterface $modelsMetadata @property \Phalcon\Mvc\Model\Transaction\Manager|\Phalcon\Mvc\Model\Transaction\ManagerInterface $transactionManager @property \Phalcon\Assets\Manager $assets @property \Phalcon\Di|\Phalcon\Di\DiInterface $di @property \Phalcon\Session\Bag|\Phalcon\Session\BagInterface $persistent @property \Phalcon\Mvc\View|\Phalcon\Mvc\ViewInterface $view
Properties¶
Methods¶
Magic method __get Magic method __isset Returns the internal dependency injector Sets the dependency injectorInterface Phalcon\Di\InjectionAwareInterface
| Namespace | Phalcon\Di |
This interface must be implemented in those classes that uses internally the Phalcon\Di that creates them
Methods¶
Returns the internal dependency injector Sets the dependency injectorClass Phalcon\Di\Service
| Namespace | Phalcon\Di | | Uses | Closure, Phalcon\Di\Exception\ServiceResolutionException, Phalcon\Di\Service\Builder | | Implements | ServiceInterface |
Represents individually a service in the services container
$service = new \Phalcon\Di\Service(
"request",
\Phalcon\Http\Request::class
);
$request = service->resolve();
Properties¶
//
protected definition;
/**
* @var bool
*/
protected resolved = false;
/**
* @var bool
*/
protected shared = false;
//
protected sharedInstance;
Methods¶
Phalcon\Di\Service Returns the service definition Returns a parameter in a specific position Returns true if the service was resolved Check whether the service is shared or not Resolves the service Set the service definition Changes a parameter in the definition without resolve the service Sets if the service is shared or not Sets/Resets the shared instance related to the serviceClass Phalcon\Di\Service\Builder
| Namespace | Phalcon\Di\Service | | Uses | Phalcon\Di\DiInterface, Phalcon\Di\Exception |
Phalcon\Di\Service\Builder
This class builds instances based on complex definitions
Methods¶
Builds a service using a complex service definitionInterface Phalcon\Di\ServiceInterface
| Namespace | Phalcon\Di |
Represents a service in the services container
Methods¶
Returns the service definition Returns a parameter in a specific position Returns true if the service was resolved Check whether the service is shared or not Resolves the service Set the service definition Changes a parameter in the definition without resolve the service Sets if the service is shared or notInterface Phalcon\Di\ServiceProviderInterface
| Namespace | Phalcon\Di |
Should be implemented by service providers, or such components, which register a service in the service container.
namespace Acme;
use Phalcon\Di\DiInterface;
use Phalcon\Di\ServiceProviderInterface;
class SomeServiceProvider implements ServiceProviderInterface
{
public function register(DiInterface $di)
{
$di->setShared(
'service',
function () {
// ...
}
);
}
}