From 1ab6b8bbf31b2dd5dc19adfbdd756f60cadfa6fd Mon Sep 17 00:00:00 2001 From: Jianxuan Li Date: Thu, 18 Dec 2014 23:25:15 +0800 Subject: [PATCH] add load lazy service function in Westdc/Service/ServiceFactory --- Westdc/Metadata/Metadata.php | 32 ++++++++++++++++- Westdc/Service/ServiceFactory.php | 49 +++++++++++++++++++++++--- Westdc/Service/service.lazy.config.php | 4 +++ 3 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 Westdc/Service/service.lazy.config.php diff --git a/Westdc/Metadata/Metadata.php b/Westdc/Metadata/Metadata.php index eb57df6..8f3a9e5 100644 --- a/Westdc/Metadata/Metadata.php +++ b/Westdc/Metadata/Metadata.php @@ -10,10 +10,40 @@ namespace Westdc\Metadata; use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\ServiceManagerAwareInterface; +use Westdc\EventModel\AbstractEventManager; + +class Metadata extends AbstractEventManager implements ServiceManagerAwareInterface{ + + protected $serviceManager; + + private $db; + + public function setServiceManager(ServiceManager $serviceManager) + { + $this->serviceManager = $serviceManager; + + $this->init(); + + return $this; + } + + private function init() + { + $dbService = $this->serviceManager->get('Db'); + $this->db = $dbService->getPdo(); + } -class Metadata implements ServiceManagerAwareInterface{ + public function delete($uuid) + { + $tools = $this->serviceManager->get('Tools'); + + if( false == $tools->isUUID($uuid) ) + return "参数错误"; + + return true; + }//delete 删除元数据 } \ No newline at end of file diff --git a/Westdc/Service/ServiceFactory.php b/Westdc/Service/ServiceFactory.php index ba8633a..3f5ed3b 100644 --- a/Westdc/Service/ServiceFactory.php +++ b/Westdc/Service/ServiceFactory.php @@ -12,8 +12,31 @@ use Zend\ServiceManager\AbstractFactoryInterface; class ServiceFactory implements AbstractFactoryInterface{ + private $invokedService; + private $invokedNames; + private $currentServiceType; + + function __construct() + { + $this->invokedService = $this->getInvokedServiceFromConfig(); + $this->invokedNames = array_keys($this->invokedService); + } + + private function getInvokedServiceFromConfig() + { + return include dirname(__FILE__) . "/service.lazy.config.php"; + } + public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { + if(!is_array($this->invokedService)) + throw new \RuntimeException('lazy services not found'); + + if(in_array($requestedName , $this->invokedNames)) + { + $this->currentServiceType = "lazy"; + return true; + } $serviceAgentDir = __DIR__ . "/ServiceAgent"; @@ -24,6 +47,7 @@ class ServiceFactory implements AbstractFactoryInterface{ while(false !== ($file = readdir($handle))) { if(substr($file,0,strlen($file)-4) == (string)$requestedName) { + $this->currentServiceType = "agent"; return true; } } @@ -36,13 +60,30 @@ class ServiceFactory implements AbstractFactoryInterface{ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) { - $serviceName = __NAMESPACE__ . "\\ServiceAgent\\" . $requestedName; + switch($this->currentServiceType) + { + case 'lazy': + $service = new $this->invokedService[$requestedName]; - $service = new $serviceName; + $service->WESTDC_SERVICE_TYPE = "lazy"; + $service->WESTDC_SERVICE_NAME = $requestedName; - $service->name = $requestedName; + return $service; + + case 'agent': + $serviceName = __NAMESPACE__ . "\\ServiceAgent\\" . $requestedName; + + $service = new $serviceName; + + $service->WESTDC_SERVICE_TYPE = "agent"; + $service->WESTDC_SERVICE_NAME = $requestedName; + + return $service; + + } - return $service; } + + } \ No newline at end of file diff --git a/Westdc/Service/service.lazy.config.php b/Westdc/Service/service.lazy.config.php new file mode 100644 index 0000000..7fce29b --- /dev/null +++ b/Westdc/Service/service.lazy.config.php @@ -0,0 +1,4 @@ + 'Westdc\Helpers\Tools' +); \ No newline at end of file