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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions phalcon/di.zep
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use Phalcon\Config;
use Phalcon\Di\Service;
use Phalcon\DiInterface;
use Phalcon\Di\Exception;
use Phalcon\Di\Exception\ServiceResolutionException;
use Phalcon\Config\Adapter\Php;
use Phalcon\Config\Adapter\Yaml;
use Phalcon\Di\ServiceInterface;
Expand Down Expand Up @@ -127,7 +128,7 @@ class Di implements DiInterface
public function set(string! name, var definition, boolean shared = false) -> <ServiceInterface>
{
var service;
let service = new Service(name, definition, shared),
let service = new Service(definition, shared),
this->_services[name] = service;
return service;
}
Expand Down Expand Up @@ -160,7 +161,7 @@ class Di implements DiInterface
var service;

if !isset this->_services[name] {
let service = new Service(name, definition, shared),
let service = new Service(definition, shared),
this->_services[name] = service;
return service;
}
Expand Down Expand Up @@ -210,7 +211,7 @@ class Di implements DiInterface
*/
public function get(string! name, parameters = null) -> var
{
var service, eventsManager, instance = null;
var service, eventsManager, instance = null, e = null;

let eventsManager = <ManagerInterface> this->_eventsManager;

Expand All @@ -227,7 +228,11 @@ class Di implements DiInterface
/**
* The service is registered in the DI
*/
let instance = service->resolve(parameters, this);
try {
let instance = service->resolve(parameters, this);
} catch ServiceResolutionException, e {
throw new Exception("Service '" . name . "' cannot be resolved");
}
} else {
/**
* The DI also acts as builder for any class even if it isn't defined in the DI
Expand Down
29 changes: 29 additions & 0 deletions phalcon/di/exception/serviceresolutionexception.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2017 Phalcon Team (https://phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: David Schissler <[email protected] |
+------------------------------------------------------------------------+
*/

namespace Phalcon\Di\Exception;

use Phalcon\Di\Exception\ServiceResolutionException;

/**
* Phalcon\Di\Exception\ServiceResolutionException
*
*/
class ServiceResolutionException extends \Phalcon\Di\Exception
{
}
42 changes: 21 additions & 21 deletions phalcon/di/factorydefault.zep
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@ class FactoryDefault extends \Phalcon\Di
parent::__construct();

let this->_services = [
"router": new Service("router", "Phalcon\\Mvc\\Router", true),
"dispatcher": new Service("dispatcher", "Phalcon\\Mvc\\Dispatcher", true),
"url": new Service("url", "Phalcon\\Mvc\\Url", true),
"modelsManager": new Service("modelsManager", "Phalcon\\Mvc\\Model\\Manager", true),
"modelsMetadata": new Service("modelsMetadata", "Phalcon\\Mvc\\Model\\MetaData\\Memory", true),
"response": new Service("response", "Phalcon\\Http\\Response", true),
"cookies": new Service("cookies", "Phalcon\\Http\\Response\\Cookies", true),
"request": new Service("request", "Phalcon\\Http\\Request", true),
"filter": new Service("filter", "Phalcon\\Filter", true),
"escaper": new Service("escaper", "Phalcon\\Escaper", true),
"security": new Service("security", "Phalcon\\Security", true),
"crypt": new Service("crypt", "Phalcon\\Crypt", true),
"annotations": new Service("annotations", "Phalcon\\Annotations\\Adapter\\Memory", true),
"flash": new Service("flash", "Phalcon\\Flash\\Direct", true),
"flashSession": new Service("flashSession", "Phalcon\\Flash\\Session", true),
"tag": new Service("tag", "Phalcon\\Tag", true),
"session": new Service("session", "Phalcon\\Session\\Adapter\\Files", true),
"sessionBag": new Service("sessionBag", "Phalcon\\Session\\Bag"),
"eventsManager": new Service("eventsManager", "Phalcon\\Events\\Manager", true),
"transactionManager": new Service("transactionManager", "Phalcon\\Mvc\\Model\\Transaction\\Manager", true),
"assets": new Service("assets", "Phalcon\\Assets\\Manager", true)
"router": new Service("Phalcon\\Mvc\\Router", true),
"dispatcher": new Service("Phalcon\\Mvc\\Dispatcher", true),
"url": new Service("Phalcon\\Mvc\\Url", true),
"modelsManager": new Service("Phalcon\\Mvc\\Model\\Manager", true),
"modelsMetadata": new Service("Phalcon\\Mvc\\Model\\MetaData\\Memory", true),
"response": new Service("Phalcon\\Http\\Response", true),
"cookies": new Service("Phalcon\\Http\\Response\\Cookies", true),
"request": new Service("Phalcon\\Http\\Request", true),
"filter": new Service("Phalcon\\Filter", true),
"escaper": new Service("Phalcon\\Escaper", true),
"security": new Service("Phalcon\\Security", true),
"crypt": new Service("Phalcon\\Crypt", true),
"annotations": new Service("Phalcon\\Annotations\\Adapter\\Memory", true),
"flash": new Service("Phalcon\\Flash\\Direct", true),
"flashSession": new Service("Phalcon\\Flash\\Session", true),
"tag": new Service("Phalcon\\Tag", true),
"session": new Service("Phalcon\\Session\\Adapter\\Files", true),
"sessionBag": new Service("Phalcon\\Session\\Bag"),
"eventsManager": new Service("Phalcon\\Events\\Manager", true),
"transactionManager": new Service("Phalcon\\Mvc\\Model\\Transaction\\Manager", true),
"assets": new Service("Phalcon\\Assets\\Manager", true)
];
}
}
20 changes: 10 additions & 10 deletions phalcon/di/factorydefault/cli.zep
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ class Cli extends FactoryDefault
parent::__construct();

