Skip to content

Commit

Permalink
fix missing require option in configuration form, fix #79
Browse files Browse the repository at this point in the history
  • Loading branch information
ramunasd committed Dec 2, 2018
1 parent 1aa85ca commit 2f56dc7
Show file tree
Hide file tree
Showing 13 changed files with 1,297 additions and 20 deletions.
1 change: 0 additions & 1 deletion app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
}
form {
max-width: 480px;
padding: 19px 29px 29px;
margin: 10px auto 20px;
background-color: #edeff1;
Expand Down
12 changes: 12 additions & 0 deletions app/Resources/views/form/fields.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% block PackageConstraintType_row %}
<div class="clearfix">
{{ form_errors(form) }}
{{ form_widget(form) }}
</div>
{% endblock %}

{% block PackageConstraintType_widget %}
<div class="col-sm-6">{{ form_widget(form.package) }} {{ form_errors(form.package) }}</div>
<div class="col-sm-4">{{ form_widget(form.constraint) }} {{ form_errors(form.constraint) }}</div>
<div class="col-sm-2 {{ form.parent.vars.id }}-collection-actions"></div>
{% endblock %}
33 changes: 33 additions & 0 deletions app/Resources/views/form/jquery.collection.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{#
# jquery.collection.html.twig
#
# Adds the following html attributes to the root node of your collection fields:
#
# - data-prototype-name: placeholder used in the prototype to replace element indexes on the collection
# - data-allow-add: if set to false, plugin will automatically set allow_add option to false
# - data-allow-delete: if set to false, plugin will automatically set allow_remove option to false
# - data-name-prefix: contains the collection's prefix used in descendant field names
#
# Those information let you configure the plugin options automatically.
#
# If you are already using a form theme, you can use both by using:
# {%
# form_theme myForm
# 'FuzAppBundle::my-form-theme.html.twig'
# 'FuzAppBundle::jquery.collection.html.twig'
# ...
# %}
#}

{% block collection_widget %}
{% spaceless %}
{% if prototype is defined %}
{% set attr = attr|merge({'data-prototype': form_row(prototype)}) %}
{% set attr = attr|merge({'data-prototype-name': prototype.vars.name}) %}
{% endif %}
{% set attr = attr|merge({'data-allow-add': allow_add ? 1 : 0}) %}
{% set attr = attr|merge({'data-allow-remove': allow_delete ? 1 : 0 }) %}
{% set attr = attr|merge({'data-name-prefix': full_name}) %}
{{ block('form_widget') }}
{% endspaceless %}
{% endblock collection_widget %}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"doctrine/instantiator": "~1.1",
"incenteev/composer-parameter-handler": "^2.1",
"swop/github-webhook": "^2.0",
"zendframework/zend-diactoros": "^1.8"
"zendframework/zend-diactoros": "^1.8",
"ninsuo/symfony-collection": "^2.1"
},
"require-dev": {
"symfony/browser-kit": "^3.4",
Expand Down
35 changes: 34 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion src/Playbloom/Satisfy/Form/Type/ConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;

class ConfigurationType extends AbstractType
{
Expand All @@ -24,6 +25,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'required' => false,
])
->add('homepage', UrlType::class)
->add('require', CollectionType::class, [
'required' => false,
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'entry_type' => PackageConstraintType::class,
'prototype' => true,
'attr' => [
'class' => 'collection_require',
],
'constraints' => [
new Assert\Valid(),
],
])
->add('requireAll', CheckboxType::class, [
'required' => false,
'attr' => [
Expand Down
55 changes: 55 additions & 0 deletions src/Playbloom/Satisfy/Form/Type/PackageConstraintType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Playbloom\Satisfy\Form\Type;

use Playbloom\Satisfy\Model\PackageConstraint;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;

class PackageConstraintType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'package',
TextType::class,
[
'required' => true,
'empty_data' => '',
'constraints' => [
new Assert\NotBlank(),
],
'attr' => [
'placeholder' => 'Package name',
],
]
)
->add('constraint', TextType::class, [
'required' => true,
'empty_data' => '',
'constraints' => [
new Assert\NotBlank(),
],
'attr' => [
'placeholder' => 'Package version, i.e. ^1.0',
],
]);
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => PackageConstraint::class,
'empty_data' => new PackageConstraint('', ''),
]);
}

