Skip to content
This repository has been archived by the owner on Oct 11, 2020. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
fagundes committed Mar 16, 2016
2 parents 6681a31 + 2b3ee87 commit 5808ab8
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 128 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -3,8 +3,8 @@
[![Coverage Status](https://coveralls.io/repos/fagundes/ZffBase/badge.svg?branch=develop&service=github)](https://coveralls.io/github/fagundes/ZffBase?branch=develop)

[![Latest Stable Version](https://img.shields.io/packagist/v/fagundes/zff-base.svg)](https://packagist.org/packages/fagundes/zff-base)
[![Build Status](https://travis-ci.org/fagundes/ZffBase.svg?branch=0.1.5)](https://travis-ci.org/fagundes/ZffBase)
[![Coverage Status](https://coveralls.io/repos/fagundes/ZffBase/badge.svg?branch=0.1.5&service=github)](https://coveralls.io/github/fagundes/ZffBase?branch=0.1.5)
[![Build Status](https://travis-ci.org/fagundes/ZffBase.svg?branch=0.1.6)](https://travis-ci.org/fagundes/ZffBase)
[![Coverage Status](https://coveralls.io/repos/fagundes/ZffBase/badge.svg?branch=0.1.6&service=github)](https://coveralls.io/github/fagundes/ZffBase?branch=0.1.6)

[![Total Downloads](https://poser.pugx.org/fagundes/zff-base/downloads)](https://packagist.org/packages/fagundes/zff-base) [![License](https://poser.pugx.org/fagundes/zff-base/license)](https://packagist.org/packages/fagundes/zff-base)

Expand Down
28 changes: 15 additions & 13 deletions config/module.config.php
Expand Up @@ -2,19 +2,19 @@

namespace Zff\Base;

if(!defined('CSS_DIR')) {
if (!defined('CSS_DIR')) {
define('CSS_DIR', __DIR__ . '/../view/public/css');
}

if(!defined('JS_DIR')) {
if (!defined('JS_DIR')) {
define('JS_DIR', __DIR__ . '/../view/public/js');
}

return [
'router' => [
'router' => [
'router_class' => Mvc\Router\ControllerRouteStack::class,
],
'view_manager' => [
'view_manager' => [
'template_path_stack' => [
'zff-base' => __DIR__ . '/../view',
],
Expand All @@ -25,7 +25,7 @@
'element/paginator' => __DIR__ . '/../view/base/element/paginator.phtml',
],
],
'asset_manager' => [
'asset_manager' => [
'resolver_configs' => [
'map' => [
//css
Expand All @@ -36,7 +36,7 @@
],
],
],
'form_elements' => [
'form_elements' => [
'invokables' => [
'bstext' => Form\Element\BsText::class,
'bstextarea' => Form\Element\BsTextarea::class,
Expand All @@ -49,13 +49,13 @@
'bsobjectradio' => Form\Element\BsObjectRadio::class,
],
],
'validators' => [
'validators' => [
'invokables' => [
'cpf' => Validator\Cpf::class,
'cpf' => Validator\Cpf::class,
'cnpj' => Validator\Cnpj::class,
],
],
'view_helpers' => [
'view_helpers' => [
'invokables' => [
//bs form helpers
'bsform' => Form\View\Helper\BsForm::class,
Expand All @@ -76,13 +76,15 @@
],
],
'service_manager' => [
'invokables' => [
Service\Table\TableHandler::class => Service\Table\TableHandler::class,
],
'abstract_factories' => [
Form\FormAbstractFactory::class,
Form\InputFilterAbstractFactory::class,
Service\ServiceAbstractFactory::class
Service\ServiceAbstractFactory::class,
],
],
'controllers' => [
'abstract_factories' => [
Controller\ControllerAbstractFactory::class,
],
],
];
39 changes: 39 additions & 0 deletions src/Controller/AbstractController.php
Expand Up @@ -23,6 +23,45 @@ class AbstractController extends AbstractActionController
*/
private $postedData;

/**
* @var array
*/
protected $forms;

/**
* @var array
*/
protected $tables;

/**
* @var array
*/
protected $services;

/**
* @return array list of form's names
*/
public function getForms()
{
return $this->forms;
}

/**
* @return array list of table's names
*/
public function getTables()
{
return $this->tables;
}

/**
* @return array list of service's names
*/
public function getServices()
{
return $this->services;
}

protected function getPostedData()
{
if (is_null($this->postedData)) {
Expand Down
106 changes: 106 additions & 0 deletions src/Controller/ControllerAbstractFactory.php
@@ -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);
}
}
1 change: 0 additions & 1 deletion src/Entity/AbstractEntity.php
Expand Up @@ -29,5 +29,4 @@ public function exchangeArray($data)
{
$this->configure($data);
}

}
4 changes: 2 additions & 2 deletions src/Hydrator/DoctrineObject.php
Expand Up @@ -54,7 +54,7 @@ protected function handleTypeConversions($value, $typeOfField)
$dateTime->setTimestamp($value);
$value = $dateTime;
} elseif (is_string($value)) {
$value = $this->getDateFormat() ?
$value = $this->getDateFormat() ?
\DateTime::createFromFormat($this->getDateFormat(), $value) :
new DateTime($value);
}
Expand All @@ -65,4 +65,4 @@ protected function handleTypeConversions($value, $typeOfField)

return $value;
}
}
}
2 changes: 1 addition & 1 deletion src/Module.php
Expand Up @@ -63,4 +63,4 @@ public function getViewHelperConfig()
],
];
}
}
}
65 changes: 18 additions & 47 deletions src/Service/AbstractService.php
Expand Up @@ -11,6 +11,7 @@
use Doctrine\ORM\Tools\Pagination\Paginator;
use Zend\Db\Adapter\Adapter;
use Zff\Base\Exception;
use Zff\Base\Table\TableHandler;

/**
* AbstractService
Expand Down Expand Up @@ -60,7 +61,7 @@ abstract class AbstractService
protected $services;

/**
* @var Table\TableHandler
* @var TableHandler
*/
protected $tableHandler;

Expand Down Expand Up @@ -144,33 +145,19 @@ public function setDbAdapterName($dbAdapterName)
$this->dbAdapterName = $dbAdapterName;
}

/**
* @return TableHandler
*/
public function getTableHandler()
{
return $this->tableHandler;
}

public function setTableHandler(Table\TableHandler $tableHandler)
public function setTableHandler(TableHandler $tableHandler)
{
$this->tableHandler = $tableHandler;
}

public function getTableClassName()
{
if (!$this->tableClassName) {
$reflectionFinalClass = new \ReflectionClass($this);
$this->tableClassName = $reflectionFinalClass->getNamespaceName()
. '\\Table\\'
. $reflectionFinalClass->getShortName();
}
return $this->tableClassName;
}

public function setTableClassName($tableClassName)
{
$this->tableClassName = $tableClassName;
return $this;
}

public function getAutocommit()
{
return $this->autocommit;
Expand Down Expand Up @@ -428,8 +415,8 @@ public function getPaginator(
$currentPageNumber = null,
$itemCountPerPage
= null
)
{
) {


$this->setPagination($currentPageNumber, $itemCountPerPage);
$query->setFirstResult($this->firstResult)
Expand All @@ -444,23 +431,6 @@ public function getFindAllQueryBuilder()
return $qb;
}

/**
* @param array|mixed $data
*
* @return Table\AbstractTable
*/
public function createTable($data)
{
$tableHandler = $this->getTableHandler();

$table = $tableHandler->createTable($this->getTableClassName());
$form = $table->getForm();

$form->setData($data);

return $table;
}

/**
* @param \ZfTable\AbstractTable $table
* @param \Doctrine\ORM\QueryBuilder $queryBuilder
Expand All @@ -476,16 +446,17 @@ public function renderTable(\ZfTable\AbstractTable $table, \Doctrine\ORM\QueryBu
return $table->render();
}

public function executeTable($data, \Doctrine\ORM\QueryBuilder $queryBuilder)
/**
* Method proxy to \Zff\Base\Table\TableHandler::executeTable
*
* @param $data
* @param \ZfTable\AbstractTable $table
* @param \Doctrine\ORM\QueryBuilder $queryBuilder
* @return bool|string if the form is invalid or the html resulting
*/
public function executeTable($data,\ZfTable\AbstractTable $table, \Doctrine\ORM\QueryBuilder $queryBuilder)
{

$table = $this->createTable($data);
$form = $table->getForm();

if ($form->isValid()) {
return $this->renderTable($table, $queryBuilder);
}
return false;
return $this->getTableHandler()->executeTable($data, $table, $queryBuilder);
}

protected function checkIfClassExists($class)
Expand Down

0 comments on commit 5808ab8

Please sign in to comment.