Phalcon di
NOTE
All classes are prefixed with Phalcon
Di\AbstractInjectionAware ¶
-
Namespace
Phalcon\Di
-
Uses
stdClass
-
Extends
stdClass
-
Implements
InjectionAwareInterface
This abstract class offers common access to the DI in a class
Properties¶
Methods¶
Returns the internal dependency injector Sets the dependency injectorDi\Di¶
-
Namespace
Phalcon\Di
-
Uses
Phalcon\Config\Adapter\Php
Phalcon\Config\Adapter\Yaml
Phalcon\Config\ConfigInterface
Phalcon\Di\DiInterface
Phalcon\Di\Exception
Phalcon\Di\Exception\ServiceResolutionException
Phalcon\Di\InitializationAwareInterface
Phalcon\Di\InjectionAwareInterface
Phalcon\Di\Service
Phalcon\Di\ServiceInterface
Phalcon\Di\ServiceProviderInterface
Phalcon\Events\ManagerInterface
-
Extends
-
Implements
DiInterface
Phalcon\Di\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\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\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
*
* @var array
*/
protected $sharedInstances;
/**
* Events Manager
*
* @var ManagerInterface|null
*/
protected $eventsManager;
/**
* Latest DI build
*
* @var DiInterface|null
*/
protected static $defaultDi;
Methods¶
Magic method to get or set services using setters/getters Phalcon\Di\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/latest/di/
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/latest/di/
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 () {
// ...
}
);
}
}
Di\DiInterface ¶
-
Namespace
Phalcon\Di
-
Uses
ArrayAccess
-
Extends
ArrayAccess
-
Implements
Interface for Phalcon\Di\Di
Methods¶
public function attempt( string $name, mixed $definition, bool $shared = bool ): ServiceInterface | bool;
Di\Exception¶
-
Namespace
Phalcon\Di
-
Uses
-
Extends
\Exception
-
Implements
Exceptions thrown in Phalcon\Di will use this class
Di\Exception\ServiceResolutionException¶
-
Namespace
Phalcon\Di\Exception
-
Uses
-
Extends
\Phalcon\Di\Exception
-
Implements
Phalcon\Di\Exception\ServiceResolutionException
Di\FactoryDefault¶
-
Namespace
Phalcon\Di
-
Uses
Phalcon\Filter\FilterFactory
-
Extends
\Phalcon\Di\Di
-
Implements
This is a variant of the standard Phalcon\Di\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 constructorDi\FactoryDefault\Cli¶
-
Namespace
Phalcon\Di\FactoryDefault
-
Uses
Phalcon\Di\FactoryDefault
Phalcon\Di\Service
Phalcon\Filter\FilterFactory
-
Extends
FactoryDefault
-
Implements
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 constructorDi\InitializationAwareInterface ¶
-
Namespace
Phalcon\Di
-
Uses
-
Extends
-
Implements
Interface for components that have initialize()
Methods¶
Di\Injectable ¶
-
Namespace
Phalcon\Di
-
Uses
Phalcon\Di\Di
Phalcon\Session\BagInterface
stdClass
-
Extends
stdClass
-
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\Mvc\Url|\Phalcon\Mvc\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 $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\Encryption\Security $security @property \Phalcon\Encryption\Crypt|\Phalcon\Encryption\Crypt\CryptInterface $crypt @property \Phalcon\Html\TagFactory $tag @property \Phalcon\Html\Escaper|\Phalcon\Html\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\Di|\Phalcon\Di\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 injectorDi\InjectionAwareInterface ¶
-
Namespace
Phalcon\Di
-
Uses
-
Extends
-
Implements
This interface must be implemented in those classes that uses internally the Phalcon\Di\Di that creates them
Methods¶
Returns the internal dependency injector Sets the dependency injectorDi\Service¶
-
Namespace
Phalcon\Di
-
Uses
Closure
Phalcon\Di\Exception\ServiceResolutionException
Phalcon\Di\Service\Builder
-
Extends
-
Implements
ServiceInterface
Represents individually a service in the services container
$service = new \Phalcon\Di\Service(
"request",
\Phalcon\Http\Request::class
);
$request = service->resolve();
Properties¶
/**
* @var mixed
*/
protected $definition;
/**
* @var bool
*/
protected $resolved = false;
/**
* @var bool
*/
protected $shared = false;
/**
* @var mixed|null
*/
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 serviceDi\Service\Builder¶
-
Namespace
Phalcon\Di\Service
-
Uses
Phalcon\Di\DiInterface
Phalcon\Di\Exception
-
Extends
-
Implements
Phalcon\Di\Service\Builder
This class builds instances based on complex definitions
Methods¶
Builds a service using a complex service definitionDi\ServiceInterface ¶
-
Namespace
Phalcon\Di
-
Uses
-
Extends
-
Implements
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 notDi\ServiceProviderInterface ¶
-
Namespace
Phalcon\Di
-
Uses
-
Extends
-
Implements
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 () {
// ...
}
);
}
}