-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(dependencyinjection): Allow optional (nullable) services #41265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(dependencyinjection): Allow optional (nullable) services #41265
Conversation
Allows working with classes that might or might not be available. Signed-off-by: Christoph Wurst <[email protected]>
juliusknorr
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍
|
@ChristophWurst Is it supposed to work for i.e.
What I would expect is that Here is my attempt: devnoname120/epubviewer@89a76c2 What I see is this instead: {
"reqId": "SA3HE70WauL60defOmaI",
"level": 3,
"time": "2025-02-26T01:17:09+00:00",
"remoteAddr": "192.168.215.1",
"user": "--",
"app": "index",
"method": "GET",
"url": "/index.php/apps/epubviewer/?file=%2Findex.php%2Fs%2FrsQgKNjyNGqgYEP%2Fdownload&type=application%2Fepub%2Bzip",
"message": "OCA\\Epubviewer\\Db\\BookmarkMapper::__construct(): Argument #2 ($userId) must be of type string, null given",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
"version": "30.0.6.2",
"exception": {
"Exception": "TypeError",
"Message": "OCA\\Epubviewer\\Db\\BookmarkMapper::__construct(): Argument #2 ($userId) must be of type string, null given",
"Code": 0,
"Trace": [
{
"function": "__construct",
"class": "OCA\\Epubviewer\\Db\\BookmarkMapper",
"type": "->",
"args": [
{
"__class__": "OC\\DB\\ConnectionAdapter"
},
null,
{
"__class__": "OCA\\Epubviewer\\Utility\\Time"
}
]
},
{
"file": "/var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php",
"line": 98,
"function": "newInstanceArgs",
"class": "ReflectionClass",
"type": "->",
"args": [
[
{
"__class__": "OC\\DB\\ConnectionAdapter"
},
null,
{
"__class__": "OCA\\Epubviewer\\Utility\\Time"
}
]
]
},
{
"file": "/var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php",
"line": 106,
"function": "buildClass",
"class": "OC\\AppFramework\\Utility\\SimpleContainer",
"type": "->",
"args": [
{
"__class__": "ReflectionClass",
"name": "OCA\\Epubviewer\\Db\\BookmarkMapper"
}
]
},
{
"file": "/var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php",
"line": 124,
"function": "resolve",
"class": "OC\\AppFramework\\Utility\\SimpleContainer",
"type": "->",
"args": [
"OCA\\Epubviewer\\Db\\BookmarkMapper"
]
},
{
"file": "/var/www/html/lib/private/AppFramework/DependencyInjection/DIContainer.php",
"line": 451,
"function": "query",
"class": "OC\\AppFramework\\Utility\\SimpleContainer",
"type": "->",
"args": [
"OCA\\Epubviewer\\Db\\BookmarkMapper"
]
},
{
"file": "/var/www/html/lib/private/AppFramework/DependencyInjection/DIContainer.php",
"line": 423,
"function": "queryNoFallback",
"class": "OC\\AppFramework\\DependencyInjection\\DIContainer",
"type": "->",
"args": [
"OCA\\Epubviewer\\Db\\BookmarkMapper"
]
},
{
"file": "/var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php",
"line": 74,
"function": "query",
"class": "OC\\AppFramework\\DependencyInjection\\DIContainer",
"type": "->",
"args": [
"OCA\\Epubviewer\\Db\\BookmarkMapper",
true
]
},
{
"function": "OC\\AppFramework\\Utility\\{closure}",
"class": "OC\\AppFramework\\Utility\\SimpleContainer",
"type": "->",
"args": [
"*** sensitive parameters replaced ***"
]
},
{
"file": "/var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php",
"line": 98,
"function": "array_map",
"args": [
{
"__class__": "Closure"
},
[
"*** sensitive parameters replaced ***"
]
]
},
{
"file": "/var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php",
"line": 106,
"function": "buildClass",
"class": "OC\\AppFramework\\Utility\\SimpleContainer",
"type": "->",
"args": [
{
"__class__": "ReflectionClass",
"name": "OCA\\Epubviewer\\Service\\BookmarkService"
}
]
},
{
"file": "/var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php",
"line": 124,
"function": "resolve",
"class": "OC\\AppFramework\\Utility\\SimpleContainer",
"type": "->",
"args": [
"OCA\\Epubviewer\\Service\\BookmarkService"
]
},
{
"file": "/var/www/html/lib/private/AppFramework/DependencyInjection/DIContainer.php",
"line": 451,
"function": "query",
"class": "OC\\AppFramework\\Utility\\SimpleContainer",
"type": "->",
"args": [
"OCA\\Epubviewer\\Service\\BookmarkService"
]
},
{
"file": "/var/www/html/lib/private/AppFramework/DependencyInjection/DIContainer.php",
"line": 423,
"function": "queryNoFallback",
"class": "OC\\AppFramework\\DependencyInjection\\DIContainer",
"type": "->",
"args": [
"OCA\\Epubviewer\\Service\\BookmarkService"
]
},
{
"file": "/var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php",
"line": 74,
"function": "query",
"class": "OC\\AppFramework\\DependencyInjection\\DIContainer",
"type": "->",
"args": [
"OCA\\Epubviewer\\Service\\BookmarkService",
true
]
},
{
"function": "OC\\AppFramework\\Utility\\{closure}",
"class": "OC\\AppFramework\\Utility\\SimpleContainer",
"type": "->",
"args": [
"*** sensitive parameters replaced ***"
]
},
{
"file": "/var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php",
"line": 98,
"function": "array_map",
"args": [
{
"__class__": "Closure"
},
[
{
"__class__": "ReflectionParameter",
"name": "appName"
},
{
"__class__": "ReflectionParameter",
"name": "request"
},
{
"__class__": "ReflectionParameter",
"name": "urlGenerator"
},
{
"__class__": "ReflectionParameter",
"name": "rootFolder"
},
{
"__class__": "ReflectionParameter",
"name": "shareManager"
},
"And 3 more entries, set log level to debug to see all entries"
]
]
},
{
"file": "/var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php",
"line": 106,
"function": "buildClass",
"class": "OC\\AppFramework\\Utility\\SimpleContainer",
"type": "->",
"args": [
{
"__class__": "ReflectionClass",
"name": "OCA\\Epubviewer\\Controller\\PageController"
}
]
},
{
"file": "/var/www/html/lib/private/AppFramework/Utility/SimpleContainer.php",
"line": 124,
"function": "resolve",
"class": "OC\\AppFramework\\Utility\\SimpleContainer",
"type": "->",
"args": [
"OCA\\Epubviewer\\Controller\\PageController"
]
},
{
"file": "/var/www/html/lib/private/AppFramework/DependencyInjection/DIContainer.php",
"line": 451,
"function": "query",
"class": "OC\\AppFramework\\Utility\\SimpleContainer",
"type": "->",
"args": [
"OCA\\Epubviewer\\Controller\\PageController"
]
},
{
"file": "/var/www/html/lib/private/AppFramework/DependencyInjection/DIContainer.php",
"line": 423,
"function": "queryNoFallback",
"class": "OC\\AppFramework\\DependencyInjection\\DIContainer",
"type": "->",
"args": [
"OCA\\Epubviewer\\Controller\\PageController"
]
},
{
"file": "/var/www/html/lib/private/AppFramework/App.php",
"line": 140,
"function": "query",
"class": "OC\\AppFramework\\DependencyInjection\\DIContainer",
"type": "->",
"args": [
"OCA\\Epubviewer\\Controller\\PageController"
]
},
{
"file": "/var/www/html/lib/private/Route/Router.php",
"line": 303,
"function": "main",
"class": "OC\\AppFramework\\App",
"type": "::",
"args": [
"OCA\\Epubviewer\\Controller\\PageController",
"showReader",
{
"__class__": "OC\\AppFramework\\DependencyInjection\\DIContainer"
},
{
"_route": "epubviewer.page.showreader"
}
]
},
{
"file": "/var/www/html/lib/base.php",
"line": 1003,
"function": "match",
"class": "OC\\Route\\Router",
"type": "->",
"args": [
"/apps/epubviewer/"
]
},
{
"file": "/var/www/html/index.php",
"line": 24,
"function": "handleRequest",
"class": "OC",
"type": "::",
"args": []
}
],
"File": "/var/www/html/apps-extra/epubviewer/lib/Db/BookmarkMapper.php",
"Line": 23,
"message": "OCA\\Epubviewer\\Db\\BookmarkMapper::__construct(): Argument #2 ($userId) must be of type string, null given",
"exception": {},
"CustomMessage": "OCA\\Epubviewer\\Db\\BookmarkMapper::__construct(): Argument #2 ($userId) must be of type string, null given"
}
} |
Summary
Allows working with classes that might or might not be available. For safety I chose to only allow this if a constructor argument is typed.
I need this for a cross-app dependency inside of server in #40559.
Checklist