Skip to content
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

Closed
wants to merge 7 commits into from
Closed

Service class API change and addition of helper class #13157

wants to merge 7 commits into from

Conversation

dschissler
Copy link
Contributor

@dschissler dschissler commented Nov 12, 2017

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, a Service 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.

*/
class ServiceResolutionException extends \Phalcon\Di\Exception
{
}
Copy link
Contributor

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

@@ -55,23 +53,15 @@ class Service implements ServiceInterface
* Phalcon\Di\Service
*
* @param mixed definition
* @param boolean shared
Copy link
Contributor

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)

@@ -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)
Copy link
Contributor

@sergeyklay sergeyklay Nov 12, 2017

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 @params and @return

@@ -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)
Copy link
Contributor

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;
Copy link
Contributor

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)
Copy link
Contributor

@sergeyklay sergeyklay Nov 12, 2017

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.

Copy link
Contributor

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.

@sergeyklay
Copy link
Contributor

You have successfully broken all the tests :)

case "object":
for dirPathItem in iterator(dirPath) {
this->_registerDir(dirPathItem);
}
Copy link
Contributor

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.

}
break;
default:
throw new Exception("Invalid services.");
Copy link
Contributor

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)) {
Copy link
Contributor

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.


let dir = new \DirectoryIterator(dirPath);
for fileinfo in iterator(dir) {
if (fileinfo->isDir()) {
Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

@sergeyklay sergeyklay added breaks bc Functionality that breaks Backwards Compatibility New Feature Request labels Nov 12, 2017
@dschissler

This comment was marked as abuse.

@dschissler

This comment was marked as abuse.

@sergeyklay
Copy link
Contributor

@dschissler Could you remove session from factory default? See: #12921

@dschissler

This comment was marked as abuse.

@dschissler dschissler closed this Dec 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaks bc Functionality that breaks Backwards Compatibility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants