-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Service class API change and addition of helper class #13157
Conversation
…tion to be caught in a container, which already has its name.
*/ | ||
class ServiceResolutionException extends \Phalcon\Di\Exception | ||
{ | ||
} |
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.
Could you rename this file phalcon/di/exception/serviceResolutionException.zep
=> phalcon/di/exception/serviceresolutionexception.zep
to follow common style
phalcon/di/service.zep
Outdated
@@ -55,23 +53,15 @@ class Service implements ServiceInterface | |||
* Phalcon\Di\Service | |||
* | |||
* @param mixed definition | |||
* @param boolean shared |
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.
Annotation not needed at all. Just use
public function __construct(var definition, boolean shared = false)
phalcon/di/service.zep
Outdated
@@ -121,6 +111,7 @@ class Service implements ServiceInterface | |||
* Resolves the service | |||
* | |||
* @param array parameters | |||
* @param \Phalcon\DiInterface dependencyInjector | |||
* @return mixed | |||
*/ | |||
public function resolve(parameters = null, <DiInterface> dependencyInjector = null) |
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.
public function resolve(array parameters = null, <DiInterface> dependencyInjector = null) -> var
and remove @param
s and @return
phalcon/di/service.zep
Outdated
@@ -254,6 +245,7 @@ class Service implements ServiceInterface | |||
/** | |||
* Returns a parameter in a specific position | |||
* | |||
* @param int position | |||
* @return array | |||
*/ | |||
public function getParameter(int position) |
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.
public function getParameter(int position) -> array | null
and remove @param
and @return
/** | ||
* | ||
*/ | ||
protected di; |
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.
Why do we need empty comment here?
/** | ||
* | ||
*/ | ||
protected function _registerDir(string! dirPath) |
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.
Method names should not be prefixed with a single underscore to indicate protected nature. Actually when you extend this class in PHP your code will not pass the code style tests.
I propose to get rid of underscores in method names for 4.x
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
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.
@dschissler Frankly speaking I'm a little confused with Dir
. Probably it's because I'm not a native speaker (like many of our users). Dir it is: DIRection. DIRectory, Do It Right, ...? Why do we need abbreviations for methods that aren't part of the public API?
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
You have successfully broken all the tests :) |
case "object": | ||
for dirPathItem in iterator(dirPath) { | ||
this->_registerDir(dirPathItem); | ||
} |
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.
if dirPath instanceof Traversable
?
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
} | ||
break; | ||
default: | ||
throw new Exception("Invalid services."); |
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.
I would like to see here a error message with an explanation of what happened and the reasons for this
{ | ||
var dirPathItem; | ||
|
||
switch (gettype(dirPath)) { |
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.
switch typeof dirPath {
// ...
}
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
|
||
let dir = new \DirectoryIterator(dirPath); | ||
for fileinfo in iterator(dir) { | ||
if (fileinfo->isDir()) { |
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.
if unlikely fileinfo->isDir() === true {
// ...
}
Also I'm pretty sure we don't need to scan this dir in each request
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.
How about .
and ..
?
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
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.
In this case I propose consider such a design with which user can easily inject a caching mechanism. For example Decorator Pattern
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
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.
Php already caches stuff like this.
This comment was marked as abuse.
This comment was marked as abuse.
This comment was marked as abuse.
This comment was marked as abuse.
@dschissler Could you remove session from factory default? See: #12921 |
I removed the name parameter from the Service class since the only place where it is used in the Phalcon code is for throwing an exception when there was a problem with the service. I added a new exception class which the DI catches and since the DI knows what the name of the service should be it is able to provide the same error reporting capabilities.
This API change allows for
Phalcon\Di\Service
classes to more gracefully wrap service closures.I added a
Phalcon\Di\Service\SharedService
class which simply offers API sugar to make the second parameter unnecessary when using a shared service.I added a new helper class
Phalcon\Di\Service\ServiceRegistry
for registering a directory of service files in either yaml or php. These files can return a php array with a static definition, a closure, aService
object or a string for the class name.[edit] I'm going to take a week off from this and then I'll take down this pull request and start over with a clean repo with just 2-3 commits. I'm going to redesign the API a bit, modify the tests, etc.