public function getBlockPrefix()
{
return 'PackageConstraintType';
}
}
40 changes: 24 additions & 16 deletions src/Playbloom/Satisfy/Model/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\Type;
use PhpCollection\Map;
use Webmozart\Assert\Assert;

/**
* Configuration class
Expand Down Expand Up @@ -53,7 +54,8 @@ class Configuration
private $repositories;

/**
* @Type("array")
* @var PackageConstraint[]
* @Type("RequireCollection<Playbloom\Satisfy\Model\PackageConstraint>")
* @SerializedName("require")
*/
private $require;
Expand Down Expand Up @@ -200,9 +202,6 @@ public function isOutputHtml(): bool
return $this->outputHtml;
}

/**
* @return $this
*/
public function setOutputHtml(bool $outputHtml)
{
$this->outputHtml = $outputHtml;
Expand Down Expand Up @@ -284,6 +283,27 @@ public function setRepositories(Map $repositories)
return $this;
}

/**
* @return PackageConstraint[]
*/
public function getRequire(): array
{
return $this->require ?: [];
}

/**
* @param PackageConstraint[] $require
*
* @return $this
*/
public function setRequire(array $require)
{
Assert::allIsInstanceOf($require, PackageConstraint::class);
$this->require = $require;

return $this;
}

/**
* @param Archive $archive
*/
Expand Down Expand Up @@ -365,18 +385,6 @@ public function setConfig(string $config)
}
}

/**
* @return array|null
*/
public function getRequire()
{
if (empty($this->require)) {
return null;
}

return $this->require;
}

/**
* @return string|null
*/
Expand Down
48 changes: 48 additions & 0 deletions src/Playbloom/Satisfy/Model/PackageConstraint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Playbloom\Satisfy\Model;

class PackageConstraint
{
/** @var string */
private $package;

/** @var string */
private $constraint;

public function __construct(string $package, string $constraint)
{
$this->package = $package;
$this->constraint = $constraint;
}

public function getPackage(): string
{
return $this->package;
}

/**
* @return $this
*/
public function setPackage(string $package)
{
$this->package = $package;

return $this;
}

public function getConstraint(): string
{
return $this->constraint;
}

/**
* @return $this
*/
public function setConstraint(string $constraint)
{
$this->constraint = $constraint;

return $this;
}
}
6 changes: 6 additions & 0 deletions src/Playbloom/Satisfy/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ services:
- { name: jms_serializer.handler, type: RepositoryCollection, direction: serialization, format: json, method: serializeCollection }
- { name: jms_serializer.handler, type: RepositoryCollection, direction: deserialization, format: json, method: deserializeCollection }

satisfy.serializer.require_collection_handler:
class: Playbloom\Satisfy\Serializer\RequireConstraintHandler
tags:
- { name: jms_serializer.handler, type: RequireCollection, direction: serialization, format: json, method: serializeCollection }
- { name: jms_serializer.handler, type: RequireCollection, direction: deserialization, format: json, method: deserializeCollection }

satisfy.processor.lock_processor:
class: Playbloom\Satisfy\Service\LockProcessor
arguments:
Expand Down
15 changes: 15 additions & 0 deletions src/Playbloom/Satisfy/Resources/views/configuration.html.twig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "base.html.twig" %}
{% form_theme form 'form/jquery.collection.html.twig' 'form/fields.html.twig' %}

{% block title %}Edit configuration{% endblock %}

Expand All @@ -10,3 +11,17 @@
<button class="btn btn-large btn-block btn-primary" type="submit">Submit</button>
{{ form_end(form) }}
{% endblock %}

{% block javascript %}
{{ parent() }}
<script src="{{ asset('js/jquery.collection.js') }}"></script>
<script type="text/javascript">
$(".collection_require").collection({
add: '<p class="col-sm-2"><button class="btn btn-default"><i class="glyphicon glyphicon-plus"></i></button></p>',
remove: '<button class="btn btn-default"><i class="glyphicon glyphicon-minus"></i></button>',
allow_down: false,
allow_up: false,
add_at_the_end: true
});
</script>
{% endblock %}
Loading

0 comments on commit 2f56dc7

Please sign in to comment.