FactoryType.php000064400000000771150732353110007533 0ustar00resolve_value( $container ); } } AbstractDependencyType.php000064400000002362150732353110011664 0ustar00callable_or_value = $callable_or_value; } /** * Resolver for the internal dependency value. * * @param Container $container The Dependency Injection Container. * * @return mixed */ protected function resolve_value( Container $container ) { $callback = $this->callable_or_value; return \is_callable( $callback ) ? $callback( $container ) : $callback; } /** * Retrieves the value stored internally for this DependencyType * * @param Container $container The Dependency Injection Container. * * @return void */ abstract public function get( Container $container ); } SharedType.php000064400000001344150732353110007327 0ustar00shared_instance ) ) { $this->shared_instance = $this->resolve_value( $container ); } return $this->shared_instance; } } Container.php000064400000006025150732353110007202 0ustar00register( MyClass::class, $container->factory( $mycallback ) ); * ``` * * @param Closure $instantiation_callback This will be invoked when the * dependency is required. It will * receive an instance of this * container so the callback can * retrieve dependencies from the * container. * * @return FactoryType An instance of the FactoryType dependency. */ public function factory( Closure $instantiation_callback ) { return new FactoryType( $instantiation_callback ); } /** * Interface for registering a new dependency with the container. * * By default, the $value will be added as a shared dependency. This means * that it will be a single instance shared among any other classes having * that dependency. * * If you want a new instance every time it's required, then wrap the value * in a call to the factory method (@see Container::factory for example) * * Note: Currently if the provided id already is registered in the container, * the provided value is ignored. * * @param string $id A unique string identifier for the provided value. * Typically it's the fully qualified name for the * dependency. * @param mixed $value The value for the dependency. Typically, this is a * closure that will create the class instance needed. */ public function register( $id, $value ) { if ( empty( $this->registry[ $id ] ) ) { if ( ! $value instanceof FactoryType ) { $value = new SharedType( $value ); } $this->registry[ $id ] = $value; } } /** * Interface for retrieving the dependency stored in the container for the * given identifier. * * @param string $id The identifier for the dependency being retrieved. * @throws Exception If there is no dependency for the given identifier in * the container. * * @return mixed Typically a class instance. */ public function get( $id ) { if ( ! isset( $this->registry[ $id ] ) ) { // this is a developer facing exception, hence it is not localized. throw new Exception( sprintf( 'Cannot construct an instance of %s because it has not been registered.', $id ) ); } return $this->registry[ $id ]->get( $this ); } }