This repository has been archived by the owner on Oct 11, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
228 additions
and
128 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
/** | ||
* @license http://opensource.org/licenses/MIT MIT | ||
* @copyright Copyright (c) 2015 Vinicius Fagundes | ||
*/ | ||
|
||
namespace Zff\Base\Controller; | ||
|
||
use Zend\ServiceManager\AbstractFactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
|
||
/** | ||
* Controller Abstract Factory | ||
* | ||
* @package ZffBase | ||
* @subpackage ZffBase_Controller | ||
*/ | ||
class ControllerAbstractFactory implements AbstractFactoryInterface | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) | ||
{ | ||
if (class_exists($requestedName)) { | ||
$reflect = new \ReflectionClass($requestedName); | ||
if ($reflect->isSubclassOf(AbstractController::class)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function createServiceWithName( | ||
ServiceLocatorInterface $serviceLocator, | ||
$name, | ||
$requestedName | ||
) | ||
{ | ||
|
||
if ($this->canCreateServiceWithName($serviceLocator, $name, $requestedName)) { | ||
/** | ||
* @var AbstractController $controller | ||
*/ | ||
$reflect = new \ReflectionClass($requestedName); | ||
$controller = $reflect->newInstance(); | ||
|
||
$forms = (array)$controller->getForms(); | ||
foreach ($forms as $customFormName => $formName) { | ||
$this->loadObject($serviceLocator, $controller, $formName, $customFormName, 'form'); | ||
} | ||
|
||
$tables = (array)$controller->getTables(); | ||
foreach ($tables as $customTableName => $tableName) { | ||
$this->loadObject($serviceLocator, $controller, $tableName, $customTableName, 'table'); | ||
} | ||
|
||
$services = (array)$controller->getServices(); | ||
foreach ($services as $customServiceName => $serviceName) { | ||
$this->loadObject($serviceLocator, $controller, $serviceName, $customServiceName, 'service'); | ||
} | ||
|
||
return $controller; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
protected function loadObject( | ||
ServiceLocatorInterface $serviceLocator, | ||
AbstractController $controller, | ||
$objectNeededName, | ||
$customObjectName, | ||
$sufixName | ||
) | ||
{ | ||
$mainServiceLocator = $serviceLocator->getServiceLocator(); | ||
|
||
if (is_int($customObjectName)) { | ||
$simpleObjectName = preg_replace('/(.*)\\\/', '', $objectNeededName); | ||
} else { | ||
$simpleObjectName = ucfirst($customObjectName); | ||
} | ||
|
||
$methodSet = 'set' . $simpleObjectName . ucfirst($sufixName); | ||
|
||
if (!method_exists($controller, $methodSet)) { | ||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'Expected method %s::%s, to set the %s %s.', | ||
get_class($controller), | ||
$methodSet, | ||
$sufixName, | ||
$objectNeededName | ||
) | ||
); | ||
} | ||
|
||
$objectNeeded = $mainServiceLocator->get($objectNeededName); | ||
call_user_func([$controller, $methodSet], $objectNeeded); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,5 +29,4 @@ public function exchangeArray($data) | |
{ | ||
$this->configure($data); | ||
} | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,4 +63,4 @@ public function getViewHelperConfig() | |
], | ||
]; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.