Replies: 1 comment
-
another case i just faced ... namespace Nuxed\DependencyInjection\Processor;
use namespace Nuxed\DependencyInjection;
use namespace Nuxed\DependencyInjection\Inflector;
final class ServiceContainerAwareProcessor implements IProcessor {
/**
* Process the given service definition and return the
* modified service definition.
*/
public function process<<<__Enforceable>> reify T>(
DependencyInjection\ServiceDefinition<T> $definition,
): DependencyInjection\ServiceDefinition<T> {
if (
\is_a(
$definition->getType(), // classname<T>
DependencyInjection\IServiceContainerAware::class,
true,
)
) {
return $definition->withInflector(
/* HH_IGNORE_ERROR[4110] - hack can't understand this, but it is valid. */
new Inflector\ServiceContainerAwareInflector(),
);
}
return $definition;
}
}
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The current
classname
is a bit broken, and using it in a more than string-class-name-representation is quite hard if not impossible in some cases ( see #8772 )Another issue is that class name cannot be enforced to be a real class name, an interface name, a trait name, an abstract class name .. etc.
This forces the developer to create a dummy interface to enforce against, even if the interface has no methods.
The case for dependency injection
In the past 2 weeks, I have been working on a DI component for the @nuxed Framework.
the current service container interface looks like this:
what I want to focus on is the
tagged
method, this method is supposed to return all implementations tagged with a specific interface, however, currently is possible to tag using any class name, that is being an abstract class or a non-abstract base class, but I want to forbid this behavior, making it impossible to use anything but an interface as a tag, this will promote composition over inheritance.the container builder which allows you to add service definitions and tag services with a specific interface looks like this:
as you can see in the
tag
method, as long as the tag is a subtype of the service type, things will work.this can be enforced at runtime using reflections or
interface_exists
, however, it is also possible to determine this statically if Hack had a type to represent interfaces.Another issue ( ref #8461 ) is that hack lacks the type
object
, making it impossible to useT::class
, unless the generic parameterT
is of a specific class, therefor, @nuxed is forced to requireclassname<T>
as an argument, instead of usingT::class
.Another issue is that T ( even when reified ) doesn't hold on to its own generic parameters.
Solution
I'm not really sure what the best solution for this is, let's discuss it :)
Beta Was this translation helpful? Give feedback.
All reactions