Skip to content

Commit

Permalink
More refactor updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Spraggs committed Jul 29, 2013
1 parent c990753 commit c1b4a57
Show file tree
Hide file tree
Showing 52 changed files with 254 additions and 31 deletions.
Empty file modified .travis.yml
100755 → 100644
Empty file.
Empty file modified README.md
100755 → 100644
Empty file.
Empty file modified composer.json
100755 → 100644
Empty file.
Empty file modified composer.lock
100755 → 100644
Empty file.
Empty file modified config/controller.config.php
100755 → 100644
Empty file.
15 changes: 13 additions & 2 deletions config/module.config.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@
),

'spiffy_crud' => array(
'forms' => array(),
'models' => array(),
'manager' => array(
'abstract_factories' => array(
'SpiffyCrud\Model\AbstractFactory'
),
),

'models' => array(

),
),

'view_helpers' => array(
Expand All @@ -17,6 +24,10 @@

'view_manager' => array(
'template_map' => array(
'spiffy-crud/controller/read' => __DIR__ . '/../view/spiffy-crud/controller/read.phtml',
'spiffy-crud/controller/create' => __DIR__ . '/../view/spiffy-crud/controller/create.phtml',
'spiffy-crud/controller/update' => __DIR__ . '/../view/spiffy-crud/controller/update.phtml',

'spiffy-crud/crud/create' => __DIR__ . '/../view/spiffy-crud/crud/create.phtml',
'spiffy-crud/crud/details' => __DIR__ . '/../view/spiffy-crud/crud/details.phtml',
'spiffy-crud/crud/form' => __DIR__ . '/../view/spiffy-crud/crud/form.phtml',
Expand Down
Empty file modified config/route.config.php
100755 → 100644
Empty file.
Empty file modified config/service.config.php
100755 → 100644
Empty file.
Empty file modified src/SpiffyCrud/Adapter/AdapterInterface.php
100755 → 100644
Empty file.
Empty file modified src/SpiffyCrud/Adapter/DoctrineObject.php
100755 → 100644
Empty file.
Empty file modified src/SpiffyCrud/Adapter/DoctrineObjectFactory.php
100755 → 100644
Empty file.
161 changes: 161 additions & 0 deletions src/SpiffyCrud/Controller/AbstractCrud.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

namespace SpiffyCrud\Controller;

use SpiffyCrud\CrudManager;
use Zend\Http\Response;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

abstract class AbstractCrud extends AbstractActionController
{
/**
* @var CrudManager
*/
protected $crudManager;

/**
* @return string
*/
abstract public function getModelName();

/**
* @return string
*/
abstract public function getReadRoute();

/**
* @return string
*/
abstract public function getCreateRoute();

/**
* @return string
*/
abstract public function getDeleteRoute();

/**
* @return string
*/
abstract public function getUpdateRoute();

/**
* @return array
*/
public function readAction()
{
$manager = $this->getCrudManager();
$model = $manager->get($this->getModelName());

$viewModel = new ViewModel(array(
'model' => $model,
'name' => $this->getModelName(),
'data' => $manager->findAll($this->getModelName()),
'createRoute' => $this->getCreateRoute(),
'updateRoute' => $this->getUpdateRoute(),
'deleteRoute' => $this->getDeleteRoute(),
));
$viewModel->setTemplate('spiffy-crud/controller/read');
return $viewModel;
}

/**
* @return array|Response
*/
public function createAction()
{
$name = $this->getModelName();
$manager = $this->getCrudManager();
$model = $manager->get($name);
$form = $manager->getForm($name);
$prg = $this->prg();

if ($prg instanceof Response) {
return $prg;
} elseif (false !== $prg) {
$form->setData($prg);

if ($form->isValid()) {
$this->getCrudManager()->persist($name, $form->getData());
return $this->redirect()->toRoute($this->getReadRoute());
}
}

$viewModel = new ViewModel(array(
'model' => $model,
'form' => $form,
'name' => $name,
'createRoute' => $this->getCreateRoute(),
));
$viewModel->setTemplate('spiffy-crud/controller/create');
return $viewModel;
}

/**
* @return array|Response
*/
public function updateAction()
{
$name = $this->getModelName();
$id = $this->params('id');
$manager = $this->getCrudManager();
$entity = $manager->find($name, $id);
$form = $manager->getForm($name, $entity);
$prg = $this->prg();

if ($prg instanceof Response) {
return $prg;
} elseif (false !== $prg) {
$form->setData($prg);

if ($form->isValid()) {
$this->getCrudManager()->persist($name, $entity);
return $this->redirect()->toRoute($this->getReadRoute());
}
}

$viewModel = new ViewModel(array(
'entity' => $entity,
'form' => $form,
'name' => $name,
'id' => $id,
'updateRoute' => $this->getUpdateRoute(),
));
$viewModel->setTemplate('spiffy-crud/controller/update');
return $viewModel;
}

/**
* @return Response
*/
public function deleteAction()
{
$name = $this->getModelName();
$manager = $this->getCrudManager();
$entity = $manager->find($name, $this->params('id'));

$manager->remove($name, $entity);
return $this->redirect()->toRoute($this->getReadRoute());
}

/**
* @param \SpiffyCrud\CrudManager $crudManager
* @return $this
*/
public function setCrudManager($crudManager)
{
$this->crudManager = $crudManager;
return $this;
}

/**
* @return \SpiffyCrud\CrudManager
*/
public function getCrudManager()
{
if (!$this->crudManager instanceof CrudManager) {
$this->crudManager = $this->getServiceLocator()->get('SpiffyCrud\CrudManager');
}
return $this->crudManager;
}
}
Empty file modified src/SpiffyCrud/Controller/Crud.php
100755 → 100644
Empty file.
Empty file modified src/SpiffyCrud/CrudManager.php
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion src/SpiffyCrud/CrudManagerFactory.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function createService(ServiceLocatorInterface $serviceLocator)
/** @var \SpiffyCrud\ModuleOptions $options */
$options = $serviceLocator->get('SpiffyCrud\ModuleOptions');
$adapter = $this->get($options->getAdapter(), $serviceLocator);
$manager = new CrudManager($adapter, new Config($options->getModels()));
$manager = new CrudManager($adapter, new Config($options->getManager()));
$manager->setDefaultHydrator($options->getDefaultHydrator());
$manager->setHydratorManager($serviceLocator->get('HydratorManager'));
$manager->setFormBuilder($this->get($options->getFormBuilder(), $serviceLocator));
Expand Down
Empty file modified src/SpiffyCrud/Exception/ExceptionInterface.php
100755 → 100644
Empty file.
Empty file modified src/SpiffyCrud/Exception/InvalidEntityException.php
100755 → 100644
Empty file.
Empty file modified src/SpiffyCrud/Exception/InvalidModelException.php
100755 → 100644
Empty file.
Empty file modified src/SpiffyCrud/Exception/RuntimeException.php
100755 → 100644
Empty file.
Empty file modified src/SpiffyCrud/Form/Annotation/AnnotationBuilderFactory.php
100755 → 100644
Empty file.
21 changes: 19 additions & 2 deletions src/SpiffyCrud/Model/AbstractFactory.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,31 @@ class AbstractFactory implements AbstractFactoryInterface
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
// TODO: Implement canCreateServiceWithName() method.
return null !== $this->getConfig($serviceLocator, $requestedName);
}

/**
* {@inheritDoc}
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
// TODO: Implement createServiceWithName() method.
$config = $this->getConfig($serviceLocator, $requestedName);
$model = new Model($config);

return $model;
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @return array|null
*/
protected function getConfig(ServiceLocatorInterface $serviceLocator, $name)
{
/** @var \SpiffyCrud\CrudManager $serviceLocator */
$sl = $serviceLocator->getServiceLocator();
$config = $sl->get('Configuration');

return isset($config['spiffy_crud']['models'][$name]) ? $config['spiffy_crud']['models'][$name] : null;
}
}
Empty file modified src/SpiffyCrud/Model/AbstractModel.php
100755 → 100644
Empty file.
8 changes: 8 additions & 0 deletions src/SpiffyCrud/Model/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace SpiffyCrud\Model;

class Model extends AbstractModel
{

}
Empty file modified src/SpiffyCrud/Model/ModelInterface.php
100755 → 100644
Empty file.
Empty file modified src/SpiffyCrud/Module.php
100755 → 100644
Empty file.
19 changes: 10 additions & 9 deletions src/SpiffyCrud/ModuleOptions.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ class ModuleOptions extends AbstractOptions
protected $formBuilder = 'DoctrineORMModule\Form\Annotation\AnnotationBuilder';

/**
* An array of models to register with the crud manager.
* Services to register with the manager.
*
* @var array
*/
protected $models = array();
protected $manager = array();

/**
* An array of forms to register with the form manager for models to use.
* An array of models to register with the crud manager. This is handled by the
* SpiffyCrud\Model\AbstractFactory.
*
* @var array
*/
protected $forms = array() ;
protected $models = array();

/**
* @param string $formBuilder
Expand Down Expand Up @@ -118,20 +119,20 @@ public function getDefaultHydrator()
}

/**
* @param array $forms
* @param array $manager
* @return $this
*/
public function setForms($forms)
public function setManager($manager)
{
$this->forms = $forms;
$this->manager = $manager;
return $this;
}

/**
* @return array
*/
public function getForms()
public function getManager()
{
return $this->forms;
return $this->manager;
}
}
Empty file modified src/SpiffyCrud/ModuleOptionsFactory.php
100755 → 100644
Empty file.
21 changes: 5 additions & 16 deletions src/SpiffyCrud/View/Helper/Datatable.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ public function __construct(CrudManager $manager)
}

