Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Optimisation of configuration file
Browse files Browse the repository at this point in the history
You can now specify only required information. DBM load automatically your entity and your form type without fullPath and other details.
Permissions of all entities are more efficiently managed and easier to configure.
Now, DBM can also configure your views with default settings.
  • Loading branch information
hugo082 committed Feb 21, 2017
1 parent 676a66e commit b7f1f4d
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 76 deletions.
10 changes: 5 additions & 5 deletions Controller/ManageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public function listAction(Request $request, $name)
$settings = $this->container->getParameter('db_manager.views');
$einfo = $this->get('db.manager.checker')->getEntity($array, $name);

$e = new $einfo['fullpath']();
$e = new $einfo['fullPath']();
$all = $this->getEntity($einfo);

$form = NULL;
if ($settings['list']['add'] && $einfo['permission']['add']) {
$form = $this->createForm($einfo['formtype'], $e);
$form = $this->createForm($einfo['fullFormType'], $e);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
Expand Down Expand Up @@ -57,8 +57,8 @@ public function addAction(Request $request, $name)
if ($settings['list']['add'] || !$einfo['permission']['add'])
return $this->redirectToRoute('db.manager.list', array('name' => $name));

$e = new $einfo['fullpath']();
$form = $this->createForm($einfo['formtype'], $e);
$e = new $einfo['fullPath']();
$form = $this->createForm($einfo['fullFormType'], $e);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
Expand Down Expand Up @@ -90,7 +90,7 @@ public function editAction(Request $request, $name, $id)
$this->addFlash('danger','Entity not found');
return $this->redirectToRoute('db.manager.list', array('name' => $name));
}
$form = $this->createForm($einfo['formtype'], $e);
$form = $this->createForm($einfo['fullFormType'], $e);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
Expand Down
85 changes: 46 additions & 39 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DB\ManagerBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -12,6 +13,8 @@
*/
class Configuration implements ConfigurationInterface
{
public $permissions = array('add', 'edit', 'remove');

/**
* {@inheritdoc}
*/
Expand All @@ -20,62 +23,66 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('db_manager');

// $rootNode
// ->children()
// ->arrayNode('twitter')
// ->children()
// ->integerNode('client_id')->end()
// ->scalarNode('client_secret')->end()
// ->end()
// ->end() // twitter
// ->end()
// ;
$this->addEntitiesSection($rootNode);
$this->addViewsSection($rootNode);

//$supportedValues = array('invalid','nulle');
return $treeBuilder;
}

$rootNode
/**
* @param ArrayNodeDefinition $node
*/
private function addViewsSection(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('entities')
->prototype('array')
->children()
->scalarNode('name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('bundle')->isRequired()->cannotBeEmpty()->end()
->scalarNode('fullpath')->isRequired()->cannotBeEmpty()->end()
->scalarNode('formtype')->isRequired()->cannotBeEmpty()->end()
->arrayNode('permission')
->children()
->booleanNode('add')->isRequired()->defaultTrue()->end()
->booleanNode('edit')->isRequired()->defaultTrue()->end()
->booleanNode('remove')->isRequired()->defaultTrue()->end()
->end()
->end()
->end()
->end()
->end()
->arrayNode('views')
->addDefaultsIfNotSet()
->canBeUnset()
->children()
->arrayNode('list')
->addDefaultsIfNotSet()
->children()
->booleanNode('add')->isRequired()->defaultTrue()->end()
->booleanNode('add')->defaultTrue()->end()
->end()
->end()
->arrayNode('edit')
->addDefaultsIfNotSet()
->children()
->booleanNode('list')->isRequired()->defaultTrue()->end()
->booleanNode('list')->defaultTrue()->end()
->end()
->end()
->end()
->end()
// ->scalarNode('client_id')
// ->validate()
// ->ifNotInArray($supportedValues)
// ->thenInvalid('c\'est mal')
// ->end()
// ->end()
->end()
;
}


return $treeBuilder;
/**
* @param ArrayNodeDefinition $node
*/
private function addEntitiesSection(ArrayNodeDefinition $node)
{
$node
->children()
->arrayNode('entities')
->prototype('array')
->children()
->scalarNode('name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('bundle')->isRequired()->cannotBeEmpty()->end()
->scalarNode('fullPath')->end() // Auto
->scalarNode('formType')->end() // Auto
->scalarNode('fullFormType')->end() // Auto
->arrayNode('permission')
->defaultValue($this->permissions)
->prototype('enum')
->values($this->permissions)
->end()
->end()
->end()
->end()
->end()
->end()
;
}
}
44 changes: 42 additions & 2 deletions DependencyInjection/DBManagerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,50 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter($this->getAlias().'.entities', $config['entities']);
$container->setParameter($this->getAlias().'.views', $config['views']);

$this->loadViews($config, $container);
$this->loadEntities($config, $container, $configuration->permissions);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}

/**
* @param array $config
* @param ContainerBuilder $container
*/
private function loadViews(array $config, ContainerBuilder $container)
{
if (isset($config['views']))
$container->setParameter($this->getAlias().'.views', $config['views']);
}