let this->_services = [
"router": new Service("router", "Phalcon\\Cli\\Router", true),
"dispatcher": new Service("dispatcher", "Phalcon\\Cli\\Dispatcher", true),
"modelsManager": new Service("modelsManager", "Phalcon\\Mvc\\Model\\Manager", true),
"modelsMetadata": new Service("modelsMetadata", "Phalcon\\Mvc\\Model\\MetaData\\Memory", true),
"filter": new Service("filter", "Phalcon\\Filter", true),
"escaper": new Service("escaper", "Phalcon\\Escaper", true),
"annotations": new Service("annotations", "Phalcon\\Annotations\\Adapter\\Memory", true),
"security": new Service("security", "Phalcon\\Security", true),
"eventsManager": new Service("eventsManager", "Phalcon\\Events\\Manager", true),
"transactionManager": new Service("transactionManager", "Phalcon\\Mvc\\Model\\Transaction\\Manager", true)
"router": new Service("Phalcon\\Cli\\Router", true),
"dispatcher": new Service("Phalcon\\Cli\\Dispatcher", true),
"modelsManager": new Service("Phalcon\\Mvc\\Model\\Manager", true),
"modelsMetadata": new Service("Phalcon\\Mvc\\Model\\MetaData\\Memory", true),
"filter": new Service("Phalcon\\Filter", true),
"escaper": new Service("Phalcon\\Escaper", true),
"annotations": new Service("Phalcon\\Annotations\\Adapter\\Memory", true),
"security": new Service("Phalcon\\Security", true),
"eventsManager": new Service("Phalcon\\Events\\Manager", true),
"transactionManager": new Service("Phalcon\\Mvc\\Model\\Transaction\\Manager", true)
];
}
}
40 changes: 9 additions & 31 deletions phalcon/di/service.zep
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Phalcon\Di;

use Phalcon\DiInterface;
use Phalcon\Di\Exception;
use Phalcon\Di\Exception\ServiceResolutionException;
use Phalcon\Di\ServiceInterface;
use Phalcon\Di\Service\Builder;

