Skip to content

Commit

Permalink
Merge pull request #221 from symfony2admingenerator/feat-credentials
Browse files Browse the repository at this point in the history
Use credentials back instead of groups
  • Loading branch information
sescandell committed Jan 4, 2016
2 parents 1ca35d9 + e940adf commit f29b802
Show file tree
Hide file tree
Showing 69 changed files with 1,070 additions and 973 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ php:
- 5.3
- 5.4
- 5.5

env:
- SYMFONY_VERSION=origin/2.6
- 5.6
- 7.0

before_script:
- composer self-update
Expand Down
11 changes: 11 additions & 0 deletions Builder/Admin/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ public function setColumnClass($columnClass)
}

/**
* @param Column $column
* @param string $optionName
* @param string $default
*
* @return string
*/
protected function getFieldOption(Column $column, $optionName, $default = null)
{
Expand Down Expand Up @@ -405,6 +409,13 @@ protected function setUserActionConfiguration(Action $action)
$action->setProperty($option, $value);
}
}

if ('generic' == $action->getType()) {
// Let's try to get credentials from builder for consistency
if ($credentials = $this->generator->getFromYaml(sprintf('builders.%s.params.credentials', $action->getName()))) {
$action->setCredentials($credentials);
}
}
}

protected function addAction(Action $action)
Expand Down
18 changes: 9 additions & 9 deletions Builder/Admin/ListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,21 @@ protected function addFilterColumn(Column $column)
$this->filterColumns[$column->getName()] = $column;
}

public function getFilterColumnGroups()
public function getFilterColumnsCredentials()
{
$groups = array();
$credentials = array();

foreach ($this->getFilterColumns() as $column) {
$columnGroups = $column->getFiltersGroups();
// If one column has no Group constraint, we always
// have to display the filter panel
if (empty($columnGroups)) {
foreach($this->getFilterColumns() as $column) {
if (! $filterCredentials = $column->getFiltersCredentials()) {
// If one column has no Credentials constraint, we always
// have to display the filter panel
return array();
}
$groups = array_merge($groups, $columnGroups);

$credentials[] = $filterCredentials;
}

return $groups;
return $credentials;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion Builder/BaseBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public function getVariables()
* (non-PHPdoc)
* @see Builder/Admingenerator\GeneratorBundle\Builder.BuilderInterface::hasVariable()
* @param string $key
* @return bool
*/
public function hasVariable($key)
{
Expand Down Expand Up @@ -169,7 +170,7 @@ public function getModelClass()
/**
* Set the generator.
*
* @param \Admingenerator\GeneratorBundle\Builder\Generator $generator A generator.
* @param \TwigGenerator\Builder\Generator $generator A generator.
*/
public function setGenerator(GenericBaseGenerator $generator)
{
Expand Down
101 changes: 54 additions & 47 deletions Builder/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,44 +118,50 @@ public function addBuilder(BuilderInterface $builder)
protected function mergeParameters(array $global, array $builder)
{
foreach ($global as $param => &$value) {
if (array_key_exists($param, $builder)) {
if (in_array($param, array('fields', 'actions', 'object_actions', 'batch_actions'))) {
// Grab builder configuration only if defined
if (!is_null($builder[$param])) {
$configurations = array();
foreach ($builder[$param] as $name => $configuration) {
if (is_array($configuration) || is_null($configuration)) {
if (!is_null($value) && array_key_exists($name, $value)) {
$configurations[$name] = $configuration
? $this->mergeConfiguration($value[$name], $configuration) // Override definition
: $value[$name]; // Configuration is null => use global definition
} else {
// New definition (new field, new action) from builder
$configurations[$name] = $configuration;
}
} else {
throw new \InvalidArgumentException(
sprintf('Invalid %s "%s" builder definition for %s', $param, $name, $this->getFromYaml('params.model'))
);
}
}

if (in_array($param, array('actions', 'object_actions', 'batch_actions'))) {
// Actions list comes from builder
$value = $configurations;
} else {
// All fields are still available in a builder
$value = array_merge($value ?:array(), $configurations);
}
}
if (!array_key_exists($param, $builder)) {
continue;
}

if (!in_array($param, array('fields', 'actions', 'object_actions', 'batch_actions'))) {
if (is_array($value)) {
$value = $this->recursiveReplace($value, $builder[$param]);
} else {
$value = $builder[$param];
}

continue;
}

// Grab builder configuration only if defined
if (is_null($builder[$param])) {
continue;
}

$configurations = array();
foreach ($builder[$param] as $name => $configuration) {
if (!is_array($configuration) && !is_null($configuration)) {
throw new \InvalidArgumentException(
sprintf('Invalid %s "%s" builder definition for %s', $param, $name, $this->getFromYaml('params.model'))
);
}

if (!is_null($value) && array_key_exists($name, $value)) {
$configurations[$name] = $configuration
? $this->mergeConfiguration($value[$name], $configuration) // Override definition
: $value[$name]; // Configuration is null => use global definition
} else {
if (is_array($value)) {
$value = $this->recursiveReplace($value, $builder[$param]);
} else {
$value = $builder[$param];
}
// New definition (new field, new action) from builder
$configurations[$name] = $configuration;
}
}

if (in_array($param, array('actions', 'object_actions', 'batch_actions'))) {
// Actions list comes from builder
$value = $configurations;
} else {
// All fields are still available in a builder
$value = array_merge($value ?:array(), $configurations);
}
}

// If builder doesn't have actions/object_actions/batch_actions remove it from merge.
Expand All @@ -177,20 +183,21 @@ protected function mergeParameters(array $global, array $builder)
protected function mergeConfiguration(array $global, array $builder)
{
foreach ($global as $name => &$value) {
if (array_key_exists($name, $builder)) {
if (is_null($builder[$name])) {
continue;
}
if (!array_key_exists($name, $builder) || is_null($builder[$name])) {
continue;
}

if (is_array($value)) {
if (!is_array($builder[$name])) {
throw new \InvalidArgumentException('Invalid generator');
}
$value = array_replace($value, $builder[$name]);
} else {
$value = $builder[$name];
}
if (!is_array($value)) {
$value = $builder[$name];

continue;
}

if (!is_array($builder[$name])) {
throw new \InvalidArgumentException('Invalid generator');
}

$value = array_replace($value, $builder[$name]);
}

return array_merge($global, array_diff_key($builder, $global));
Expand Down
Loading

0 comments on commit f29b802

Please sign in to comment.