/**
* @param array $config
* @param ContainerBuilder $container
* @param array $permissions
*/
private function loadEntities(array $config, ContainerBuilder $container, array $permissions)
{
foreach ($config['entities'] as $name => $values) {
if (!isset($values['fullPath']))
$values['fullPath'] = $values['bundle']."\\Entity\\".$values['name'];

if (!isset($values['formType']))
$values['formType'] = $values['name'] . "Type";

if (!isset($values['fullFormType']))
$values['fullFormType'] = $values['bundle'] . "\\Form\\" . $values['formType'];

$tmp = array();
foreach ($permissions as $p)
$tmp[$p] = in_array($p, $values['permission']);
$values['permission'] = $tmp;

$config['entities'][$name] = $values;
}

if (isset($config['views']))
$container->setParameter($this->getAlias().'.entities', $config['entities']);
}
}
48 changes: 48 additions & 0 deletions Model/BaseManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace DB\ManagerBundle\Model;

use DB\ManagerBundle\Model\ListingEntityInterface;
use Doctrine\ORM\Mapping as ORM;

/**
* Storage agnostic manager full compatible object.
*
* @author Hugo Fouquet <[email protected]>
*/
abstract class BaseManager implements ListingEntityInterface
{
private $bloquedMethods = array('getVars', 'getProperties');

/**
* @return All properties values
*/
public function getVars(){
return $this->getProp(true);
}

/**
* @return All properties names
*/
public function getProperties(){
return $this->getProp(false);
}

private function getProp(bool $execute) {
$reflect = new \ReflectionClass($this);
$methods = $reflect->getMethods(\ReflectionProperty::IS_PUBLIC);

$tmp = array();
foreach ($methods as $met) {
$metName = $met->getName();
if (0 === strpos($metName, 'get') && !in_array($metName, $this->bloquedMethods)) {
if ($execute)
$tmp[] = $this->$metName();
else
$tmp[] = str_replace('get', '', $metName);
}
}
return $tmp;
}
}

9 changes: 9 additions & 0 deletions Model/ListingEntityInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace DB\ManagerBundle\Model;

interface ListingEntityInterface
{
public function getVars();
public function getProperties();
}
76 changes: 58 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# Database Manager Web Interface

DBManager is a web interface generator that help you to implement a database
DBManager (DBM) is a web interface generator that help you to implement a database
manager on your website.

Features include:
Expand All @@ -11,7 +11,7 @@ Features include:
* Remove
* Personalize interface

`v1.0` `21 FEV 17`
`v1.1` `21 FEV 17`

## Installation

Expand Down Expand Up @@ -47,28 +47,23 @@ Enable the bundle in the kernel :
{
$bundles = array(
// ...
new FOS\UserBundle\FOSUserBundle()
new DB\ManagerBundle\DBManagerBundle()
);
}

Update your `config.yml` :
Update your `routing.yml` :

db.manager:
resource: "@DBManagerBundle/Resources/config/routing.yml"
prefix: /database

Set up your `config.yml` :

db_manager:
views:
list:
add: false
edit:
list: false
entities:
DisplayName: # Display and URL name
DisplayName:
name: RealName
bundle: AppBundle
fullpath: AppBundle\Entity\RealName
formtype: AppBundle\Form\RealNameType
permission:
add: true
edit: true
remove: false
bundle: YourBundle

## About

Expand All @@ -77,4 +72,49 @@ See also the [creator](https://github.com/hugo082).

## License

This bundle is under the MIT license. See the complete license [in the bundle](LICENSE)
This bundle is under the MIT license. See the complete license [in the bundle](LICENSE)

## Documentation

1. Add an entity

DBM load your entities with your configuration file. You can specify an entity to follow by adding it in your config.yml

db_manager:
entities:
DisplayName:
name: RealName
bundle: YourBundle

You can configure different actions on each entity :

DisplayName:
name: RealName
bundle: YourBundle
fullPath: AppBundle\Entity\Airport # Optional
formType: AirportEditType # Optional
fullFormType: AnotherBundle\Form\AirportEditType # Optional
permission: [ "edit" ] # Optional - add | edit | remove

By default, DBM load your entity in `YourBundle\Entity\RealName`, name the form with `RealNameType` and load your form type in
`YourBundle\Form\formType` (so `YourBundle\Form\RealNameType`)
`DisplayName` is used by DBM for display on template and in url, you can enter the same name of RealName.

2. Configure views

You can configure your views by adding the `views` keyword in your configuration file.

db_manager:
views: ~

By default, DBM insert the add form in listing view and list in edit view. But if you want to separate all views, you can
set the options to `false`.

db_manager:
views:
list:
add: false
edit:
list: false

If you do not specify an argument, the argument takes its default value (`true`)
14 changes: 13 additions & 1 deletion Resources/doc/index.rst
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
Comin soon
Comin soon

Amélioration du config.yml
name and bundle in same

Ajout du routing dans le README

Gérer bdd vide

template for listing in config

Entity:
formtype: #Optional
Loading

0 comments on commit b7f1f4d

Please sign in to comment.