2014-11-04 09:05:21 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: Administrator
|
|
|
|
* Date: 2014/11/4
|
|
|
|
* Time: 10:39
|
|
|
|
*/
|
|
|
|
namespace Westdc\Service;
|
|
|
|
|
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface;
|
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface;
|
|
|
|
|
|
|
|
class ServiceFactory implements AbstractFactoryInterface{
|
|
|
|
|
2014-12-18 15:25:15 +00:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2014-11-04 09:05:21 +00:00
|
|
|
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
|
|
|
|
{
|
2014-12-18 15:25:15 +00:00
|
|
|
if(!is_array($this->invokedService))
|
|
|
|
throw new \RuntimeException('lazy services not found');
|
|
|
|
|
|
|
|
if(in_array($requestedName , $this->invokedNames))
|
|
|
|
{
|
|
|
|
$this->currentServiceType = "lazy";
|
|
|
|
return true;
|
|
|
|
}
|
2014-11-04 09:05:21 +00:00
|
|
|
|
|
|
|
$serviceAgentDir = __DIR__ . "/ServiceAgent";
|
|
|
|
|
|
|
|
if(is_dir($serviceAgentDir))
|
|
|
|
{
|
|
|
|
if(false != ($handle = opendir($serviceAgentDir)))
|
|
|
|
{
|
|
|
|
while(false !== ($file = readdir($handle)))
|
|
|
|
{
|
|
|
|
if(substr($file,0,strlen($file)-4) == (string)$requestedName) {
|
2014-12-18 15:25:15 +00:00
|
|
|
$this->currentServiceType = "agent";
|
2014-11-04 09:05:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
|
|
|
|
{
|
2014-12-18 15:25:15 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
$service = new $serviceName;
|
2014-11-04 09:05:21 +00:00
|
|
|
|
2014-12-18 15:25:15 +00:00
|
|
|
$service->WESTDC_SERVICE_TYPE = "agent";
|
|
|
|
$service->WESTDC_SERVICE_NAME = $requestedName;
|
2014-11-04 09:05:21 +00:00
|
|
|
|
2014-12-18 15:25:15 +00:00
|
|
|
return $service;
|
|
|
|
|
|
|
|
}
|
2014-11-04 09:05:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-12-18 15:25:15 +00:00
|
|
|
|
|
|
|
|
2014-11-04 09:05:21 +00:00
|
|
|
}
|