/**
* @param $name
* @param array $data
* @return mixed
* {@inheritDoc}
*/
public function __invoke($name, array $data)
public function __invoke($name, array $data, array $options = array())
{
/** @var \SpiffyCrud\Model\ModelInterface $model */
$model = $this->manager->get($name);
Expand All @@ -47,28 +45,19 @@ public function __invoke($name, array $data)
$this->datatable->setOptions(new DatatableOptions($options['options']));
}

$columns = $this->detectColumns($model, $name);
$columns[] = array(
'sTitle' => 'Admin',
'mRender' => 'function(i, j, row) {
return "<a href=\"/crud/'. $name . '/" + row.id + "/update\">edit</a> " +
"<a href=\"/crud/'. $name . '/" + row.id + "/delete\">delete</a>";
}'
);

$this->datatable->setColumns(Collection::factory($columns));
$this->datatable->setColumns(Collection::factory($this->detectColumns($name)));
$this->datatable->setDataResult(new DataResult($data, count($data)));

return $this->getView()->datatable('crudlist', $this->datatable);
}

/**
* @param Model\ModelInterface $model
* @param string $name
* @return array
*/
protected function detectColumns(Model\ModelInterface $model, $name)
protected function detectColumns($name)
{
$model = $this->manager->get($name);
$rendererOptions = $model->getViewOptions();
if (isset($rendererOptions['columns'])) {
return $rendererOptions['columns'];
Expand Down
Empty file modified src/SpiffyCrud/View/Helper/DatatableFactory.php
100755 → 100644
Empty file.
8 changes: 7 additions & 1 deletion src/SpiffyCrud/View/Helper/HelperInterface.php
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@

interface HelperInterface
{
public function __invoke($name, array $data);
/**
* @param string $name
* @param array $data
* @param array $options
* @return string
*/
public function __invoke($name, array $data, array $options = array());
}
Empty file modified test/Bootstrap.php
100755 → 100644
Empty file.
Empty file modified test/SpiffyCrudTest/Asset/AdvancedEntity.php
100755 → 100644
Empty file.
Empty file modified test/SpiffyCrudTest/Asset/IncludedFieldsModel.php
100755 → 100644
Empty file.
Empty file modified test/SpiffyCrudTest/Asset/SimpleEntity.php
100755 → 100644
Empty file.
Empty file modified test/SpiffyCrudTest/Asset/SimpleForm.php
100755 → 100644
Empty file.
Empty file modified test/SpiffyCrudTest/Asset/SimpleModel.php
100755 → 100644
Empty file.
Empty file modified test/SpiffyCrudTest/CrudManagerTest.php
100755 → 100644
Empty file.
Empty file modified test/SpiffyCrudTest/FormManagerTest.php
100755 → 100644
Empty file.
Empty file modified test/SpiffyCrudTest/Mapper/DoctrineObjectTest.php
100755 → 100644
Empty file.
Empty file modified test/SpiffyCrudTest/Mapper/SimpleArrayTest.php
100755 → 100644
Empty file.
Empty file modified test/SpiffyCrudTest/Model/ModelTest.php
100755 → 100644
Empty file.
Empty file modified test/SpiffyCrudTest/ModelManagerTest.php
100755 → 100644
Empty file.
Empty file modified test/SpiffyCrudTest/Service/ManagerCrudFactoryTest.php
100755 → 100644
Empty file.
Empty file modified test/phpunit.xml
100755 → 100644
Empty file.
10 changes: 10 additions & 0 deletions view/spiffy-crud/controller/create.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Create</h1>

<?php echo $this->partial(
'spiffy-crud/crud/form',
array(
'form' => $form,
'url' => $this->url($createRoute)
)
);?>
<a href="<?php echo $this->url('spiffy_crud/details', array('name' => $name)); ?>">Back to List</a>
11 changes: 11 additions & 0 deletions view/spiffy-crud/controller/read.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>Details for <?php echo $model->getDisplayName(); ?></h1>

<p>
<a href="<?php echo $this->url($createRoute); ?>">Create</a>
</p>

<?php echo $this->spiffycrud($name, $data); ?>

<p style="clear:left;">
<a href="<?php echo $this->url($createRoute); ?>">Create</a>
</p>
9 changes: 9 additions & 0 deletions view/spiffy-crud/controller/update.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Update</h1>

<?php echo $this->partial(
'spiffy-crud/crud/form',
array(
'form' => $form,
'url' => $this->url($updateRoute, array('id' => $id))
)
);?>
Empty file modified view/spiffy-crud/crud/create.phtml
100755 → 100644
Empty file.
Empty file modified view/spiffy-crud/crud/details.phtml
100755 → 100644
Empty file.
Empty file modified view/spiffy-crud/crud/form.phtml
100755 → 100644
Empty file.
Empty file modified view/spiffy-crud/crud/index.phtml
100755 → 100644
Empty file.
Empty file modified view/spiffy-crud/crud/update.phtml
100755 → 100644
Empty file.

0 comments on commit c1b4a57

Please sign in to comment.