Di\AbstractInjectionAware
AbstractSource on GitHubThis abstract class offers common access to the DI in a class
\stdClassPhalcon\Di\AbstractInjectionAware- implementsPhalcon\Di\InjectionAwareInterface
Uses Phalcon\Di\Traits\InjectionAwareTrait · stdClass
Di\Di
ClassSource on GitHubPhalcon\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();\stdClassPhalcon\Di\Di- implementsPhalcon\Di\DiInterface
Uses Phalcon\Di\Exception · Phalcon\Di\Exception\ServiceResolutionException · Phalcon\Di\Exceptions\AliasAlreadyInUse · Phalcon\Di\Exceptions\AliasNameMustBeString · Phalcon\Di\Exceptions\CircularAliasReference · Phalcon\Di\Traits\DiArrayAccessTrait · Phalcon\Di\Traits\DiEventsTrait · Phalcon\Di\Traits\DiExceptionsTrait · Phalcon\Di\Traits\DiInstanceTrait · Phalcon\Di\Traits\DiLoadTrait · Phalcon\Events\ManagerInterface · Phalcon\Events\Traits\EventsAwareTrait · stdClass
Method Summary
public__call(string $method,array $arguments = [])Magic method to get or set services using setters/getters
public__construct()Phalcon\Di\Di constructor
publicattempt(string $name,mixed $definition,bool $shared = false)Attempts to register a service in the services container
publicmixedget(string $name,mixed $parameters = null)Resolves the service based on its configuration
publicstringgetAlias(string $name)Return the alias based on a passed key. Returns an empty string if
publicDiInterface|nullgetDefault()Return the latest DI created
publicManagerInterface|nullgetInternalEventsManager()Returns the internal event manager
publicmixedgetRaw(string $name)Returns a service definition without resolving
publicServiceInterfacegetService(string $name)Returns a Phalcon\Di\Service instance
publicarraygetServices()Return the services registered in the DI
publicmixedgetShared(string $name,mixed $parameters = null)Resolves a service, the resolved service is stored in the DI, subsequent
publicboolhas(string $name)Check whether the DI contains a service by a name
publicboolhasShared(string $name)Check whether the DI has a cached shared instance for a service name.
publicvoidregister(ServiceProviderInterface $provider)Registers a service provider.
publicvoidremove(string $name)Removes a service in the services container
publicvoidremoveShared(string $name)Removes the cached shared instance for a service, leaving the service
publicvoidreset()Resets the internal default DI
publicServiceInterfaceset(string $name,mixed $definition,bool $shared = false)Registers a service in the services container
publicselfsetAlias(string $name,array|string $aliases)Sets one or more aliases to the given name.
publicvoidsetDefault(DiInterface $container)Set a default dependency injection container to be obtained into static
publicvoidsetInternalEventsManager(ManagerInterface $eventsManager)Sets the internal event manager
publicServiceInterfacesetService(string $name,ServiceInterface $rawDefinition)Sets a service using a raw Phalcon\Di\Service definition
publicServiceInterfacesetShared(string $name,mixed $definition)Registers an “always shared” service in the services container
Properties
protectedarray<string, string>$aliases = []List of service aliases
protectedobject|null$defaultContainer = nullLatest DI build
protectedServiceInterface[]$services = []List of registered services
protectedarray$sharedInstances = []List of shared instances
Methods
__call()
public function __call(
string $method,
array $arguments = []
);Magic method to get or set services using setters/getters
__construct()
public function __construct();Phalcon\Di\Di constructor
attempt()
public function attempt(
string $name,
mixed $definition,
bool $shared = false
);Attempts to register a service in the services container Only is successful if a service hasn’t been registered previously with the same name
get()
public function get(
string $name,
mixed $parameters = null
): mixed;Resolves the service based on its configuration
getAlias()
public function getAlias( string $name ): string;Return the alias based on a passed key. Returns an empty string if the alias does not exist
getDefault()
public static function getDefault(): DiInterface|null;Return the latest DI created
getInternalEventsManager()
public function getInternalEventsManager(): ManagerInterface|null;Returns the internal event manager
getRaw()
public function getRaw( string $name ): mixed;Returns a service definition without resolving
getService()
public function getService( string $name ): ServiceInterface;Returns a Phalcon\Di\Service instance
getServices()
public function getServices(): array;Return the services registered in the DI
getShared()
public function getShared(
string $name,
mixed $parameters = null
): mixed;Resolves a service, the resolved service is stored in the DI, subsequent requests for this service will return the same instance
has()
public function has( string $name ): bool;Check whether the DI contains a service by a name
hasShared()
public function hasShared( string $name ): bool;Check whether the DI has a cached shared instance for a service name.
Unlike has(), which reports on the service definition registry,
this method reports only on the resolved-instance cache populated by
getShared().
register()
public function register( ServiceProviderInterface $provider ): void;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 () {
// ...
}
);
}
}remove()
public function remove( string $name ): void;Removes a service in the services container It also removes any shared instance created for the service
removeShared()
public function removeShared( string $name ): void;Removes the cached shared instance for a service, leaving the service
definition intact so the next getShared() call rebuilds it.
reset()
public static function reset(): void;Resets the internal default DI
set()
public function set(
string $name,
mixed $definition,
bool $shared = false
): ServiceInterface;Registers a service in the services container
setAlias()
public function setAlias(
string $name,
array|string $aliases
): self;Sets one or more aliases to the given name.
setDefault()
public static function setDefault( DiInterface $container ): void;Set a default dependency injection container to be obtained into static methods
setInternalEventsManager()
public function setInternalEventsManager( ManagerInterface $eventsManager ): void;Sets the internal event manager
setService()
public function setService(
string $name,
ServiceInterface $rawDefinition
): ServiceInterface;Sets a service using a raw Phalcon\Di\Service definition
setShared()
public function setShared(
string $name,
mixed $definition
): ServiceInterface;Registers an “always shared” service in the services container
Di\DiInterface
InterfaceSource on GitHubInterface for Phalcon\Di
\ArrayAccessPhalcon\Di\DiInterface
Uses ArrayAccess
Method Summary
publicattempt(string $name,mixed $definition,bool $shared = false)Attempts to register a service in the services container
publicmixedget(string $name,mixed $parameters = null)Resolves the service based on its configuration
publicDiInterface|nullgetDefault()Return the last DI created
publicmixedgetRaw(string $name)Returns a service definition without resolving
publicServiceInterfacegetService(string $name)Returns the corresponding Phalcon\Di\Service instance for a service
publicarraygetServices()Return the services registered in the DI
publicmixedgetShared(string $name,mixed $parameters = null)Returns a shared service based on their configuration
publicboolhas(string $name)Check whether the DI contains a service by a name
publicboolhasShared(string $name)Check whether the DI has a cached shared instance for a service name.
publicvoidremove(string $name)Removes a service in the services container
publicvoidremoveShared(string $name)Removes the cached shared instance for a service, leaving the service
publicvoidreset()Resets the internal default DI
publicServiceInterfaceset(string $name,mixed $definition,bool $shared = false)Registers a service in the services container
publicvoidsetDefault(DiInterface $container)Set a default dependency injection container to be obtained into static
publicServiceInterfacesetService(string $name,ServiceInterface $rawDefinition)Sets a service using a raw Phalcon\Di\Service definition
publicServiceInterfacesetShared(string $name,mixed $definition)Registers an “always shared” service in the services container
Methods
attempt()
public function attempt(
string $name,
mixed $definition,
bool $shared = false
);Attempts to register a service in the services container Only is successful if a service hasn’t been registered previously with the same name
get()
public function get(
string $name,
mixed $parameters = null
): mixed;Resolves the service based on its configuration
getDefault()
public static function getDefault(): DiInterface|null;Return the last DI created
getRaw()
public function getRaw( string $name ): mixed;Returns a service definition without resolving
getService()
public function getService( string $name ): ServiceInterface;Returns the corresponding Phalcon\Di\Service instance for a service
getServices()
public function getServices(): array;Return the services registered in the DI
getShared()
public function getShared(
string $name,
mixed $parameters = null
): mixed;Returns a shared service based on their configuration
has()
public function has( string $name ): bool;Check whether the DI contains a service by a name
hasShared()
public function hasShared( string $name ): bool;Check whether the DI has a cached shared instance for a service name.
Unlike has(), which reports on the service definition registry,
this method reports only on the resolved-instance cache populated by
getShared(). A service can be registered (has() returns true)
without yet having a shared instance (hasShared() returns false).
remove()
public function remove( string $name ): void;Removes a service in the services container
removeShared()
public function removeShared( string $name ): void;Removes the cached shared instance for a service, leaving the service
definition intact so the next getShared() call rebuilds it.
Useful in fork-based multi-process setups where a child inherits the parent’s resource handle (e.g. a database connection) and needs to discard the cached instance without re-registering the service.
reset()
public static function reset(): void;Resets the internal default DI
set()
public function set(
string $name,
mixed $definition,
bool $shared = false
): ServiceInterface;Registers a service in the services container
setDefault()
public static function setDefault( DiInterface $container ): void;Set a default dependency injection container to be obtained into static methods
setService()
public function setService(
string $name,
ServiceInterface $rawDefinition
): ServiceInterface;Sets a service using a raw Phalcon\Di\Service definition
setShared()
public function setShared(
string $name,
mixed $definition
): ServiceInterface;Registers an “always shared” service in the services container
Di\Exception
ClassSource on GitHubExceptions thrown in Phalcon\Di will use this class
\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exception\ServiceResolutionExceptionPhalcon\Di\Exceptions\AliasAlreadyInUsePhalcon\Di\Exceptions\AliasNameMustBeStringPhalcon\Di\Exceptions\ArgumentTypeRequiredPhalcon\Di\Exceptions\CallArgumentsMustBeArrayPhalcon\Di\Exceptions\CircularAliasReferencePhalcon\Di\Exceptions\ContainerRequiredPhalcon\Di\Exceptions\DefinitionMustBeArrayForReadPhalcon\Di\Exceptions\DefinitionMustBeArrayForUpdatePhalcon\Di\Exceptions\MethodCallMustBeArrayPhalcon\Di\Exceptions\MethodNameRequiredPhalcon\Di\Exceptions\MissingClassNameParameterPhalcon\Di\Exceptions\MissingParameterKeyPhalcon\Di\Exceptions\PropertyInjectionRequiresInstancePhalcon\Di\Exceptions\PropertyMustBeArrayPhalcon\Di\Exceptions\PropertyNameRequiredPhalcon\Di\Exceptions\PropertyValueRequiredPhalcon\Di\Exceptions\ServiceCannotBeResolvedPhalcon\Di\Exceptions\SetterInjectionRequiresInstancePhalcon\Di\Exceptions\SetterParametersMustBeArrayPhalcon\Di\Exceptions\UnknownServiceType
Di\Exception\ServiceResolutionException
ClassSource on GitHubPhalcon\Di\Exception\ServiceResolutionException
\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exception\ServiceResolutionException
Uses Phalcon\Di\Exception
Di\Exceptions\AliasAlreadyInUse
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\AliasAlreadyInUse
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct( string $alias );Di\Exceptions\AliasNameMustBeString
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\AliasNameMustBeString
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct();Di\Exceptions\ArgumentTypeRequired
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\ArgumentTypeRequired
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct( int $position );Di\Exceptions\CallArgumentsMustBeArray
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\CallArgumentsMustBeArray
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct( int $position );Di\Exceptions\CircularAliasReference
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\CircularAliasReference
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct( string $name );Di\Exceptions\ContainerRequired
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\ContainerRequired
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct();Di\Exceptions\DefinitionMustBeArrayForRead
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\DefinitionMustBeArrayForRead
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct();Di\Exceptions\DefinitionMustBeArrayForUpdate
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\DefinitionMustBeArrayForUpdate
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct();Di\Exceptions\MethodCallMustBeArray
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\MethodCallMustBeArray
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct( int $position );Di\Exceptions\MethodNameRequired
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\MethodNameRequired
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct( int $position );Di\Exceptions\MissingClassNameParameter
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\MissingClassNameParameter
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct();Di\Exceptions\MissingParameterKey
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\MissingParameterKey
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct(
string $key,
int $position
);Di\Exceptions\PropertyInjectionRequiresInstance
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\PropertyInjectionRequiresInstance
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct();Di\Exceptions\PropertyMustBeArray
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\PropertyMustBeArray
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct( int $position );Di\Exceptions\PropertyNameRequired
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\PropertyNameRequired
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct( int $position );Di\Exceptions\PropertyValueRequired
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\PropertyValueRequired
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct( int $position );Di\Exceptions\ServiceCannotBeResolved
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\ServiceCannotBeResolved
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct( string $name );Di\Exceptions\SetterInjectionRequiresInstance
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\SetterInjectionRequiresInstance
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct();Di\Exceptions\SetterParametersMustBeArray
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\SetterParametersMustBeArray
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct();Di\Exceptions\UnknownServiceType
ClassSource on GitHub\ExceptionPhalcon\Di\ExceptionPhalcon\Di\Exceptions\UnknownServiceType
Uses Phalcon\Di\Exception
Method Summary
Methods
__construct()
public function __construct( int $position );Di\FactoryDefault
ClassSource on GitHubThis 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
@property Annotations $annotations @property AnnotationsMemory $annotationsMemory @property AssetsManager $assets @property Crypt $crypt @property Cookies $cookies @property Dispatcher $dispatcher @property Escaper $escaper @property EventsManager $eventsManager @property Factory $modelsEventFactory @property Direct $flash @property Session $flashSession @property Filter $filter @property HelperFactory $helper @property ModelsManager $modelsManager @property MetadataManager $modelsMetadata @property QueueFactory $queueFactory @property Request $request @property Response $response @property Router $router @property Security $security @property Settings $settings @property SerializerFactory $storageSerializer @property TagFactory $tag @property TransactionManager $transactionManager @property Url $url
\stdClassPhalcon\Di\DiPhalcon\Di\FactoryDefault
Uses Phalcon\Annotations\Adapter\Memory · Phalcon\Annotations\Annotations · Phalcon\Assets\Manager · Phalcon\Db\Event\Factory · Phalcon\Encryption\Crypt · Phalcon\Encryption\Security · Phalcon\Events\Manager · Phalcon\Filter\Filter · Phalcon\Filter\FilterFactory · Phalcon\Flash\Direct · Phalcon\Flash\Session · Phalcon\Html\Escaper · Phalcon\Html\TagFactory · Phalcon\Http\Request · Phalcon\Http\Response · Phalcon\Http\Response\Cookies · Phalcon\Mvc\Dispatcher · Phalcon\Mvc\Model\Manager · Phalcon\Mvc\Model\MetaData\Memory · Phalcon\Mvc\Model\Transaction\Manager · Phalcon\Mvc\Router · Phalcon\Mvc\Url · Phalcon\Queue\QueueFactory · Phalcon\Storage\SerializerFactory · Phalcon\Support\HelperFactory · Phalcon\Support\Settings
Method Summary
Methods
__construct()
public function __construct();Phalcon\Di\FactoryDefault constructor
Di\FactoryDefault\Cli
ClassSource on GitHubPhalcon\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
\stdClassPhalcon\Di\DiPhalcon\Di\FactoryDefaultPhalcon\Di\FactoryDefault\Cli
Uses Phalcon\Annotations\Adapter\Memory · Phalcon\Annotations\Annotations · Phalcon\Cli\Dispatcher · Phalcon\Cli\Router · Phalcon\Di\FactoryDefault · Phalcon\Di\Service · Phalcon\Encryption\Security · Phalcon\Events\Manager · Phalcon\Filter\FilterFactory · Phalcon\Html\Escaper · Phalcon\Html\TagFactory · Phalcon\Mvc\Model\Manager · Phalcon\Mvc\Model\MetaData\Memory · Phalcon\Mvc\Model\Transaction\Manager · Phalcon\Queue\QueueFactory · Phalcon\Storage\SerializerFactory · Phalcon\Support\HelperFactory · Phalcon\Support\Settings
Method Summary
Methods
__construct()
public function __construct();Phalcon\Di\FactoryDefault\Cli constructor
Di\InitializationAwareInterface
InterfaceSource on GitHubInterface for components that have initialize()
Phalcon\Di\InitializationAwareInterface
Method Summary
Methods
initialize()
public function initialize(): void;Di\Injectable
AbstractSource on GitHubThis 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 AnnotationsAdapterInterface|AnnotationsMemory $annotations @property AssetsManager $assets @property DiInterface|null $container @property DbAdapterInterface $db @property DiInterface|null $di @property Cookies|CookiesInterface $cookies @property Crypt|CryptInterface $crypt @property EventsManager|EventsManagerInterface $eventsManager @property Escaper|EscaperInterface $escaper @property Direct $flash @property Session $flashSession @property Filter|FilterInterface $filter @property HelperFactory $helper @property Bag|BagInterface $persistent @property Request|RequestInterface $request @property Response|ResponseInterface $response @property Router|RouterInterface $router @property Security $security @property SessionManager $session @property Settings $settings @property Url|UrlInterface $url
// * @property Manager|ManagerInterface $modelsManager // * @property AnnotationsMemory|MetadataInterface $modelsMetadata // * @property ManagerInterface $transactionManager // * @property View|ViewInterface $view
\stdClassPhalcon\Di\Injectable- implementsPhalcon\Di\InjectionAwareInterface
Uses Phalcon\Annotations\Adapter\AdapterInterface · Phalcon\Annotations\Adapter\Memory · Phalcon\Assets\Manager · Phalcon\Db\Adapter\AdapterInterface · Phalcon\Di\Traits\InjectionAwareTrait · Phalcon\Encryption\Crypt · Phalcon\Encryption\Crypt\CryptInterface · Phalcon\Encryption\Security · Phalcon\Events\Manager · Phalcon\Events\ManagerInterface · Phalcon\Filter\Filter · Phalcon\Filter\FilterInterface · Phalcon\Flash\Direct · Phalcon\Flash\Session · Phalcon\Html\Escaper · Phalcon\Html\Escaper\EscaperInterface · Phalcon\Http\Request · Phalcon\Http\RequestInterface · Phalcon\Http\Response · Phalcon\Http\ResponseInterface · Phalcon\Http\Response\Cookies · Phalcon\Http\Response\CookiesInterface · Phalcon\Mvc\Model\Manager · Phalcon\Mvc\Model\ManagerInterface · Phalcon\Mvc\Router · Phalcon\Mvc\RouterInterface · Phalcon\Mvc\Url · Phalcon\Mvc\Url\UrlInterface · Phalcon\Session\Bag · Phalcon\Session\BagInterface · Phalcon\Session\ManagerInterface · Phalcon\Support\HelperFactory · Phalcon\Support\Settings · stdClass
Method Summary
public__get(string $propertyName)Magic method __get
publicbool__isset(string $name)Magic method __isset
publicDiInterface|nullgetDI()Returns the internal dependency injector
Methods
__get()
public function __get( string $propertyName );Magic method __get
__isset()
public function __isset( string $name ): bool;Magic method __isset
getDI()
public function getDI(): DiInterface|null;Returns the internal dependency injector
Di\InjectionAwareInterface
InterfaceSource on GitHubThis interface must be implemented in those classes that uses internally the Phalcon\Di that creates them
Phalcon\Di\InjectionAwareInterface
Method Summary
publicDiInterface|nullgetDI()Returns the internal dependency injector
publicvoidsetDI(DiInterface $container)Sets the dependency injector
Methods
getDI()
public function getDI(): DiInterface|null;Returns the internal dependency injector
setDI()
public function setDI( DiInterface $container ): void;Sets the dependency injector
Di\Service
ClassSource on GitHubRepresents individually a service in the services container
$service = new \Phalcon\Di\Service(
"request",
\Phalcon\Http\Request::class
);
$request = service->resolve();@property array $definition @property bool $resolved @property bool $shared @property mixed $sharedInstance
Phalcon\Di\Service- implementsPhalcon\Di\ServiceInterface
Uses Closure · Phalcon\Di\Exception\ServiceResolutionException · Phalcon\Di\Exceptions\DefinitionMustBeArrayForRead · Phalcon\Di\Exceptions\DefinitionMustBeArrayForUpdate · Phalcon\Di\Service\Builder · Phalcon\Di\Traits\DiInstanceTrait
Method Summary
public__construct(mixed $definition,bool $shared = false)Service constructor.
publicmixedgetDefinition()Returns the service definition
publicmixedgetParameter(int $position)Returns a parameter in a specific position
publicboolisResolved()Returns true if the service was resolved
publicboolisShared()Check whether the service is shared or not
publicmixedresolve(array|null $parameters = null,DiInterface|null $container = null)Resolves the service
publicvoidsetDefinition(mixed $definition)Set the service definition
publicServiceInterfacesetParameter(int $position,array $parameter)Changes a parameter in the definition without resolve the service
publicvoidsetShared(bool $shared)Sets if the service is shared or not
publicvoidsetSharedInstance(mixed $sharedInstance)Sets/Resets the shared instance related to the service
Properties
protectedmixed$definitionprotectedbool$resolved = falseprotectedbool$shared = falseprotectedmixed$sharedInstanceMethods
__construct()
final public function __construct(
mixed $definition,
bool $shared = false
);Service constructor.
getDefinition()
public function getDefinition(): mixed;Returns the service definition
getParameter()
public function getParameter( int $position ): mixed;Returns a parameter in a specific position
isResolved()
public function isResolved(): bool;Returns true if the service was resolved
isShared()
public function isShared(): bool;Check whether the service is shared or not
resolve()
public function resolve(
array|null $parameters = null,
DiInterface|null $container = null
): mixed;Resolves the service
setDefinition()
public function setDefinition( mixed $definition ): void;Set the service definition
setParameter()
public function setParameter(
int $position,
array $parameter
): ServiceInterface;Changes a parameter in the definition without resolve the service
setShared()
public function setShared( bool $shared ): void;Sets if the service is shared or not
setSharedInstance()
public function setSharedInstance( mixed $sharedInstance ): void;Sets/Resets the shared instance related to the service
Di\ServiceInterface
InterfaceSource on GitHubRepresents a service in the services container
Phalcon\Di\ServiceInterface
Method Summary
publicmixedgetDefinition()Returns the service definition
publicmixedgetParameter(int $position)Returns a parameter in a specific position
publicboolisResolved()Returns true if the service was resolved
publicboolisShared()Check whether the service is shared or not
publicmixedresolve(array|null $parameters = null,DiInterface|null $container = null)Resolves the service
publicsetDefinition(mixed $definition)Set the service definition
publicServiceInterfacesetParameter(int $position,array $parameter)Changes a parameter in the definition without resolve the service
publicsetShared(bool $shared)Sets if the service is shared or not
Methods
getDefinition()
public function getDefinition(): mixed;Returns the service definition
getParameter()
public function getParameter( int $position ): mixed;Returns a parameter in a specific position
isResolved()
public function isResolved(): bool;Returns true if the service was resolved
isShared()
public function isShared(): bool;Check whether the service is shared or not
resolve()
public function resolve(
array|null $parameters = null,
DiInterface|null $container = null
): mixed;Resolves the service
setDefinition()
public function setDefinition( mixed $definition );Set the service definition
setParameter()
public function setParameter(
int $position,
array $parameter
): ServiceInterface;Changes a parameter in the definition without resolve the service
setShared()
public function setShared( bool $shared );Sets if the service is shared or not
Di\ServiceProviderInterface
InterfaceSource on GitHubShould 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 () {
// ...
}
);
}
}Phalcon\Di\ServiceProviderInterface
Method Summary
Methods
register()
public function register( DiInterface $container ): void;Registers a service provider.
Di\Service\Builder
ClassSource on GitHubThis class builds instances based on complex definitions
Phalcon\Di\Service\Builder
Uses Phalcon\Di\DiInterface · Phalcon\Di\Exception · Phalcon\Di\Traits\DiExceptionsTrait · Phalcon\Di\Traits\DiInstanceTrait
Method Summary
Methods
build()
public function build(
DiInterface $container,
array $definition,
array|null $parameters = null
);Builds a service using a complex service definition
Di\Traits\DiArrayAccessTrait
TraitSource on GitHubPhalcon\Di\Traits\DiArrayAccessTrait
Uses Phalcon\Di\Exception · Phalcon\Di\InjectionAwareInterface · Phalcon\Di\ServiceInterface · ReturnTypeWillChange
Used by Phalcon\Di\Di
Method Summary
publicgetShared(string $name,array|null $parameters = null)Resolves a service, the resolved service is stored in the DI, subsequent
publicboolhas(string $name)Check whether the DI contains a service by a name
publicbooloffsetExists(mixed $name)Check if a service is registered using the array syntax
publicoffsetGet(mixed $name)Allows to obtain a shared service using the array syntax
publicvoidoffsetSet(mixed $name,mixed $definition)Allows to register a shared service using the array syntax
publicvoidoffsetUnset(mixed $name)Removes a service from the services container using the array syntax
publicvoidremove(string $name)Removes a service in the services container
publicServiceInterfaceset(string $name,mixed $definition,bool $shared = false)Registers a service in the services container
publicServiceInterfacesetShared(string $name,mixed $definition)Registers an “always shared” service in the services container
Methods
getShared()
abstract public function getShared(
string $name,
array|null $parameters = null
);Resolves a service, the resolved service is stored in the DI, subsequent requests for this service will return the same instance
has()
abstract public function has( string $name ): bool;Check whether the DI contains a service by a name
offsetExists()
public function offsetExists( mixed $name ): bool;Check if a service is registered using the array syntax
offsetGet()
public function offsetGet( mixed $name );Allows to obtain a shared service using the array syntax
var_dump($di["request"]);offsetSet()
public function offsetSet(
mixed $name,
mixed $definition
): void;Allows to register a shared service using the array syntax
$di["request"] = new \Phalcon\Http\Request();offsetUnset()
public function offsetUnset( mixed $name ): void;Removes a service from the services container using the array syntax
remove()
abstract public function remove( string $name ): void;Removes a service in the services container It also removes any shared instance created for the service
set()
abstract public function set(
string $name,
mixed $definition,
bool $shared = false
): ServiceInterface;Registers a service in the services container
setShared()
public function setShared(
string $name,
mixed $definition
): ServiceInterface;Registers an “always shared” service in the services container
Di\Traits\DiEventsTrait
TraitSource on GitHubTrait DiEventsTrait
Phalcon\Di\Traits\DiEventsTrait
Uses Phalcon\Events\ManagerInterface
Used by Phalcon\Di\Di
Di\Traits\DiExceptionsTrait
TraitSource on GitHubTrait DiExceptionsTrait
@package Phalcon\Di\Traits
Phalcon\Di\Traits\DiExceptionsTrait
Uses Phalcon\Di\Exception · Phalcon\Di\Exceptions\MissingParameterKey
Used by Phalcon\Di\Di · Phalcon\Di\Service\Builder
Di\Traits\DiInstanceTrait
TraitSource on GitHubTrait DiInstanceTrait
@package Phalcon\Di\Traits
Phalcon\Di\Traits\DiInstanceTrait
Used by Phalcon\Di\Di · Phalcon\Di\Service · Phalcon\Di\Service\Builder
Di\Traits\DiLoadTrait
TraitSource on GitHubTrait DiLoadTrait
@package Phalcon\Di\Traits
Phalcon\Di\Traits\DiLoadTrait
Uses Phalcon\Config\Adapter\Php · Phalcon\Config\Adapter\Yaml · Phalcon\Config\ConfigInterface
Used by Phalcon\Di\Di
Method Summary
publicvoidloadFromPhp(string $filePath)Loads services from a php config file.
publicvoidloadFromYaml(string $filePath,array|null $callbacks = null)Loads services from a yaml file.
protectedvoidloadFromConfig(ConfigInterface $config)Loads services from a Config object.
Methods
loadFromPhp()
public function loadFromPhp( string $filePath ): void;Loads services from a php config file.
$di->loadFromPhp("path/services.php");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/di
loadFromYaml()
public function loadFromYaml(
string $filePath,
array|null $callbacks = null
): void;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
loadFromConfig()
protected function loadFromConfig( ConfigInterface $config ): void;Loads services from a Config object.
Di\Traits\InjectionAwareTrait
TraitSource on GitHubThis abstract class offers common access to the DI in a class
Class AbstractInjectionAware
@package Phalcon\Di
@property object $container
Phalcon\Di\Traits\InjectionAwareTrait
Uses Phalcon\Di\DiInterface
Used by Phalcon\Di\AbstractInjectionAware · Phalcon\Di\Injectable · Phalcon\Mvc\Model\Manager · Phalcon\Mvc\Model\Query · Phalcon\Mvc\View\Engine\Volt\Compiler · Phalcon\Session\Bag
Method Summary
publicDiInterface|nullgetDI()Returns the internal dependency injector
publicvoidsetDI(DiInterface $container)Sets the dependency injector
protectedvoidcheckContainer(string $exceptionClass,string $message,int $code = 0)Properties
protectedobject|null$container = nullDependency Injector
Methods
getDI()
public function getDI(): DiInterface|null;Returns the internal dependency injector
setDI()
public function setDI( DiInterface $container ): void;Sets the dependency injector
checkContainer()
protected function checkContainer(
string $exceptionClass,
string $message,
int $code = 0
): void;