Created
April 15, 2016 09:03
-
-
Save nask0/e1f2dd6bde4c7b6efae30b019417899e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CloseContacts\Lib\DataServices; | |
use Phalcon\Di\InjectionAwareInterface; | |
class DataServiceFactory implements InjectionAwareInterface | |
{ | |
/* @var \Phalcon\Di\FactoryDefault - Dependency injector instance */ | |
protected $_di; | |
/* @var string $_nsPrefix Data Services namespace */ | |
private $_nsPrefix = __NAMESPACE__ . '\\'; | |
/** | |
* @return \Phalcon\Di\FactoryDefault | |
*/ | |
public function getDI() | |
{ | |
return $this->_di; | |
} | |
/** | |
* @param \Phalcon\DiInterface $dependencyInjector | |
*/ | |
public function setDI( \Phalcon\DiInterface $dependencyInjector ) | |
{ | |
$this->_di = $dependencyInjector; | |
} | |
/** | |
* Load and return \Api\Lib\Data\Services\DataServiceAbstract instance. | |
* | |
* @param string $dataService | |
* @return \CloseContacts\Lib\DataServices\DataServiceAbstract instance | |
*/ | |
public function load( $dataService ) | |
{ | |
try { | |
$serviceName = ucfirst( (string) $dataService ); | |
$serviceClass = $this->_nsPrefix . $serviceName; | |
if ( class_exists( $serviceClass ) ) { | |
$diServiceName = 'dataService' . $serviceName; | |
if ( $this->getDI()->has( $diServiceName ) ) { | |
return $this->getDI()->get( $diServiceName ); | |
} else { | |
$dataServiceInstance = new $serviceClass( | |
$this->getDI()->get( 'modelsManager'), | |
$this->getDI()->get( 'transactions' ), | |
$this->getDI()->get( 'validation' ) | |
); | |
$this->getDI()->set( $diServiceName, $dataServiceInstance, true ); | |
return $dataServiceInstance; | |
} | |
} | |
return null; | |
} catch ( \Exception $e ) { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment