Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Symfony form management more robust #8561

Merged
merged 1 commit into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Core/Form/FormDataProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* 2007-2017 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2017 PrestaShop SA
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
namespace PrestaShop\PrestaShop\Core\Form;

use Symfony\Component\Form\FormInterface;

/**
* Symfony forms data provider.
*/
interface FormDataProviderInterface
{
/**
* @return array the form data as an associative array
*/
public function getData();

/**
* Persists form Data in Database and Filesystem.
*
* @param array $data
* @return array $errors if data can't persisted an array of errors messages
* @throws UndefinedOptionsException
*/
public function setData(array $data);
}
3 changes: 3 additions & 0 deletions src/Core/Form/FormHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ interface FormHandlerInterface
public function getForm();

/**
* Describe what need to be done on saving the form: mostly persists the data
* using a form data provider, but it's also the right place to dispatch events/log something.
*
* @param array $data data retrieved from form that need to be persisted in database
* @throws \Exception if the data can't be handled
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
namespace PrestaShopBundle\Form\Admin\AdvancedParameters\Administration;

use PrestaShop\PrestaShop\Adapter\Admin\NotificationsConfiguration;
use PrestaShop\PrestaShop\Adapter\GeneralConfiguration;
use PrestaShop\PrestaShop\Adapter\Upload\UploadQuotaConfiguration;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
use PrestaShop\PrestaShop\Core\Form\FormDataProviderInterface;
use PrestaShop\PrestaShop\Adapter\GeneralConfiguration;

/**
* This class is responsible of managing the data manipulated using forms
* in "Configure > Advanced Parameters > Administration" page.
*/
class FormDataProvider
final class FormDataProvider implements FormDataProviderInterface
{
/**
* @var GeneralConfiguration
Expand Down Expand Up @@ -64,7 +64,7 @@ public function __construct(
}

/**
* @return array
* @{inheritdoc}
*/
public function getData()
{
Expand All @@ -76,11 +76,7 @@ public function getData()
}

/**
* Persists form Data in Database and Filesystem
*
* @param array $data
* @return array $errors if data can't persisted an array of errors messages
* @throws UndefinedOptionsException
* @{inheritdoc}
*/
public function setData(array $data)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,53 +25,51 @@
*/
namespace PrestaShopBundle\Form\Admin\AdvancedParameters\Administration;

use PrestaShopBundle\Form\Admin\AdvancedParameters\Administration\NotificationsType;
use PrestaShopBundle\Form\Admin\AdvancedParameters\Administration\UploadQuotaType;
use PrestaShopBundle\Form\Admin\AdvancedParameters\Administration\GeneralType;
use PrestaShop\PrestaShop\Core\Form\FormDataProviderInterface;
use PrestaShop\PrestaShop\Core\Form\FormHandlerInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;

/**
* This class manages the data manipulated using forms
* in "Configure > Advanced Parameters > Administration" page.
*/
class FormHandler implements FormHandlerInterface
final class FormHandler implements FormHandlerInterface
{
/**
* @var FormFactoryInterface
*/
private $formFactory;

/**
* @var FormDataProvider
* @var FormDataProviderInterface
*/
private $formDataProvider;

public function __construct(
FormFactoryInterface $formFactory,
FormDataProvider $formDataProvider
)
public function __construct(FormFactoryInterface $formFactory, FormDataProviderInterface $formDataProvider)
{
$this->formFactory = $formFactory;
$this->formDataProvider = $formDataProvider;
}

/**
* @return \Symfony\Component\Form\FormInterface
* @{inheritdoc}
*/
public function getForm()
{
return $this->formFactory->createBuilder()
->add('general', 'PrestaShopBundle\Form\Admin\AdvancedParameters\Administration\GeneralType')
->add('upload_quota', 'PrestaShopBundle\Form\Admin\AdvancedParameters\Administration\UploadQuotaType')
->add('notifications', 'PrestaShopBundle\Form\Admin\AdvancedParameters\Administration\NotificationsType')
->add('general', GeneralType::class)
->add('upload_quota', UploadQuotaType::class)
->add('notifications', NotificationsType::class)
->setData($this->formDataProvider->getData())
->getForm()
;
}

/**
* @param array $data
* @return array errors found if not empty
* @throws UndefinedOptionsException if data is invalid
* @{inheritdoc}
*/
public function save(array $data)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@
*/
namespace PrestaShopBundle\Form\Admin\AdvancedParameters\Performance;

use PrestaShop\PrestaShop\Adapter\Cache\CachingConfiguration;
use PrestaShop\PrestaShop\Adapter\Cache\CombineCompressCacheConfiguration;
use PrestaShop\PrestaShop\Adapter\Debug\DebugModeConfiguration;
use PrestaShop\PrestaShop\Adapter\OptionalFeatures\OptionalFeaturesConfiguration;
use PrestaShop\PrestaShop\Adapter\Media\MediaServerConfiguration;
use PrestaShop\PrestaShop\Adapter\Cache\CombineCompressCacheConfiguration;
use PrestaShop\PrestaShop\Adapter\Smarty\SmartyCacheConfiguration;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;
use PrestaShop\PrestaShop\Adapter\Media\MediaServerConfiguration;
use PrestaShop\PrestaShop\Adapter\Debug\DebugModeConfiguration;
use PrestaShop\PrestaShop\Core\Form\FormDataProviderInterface;
use PrestaShop\PrestaShop\Adapter\Cache\CachingConfiguration;

/**
* This class is responsible of managing the data manipulated using forms
* in "Configure > Advanced Parameters > Performance" page.
*/
class PerformanceFormDataProvider
final class PerformanceFormDataProvider implements FormDataProviderInterface
{
/**
* @var SmartyCacheConfiguration
Expand Down Expand Up @@ -87,7 +87,7 @@ public function __construct(
}

/**
* @return array
* @{inheritdoc}
*/
public function getData()
{
Expand All @@ -102,11 +102,7 @@ public function getData()
}

/**
* Persists form Data in Database and Filesystem
*
* @param array $data
* @return array $errors if data can't persisted an array of errors messages
* @throws UndefinedOptionsException
* @{inheritdoc}
*/
public function setData(array $data)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,23 @@
*/
namespace PrestaShopBundle\Form\Admin\AdvancedParameters\Performance;

use PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\CombineCompressCacheType;
use PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\OptionalFeaturesType;
use PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\MemcacheServerType;
use PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\MediaServersType;
use PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\DebugModeType;
use PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\CachingType;
use PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\SmartyType;
use PrestaShop\PrestaShop\Core\Form\FormDataProviderInterface;
use PrestaShop\PrestaShop\Adapter\Feature\CombinationFeature;
use PrestaShop\PrestaShop\Core\Form\FormHandlerInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException;

/**
* This class manages the data manipulated using forms
* in "Configure > Advanced Parameters > Performance" page.
*/
class PerformanceFormHandler implements FormHandlerInterface
final class PerformanceFormHandler implements FormHandlerInterface
{
/**
* @var FormFactoryInterface
Expand All @@ -47,13 +54,13 @@ class PerformanceFormHandler implements FormHandlerInterface
private $combinationFeature;

/**
* @var PerformanceFormDataProvider
* @var FormDataProviderInterface
*/
private $formDataProvider;

public function __construct(
FormFactoryInterface $formFactory,
PerformanceFormDataProvider $formDataProvider,
FormDataProviderInterface $formDataProvider,
CombinationFeature $combinationFeature
)
{
Expand All @@ -63,29 +70,27 @@ public function __construct(
}

/**
* @return \Symfony\Component\Form\FormInterface
* @{inheritdoc}
*/
public function getForm()
{
return $this->formFactory->createBuilder()
->add('smarty', 'PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\SmartyType')
->add('debug_mode', 'PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\DebugModeType')
->add('optional_features', 'PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\OptionalFeaturesType', array(
->add('smarty', SmartyType::class)
->add('debug_mode', DebugModeType::class)
->add('optional_features', OptionalFeaturesType::class, array(
'are_combinations_used' => $this->combinationFeature->isUsed()
))
->add('ccc', 'PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\CombineCompressCacheType')
->add('media_servers', 'PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\MediaServersType')
->add('caching', 'PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\CachingType')
->add('add_memcache_server', 'PrestaShopBundle\Form\Admin\AdvancedParameters\Performance\MemcacheServerType')
->add('ccc', CombineCompressCacheType::class)
->add('media_servers', MediaServersType::class)
->add('caching', CachingType::class)
->add('add_memcache_server', MemcacheServerType::class)
->setData($this->formDataProvider->getData())
->getForm()
;
}

/**
* @param array $data
* @return array errors found if not empty
* @throws UndefinedOptionsException if data is invalid
* @{inheritdoc}
*/
public function save(array $data)
{
Expand Down