add load lazy service function in Westdc/Service/ServiceFactory

This commit is contained in:
Jianxuan Li 2014-12-18 23:25:15 +08:00
parent f693f1cf45
commit 1ab6b8bbf3
3 changed files with 80 additions and 5 deletions

View File

@ -10,10 +10,40 @@ namespace Westdc\Metadata;
use Zend\ServiceManager\ServiceManager; use Zend\ServiceManager\ServiceManager;
use Zend\ServiceManager\ServiceManagerAwareInterface; 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 删除元数据
} }

View File

@ -12,8 +12,31 @@ use Zend\ServiceManager\AbstractFactoryInterface;
class ServiceFactory implements 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) 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"; $serviceAgentDir = __DIR__ . "/ServiceAgent";
@ -24,6 +47,7 @@ class ServiceFactory implements AbstractFactoryInterface{
while(false !== ($file = readdir($handle))) while(false !== ($file = readdir($handle)))
{ {
if(substr($file,0,strlen($file)-4) == (string)$requestedName) { if(substr($file,0,strlen($file)-4) == (string)$requestedName) {
$this->currentServiceType = "agent";
return true; return true;
} }
} }
@ -36,13 +60,30 @@ class ServiceFactory implements AbstractFactoryInterface{
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{ {
switch($this->currentServiceType)
{
case 'lazy':
$service = new $this->invokedService[$requestedName];
$service->WESTDC_SERVICE_TYPE = "lazy";
$service->WESTDC_SERVICE_NAME = $requestedName;
return $service;
case 'agent':
$serviceName = __NAMESPACE__ . "\\ServiceAgent\\" . $requestedName; $serviceName = __NAMESPACE__ . "\\ServiceAgent\\" . $requestedName;
$service = new $serviceName; $service = new $serviceName;
$service->name = $requestedName; $service->WESTDC_SERVICE_TYPE = "agent";
$service->WESTDC_SERVICE_NAME = $requestedName;
return $service; return $service;
} }
}
} }

View File

@ -0,0 +1,4 @@
<?php
return array(
'Tools' => 'Westdc\Helpers\Tools'
);