Skip to content

Commit

Permalink
Form type improvements: inject ParameterBag instead of Container; cle…
Browse files Browse the repository at this point in the history
…anup
  • Loading branch information
jmsche committed Jun 18, 2019
1 parent b3bb921 commit 832bc33
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 48 deletions.
61 changes: 15 additions & 46 deletions Form/Type/FroalaEditorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
use KMS\FroalaEditorBundle\Service\OptionManager;
use KMS\FroalaEditorBundle\Service\PluginProvider;
use KMS\FroalaEditorBundle\Utility\UConfiguration;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -27,10 +26,10 @@ class FroalaEditorType extends AbstractType
// --------------------------- MEMBERS -------------------------//
// -------------------------------------------------------------//

/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $m_container;
/**
* @var \Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface
*/
private $parameterBag;

/**
* @var \KMS\FroalaEditorBundle\Service\OptionManager
Expand All @@ -42,30 +41,17 @@ class FroalaEditorType extends AbstractType
*/
private $m_pluginProvider;

/**
* @var int
*/
private $m_version;

// -------------------------------------------------------------//
// -------------------------- CONSTRUCTOR ----------------------//
// -------------------------------------------------------------//

/**
* FroalaEditorType constructor.
* @param \Symfony\Component\HttpKernel\Kernel $p_kernel
* @param \Symfony\Component\DependencyInjection\ContainerInterface $p_container
* @param \KMS\FroalaEditorBundle\Service\OptionManager $p_optionManager
* @param \KMS\FroalaEditorBundle\Service\PluginProvider $p_pluginProvider
*/
public function __construct( Kernel $p_kernel, ContainerInterface $p_container, OptionManager $p_optionManager, PluginProvider $p_pluginProvider )
public function __construct( ParameterBagInterface $parameterBag, OptionManager $p_optionManager, PluginProvider $p_pluginProvider )
{
// ------------------------- DECLARE ---------------------------//

$this->m_container = $p_container;
$this->parameterBag = $parameterBag;
$this->m_optionManager = $p_optionManager;
$this->m_pluginProvider = $p_pluginProvider;
$this->m_version = $p_kernel::MAJOR_VERSION;
}

// -------------------------------------------------------------//
Expand Down Expand Up @@ -106,10 +92,10 @@ public function buildView( FormView $p_view, FormInterface $p_form, array $p_opt
$profile = isset( $p_options [ "profile" ] ) ? $p_options [ "profile" ] : null;
// ------------------------- DECLARE ---------------------------//

if( $profile && $this->m_container->hasParameter( Configuration::$NODE_ROOT . '.profiles' ) )
if( $profile && $this->parameterBag->has( Configuration::$NODE_ROOT . '.profiles' ) )
{

$profiles = $this->m_container->getParameter( Configuration::$NODE_ROOT . '.profiles' );
$profiles = $this->parameterBag->get( Configuration::$NODE_ROOT . '.profiles' );

if( array_key_exists( $profile, $profiles ) )
{
Expand Down Expand Up @@ -172,10 +158,10 @@ public function configureOptions( OptionsResolver $p_resolver )
{

// If defined in config file, set default value to form, else set option as available.
if( $this->m_container->hasParameter( Configuration::$NODE_ROOT . '.' . $option ) )
if( $this->parameterBag->has( Configuration::$NODE_ROOT . '.' . $option ) )
{
$arrDefault[ $option ] =
$this->m_container->getParameter( Configuration::$NODE_ROOT . '.' . $option );
$this->parameterBag->get( Configuration::$NODE_ROOT . '.' . $option );
}
else
{
Expand All @@ -195,24 +181,7 @@ public function configureOptions( OptionsResolver $p_resolver )
*/
public function getParent()
{
// ------------------------- DECLARE ---------------------------//

if( $this->m_version >= 3 )
{
return "Symfony\Component\Form\Extension\Core\Type\TextareaType";
}

return "textarea";
}

/**
* @return string
*/
public function getName()
{
//------------------------- DECLARE ---------------------------//

return "froala";
return TextareaType::class;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ services:
# Form type.
kms_froala_editor.form.type:
class: "KMS\\FroalaEditorBundle\\Form\\Type\\FroalaEditorType"
arguments: [ "@kernel", "@service_container", "@kms_froala_editor.option_manager", "@kms_froala_editor.plugin_provider" ]
arguments: [ "@parameter_bag", "@kms_froala_editor.option_manager", "@kms_froala_editor.plugin_provider" ]
tags:
- { name: "form.type", alias: "froala" }
- { name: "form.type" }

# Plugin provider.
kms_froala_editor.option_manager:
Expand Down

0 comments on commit 832bc33

Please sign in to comment.