Expand All @@ -40,9 +41,6 @@ use Phalcon\Di\Service\Builder;
*/
class Service implements ServiceInterface
{

protected _name;

protected _definition;

protected _shared = false;
Expand All @@ -54,23 +52,13 @@ class Service implements ServiceInterface
/**
* Phalcon\Di\Service
*
* @param mixed definition
*/
public final function __construct(string! name, definition, boolean shared = false)
public function __construct(var definition, boolean shared = false)
{
let this->_name = name,
this->_definition = definition,
let this->_definition = definition,
this->_shared = shared;
}

/**
* Returns the service's name
*/
public function getName() -> string
{
return this->_name;
}

/**
* Sets if the service is shared or not
*/
Expand All @@ -90,27 +78,24 @@ class Service implements ServiceInterface
/**
* Sets/Resets the shared instance related to the service
*
* @param mixed sharedInstance
*/
public function setSharedInstance(sharedInstance) -> void
public function setSharedInstance(var sharedInstance) -> void
{
let this->_sharedInstance = sharedInstance;
}

/**
* Set the service definition
*
* @param mixed definition
*/
public function setDefinition(definition) -> void
public function setDefinition(var definition) -> void
{
let this->_definition = definition;
}

/**
* Returns the service definition
*
* @return mixed
*/
public function getDefinition()
{
Expand All @@ -120,10 +105,8 @@ class Service implements ServiceInterface
/**
* Resolves the service
*
* @param array parameters
* @return mixed
*/
public function resolve(parameters = null, <DiInterface> dependencyInjector = null)
public function resolve(var parameters = null, <DiInterface> dependencyInjector = null)
{
boolean found;
var shared, definition, sharedInstance, instance, builder;
Expand Down Expand Up @@ -202,7 +185,7 @@ class Service implements ServiceInterface
* If the service can't be built, we must throw an exception
*/
if found === false {
throw new Exception("Service '" . this->_name . "' cannot be resolved");
throw new ServiceResolutionException();
}

/**
Expand Down Expand Up @@ -254,9 +237,8 @@ class Service implements ServiceInterface
/**
* Returns a parameter in a specific position
*
* @return array
*/
public function getParameter(int position)
public function getParameter(int position) -> array | null
{
var definition, arguments, parameter;

Expand Down Expand Up @@ -292,10 +274,6 @@ class Service implements ServiceInterface
{
var name, definition, shared;

if !fetch name, attributes["_name"] {
throw new Exception("The attribute '_name' is required");
}

if !fetch definition, attributes["_definition"] {
throw new Exception("The attribute '_definition' is required");
}
Expand All @@ -304,6 +282,6 @@ class Service implements ServiceInterface
throw new Exception("The attribute '_shared' is required");
}

return new self(name, definition, shared);
return new self(definition, shared);
}
}
114 changes: 114 additions & 0 deletions phalcon/di/service/serviceregistry.zep
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
namespace Phalcon\Di\Service;

use Phalcon\DiInterface;
use Phalcon\Di\Exception;
use Phalcon\Di\ServiceInterface;
use Phalcon\Config\Adapter\Yaml;

/**
* Phalcon\Di\Service\ServiceRegistry
*
**/
class ServiceRegistry
{

protected di;

/**
*
*/
public function __construct(<DiInterface> di)
{
let this->di = di;
}

/**
*
*/
public function registerDirectory(dirPath)
{
var dirPathItem;

switch typeof dirPath {
case "string":
this->registerDirectoryInternal(dirPath);
break;
case "array":
case "object":
for dirPathItem in iterator(dirPath) {
this->registerDirectoryInternal(dirPathItem);
}
break;
default:
throw new Exception("Invalid service directory variable type.");
break;
}
}

/**
*
*/
protected function registerDirectoryInternal(string! dirPath)
{
var dir, fileinfo, extension, serviceNameSection, forceShared, serviceName;

let dir = new \DirectoryIterator(dirPath);
for fileinfo in iterator(dir) {

// Only handle normal files within a directory.
if (!fileinfo->isFile()) {
continue;
}

let extension = fileinfo->getExtension();
let serviceNameSection = fileinfo->getBasename("." . extension);

let forceShared = substr(serviceNameSection, -7) === "_shared";
let serviceName = forceShared ? substr(serviceNameSection, 0, -7) : serviceNameSection;

this->registerFileInternal(fileinfo->getPathname(), serviceName, extension, forceShared);
}
}

/**
*
*/
protected function registerFileInternal(string! path, string! serviceName, string! extension, boolean forceShared)
{
var serviceDef;

switch extension {
case "php":
let serviceDef = require path;

switch typeof serviceDef {
case "object":
if (serviceDef instanceof ServiceInterface) {
if (forceShared) {
serviceDef->setShared(true);
}
this->di->setRaw(serviceName, serviceDef);
} elseif (is_callable(serviceDef)) {
this->di->set(serviceName, serviceDef, forceShared);
} else {
throw new Exception("Invalid service definition object.");
}
break;
case "array":
case "string":
this->di->set(serviceName, serviceDef, forceShared);
break;
default:
throw new Exception("Invalid service definiton variable type.");
}
break;
case "yml":
let serviceDef = new Yaml(path);
this->di->set(serviceName, serviceDef->toArray(), forceShared);
break;
default:
throw new Exception("Invalid service definition file extension.");
break;
}
}
}
Loading