Skip to content

Commit

Permalink
Merge pull request #151 from Kunstmaan/icon_font_field_type
Browse files Browse the repository at this point in the history
Icon font field type
  • Loading branch information
jverdeyen committed Jun 26, 2014
2 parents 9d6eed8 + c55cc81 commit 5044abd
Show file tree
Hide file tree
Showing 11 changed files with 432 additions and 7 deletions.
40 changes: 40 additions & 0 deletions Controller/IconFontController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Kunstmaan\MediaBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

/**
* IconFontController
*/
class IconFontController extends Controller
{
/**
* @param Request $request
*
* @Route("/chooser", name="KunstmaanMediaBundle_icon_font_chooser")
* @Template()
*
* @return array
*/
public function iconFontChooserAction(Request $request)
{
$loader = $request->query->get('loader');
$loaderData = unserialize($request->query->get('loader_data'));

$iconFontManager = $this->get('kunstmaan_media.icon_font_manager');
if (empty($loader)) {
$loader = $iconFontManager->getDefaultLoader();
} else {
$loader = $iconFontManager->getLoader($loader);
}
$loader->setData($loaderData);

return array(
'loader' => $loader
);
}
}
16 changes: 11 additions & 5 deletions DependencyInjection/Compiler/MediaHandlerCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ class MediaHandlerCompilerPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container)
{
if (false === $container->hasDefinition('kunstmaan_media.media_manager')) {
return;
if ($container->hasDefinition('kunstmaan_media.media_manager')) {
$definition = $container->getDefinition('kunstmaan_media.media_manager');

foreach ($container->findTaggedServiceIds('kunstmaan_media.media_handler') as $id => $attributes) {
$definition->addMethodCall('addHandler', array(new Reference($id)));
}
}

$definition = $container->getDefinition('kunstmaan_media.media_manager');
if ($container->hasDefinition('kunstmaan_media.icon_font_manager')) {
$definition = $container->getDefinition('kunstmaan_media.icon_font_manager');

foreach ($container->findTaggedServiceIds('kunstmaan_media.media_handler') as $id => $attributes) {
$definition->addMethodCall('addHandler', array(new Reference($id)));
foreach ($container->findTaggedServiceIds('kunstmaan_media.icon_font.loader') as $id => $attributes) {
$definition->addMethodCall('addLoader', array(new Reference($id), $id));
}
}
}
}
96 changes: 96 additions & 0 deletions Form/Type/IconFontType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Kunstmaan\MediaBundle\Form\Type;

use Kunstmaan\MediaBundle\Helper\IconFont\IconFontManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
* IconFontType
*/
class IconFontType extends AbstractType
{
/**
* @var IconFontManager
*/
private $iconFontManager;

/**
* @param IconFontManager $iconFontManager
*/
public function __construct(IconFontManager $iconFontManager)
{
$this->iconFontManager = $iconFontManager;
}

/**
* @return string
*/
public function getParent()
{
return 'text';
}

/**
* @return string
*/
public function getName()
{
return 'iconfont';
}

/**
* Sets the default options for this type.
*
* @param OptionsResolverInterface $resolver The resolver for the options.
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
parent::setDefaultOptions($resolver);

$resolver->setDefaults(array(
'loader' => null,
'loader_data' => null
));
}

/**
* Builds the form.
*
* This method is called for each type in the hierarchy starting form the
* top most type. Type extensions can further modify the form.
*
* @param FormBuilderInterface $builder The form builder
* @param array $options The options
*
* @see FormTypeExtensionInterface::buildForm()
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if (!$options['loader']) {
$loader = $this->iconFontManager->getDefaultLoader();
} else {
$loader = $this->iconFontManager->getLoader($options['loader']);
}
$loader->setData($options['loader_data']);

$builder->setAttribute('loader', $options['loader']);
$builder->setAttribute('loader_object', $loader);
$builder->setAttribute('loader_data', $options['loader_data']);
}

/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['loader'] = $form->getConfig()->getAttribute('loader');
$view->vars['loader_object'] = $form->getConfig()->getAttribute('loader_object');
$view->vars['loader_data'] = serialize($form->getConfig()->getAttribute('loader_data'));
}

}
24 changes: 24 additions & 0 deletions Helper/IconFont/AbstractIconFontLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Kunstmaan\MediaBundle\Helper\IconFont;

use Symfony\Component\HttpKernel\KernelInterface;

/**
* AbstractIconFontLoader
*/
abstract class AbstractIconFontLoader implements IconFontLoaderInterface
{
/**
* @var string
*/
protected $rootPath;

/**
* @param KernelInterface $kernel
*/
public function __construct(KernelInterface $kernel)
{
$this->rootPath = dirname($kernel->getRootDir());
}
}
56 changes: 56 additions & 0 deletions Helper/IconFont/DefaultIconFontLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Kunstmaan\MediaBundle\Helper\IconFont;

use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;

/**
* DefaultIconFontLoader
*/
class DefaultIconFontLoader extends AbstractIconFontLoader
{
/**
* @var string
*/
private $cssPath;

/**
* @param array $data
* @throws MissingOptionsException
* @throws InvalidOptionsException
*/
public function setData(array $data)
{
if (!array_key_exists('css', $data)) {
throw new MissingOptionsException('Missing required loader_data option: "css"');
}

$this->cssPath = trim($data['css'], '/');

$cssPath = $this->rootPath . '/web/' . $this->cssPath;
if (!file_exists($cssPath)) {
throw new InvalidOptionsException(sprintf('Could not find the css file with this path "%s"', $cssPath));
}
}

/**
* @return string
*/
public function getCssLink()
{
return '/' . $this->cssPath;
}

/**
* @return array
*/
public function getCssClasses()
{
$contents = file_get_contents($this->rootPath . '/web/' . $this->cssPath);

preg_match_all('/\.([a-zA-Z0-9-_]+):before[ ]*\{[ \n]*content:/', $contents, $matches);

return $matches[1];
}
}
10 changes: 10 additions & 0 deletions Helper/IconFont/IconFontLoaderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Kunstmaan\MediaBundle\Helper\IconFont;

interface IconFontLoaderInterface
{
public function setData(array $data);
public function getCssLink();
public function getCssClasses();
}
61 changes: 61 additions & 0 deletions Helper/IconFont/IconFontManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Kunstmaan\MediaBundle\Helper\IconFont;

/**
* IconFontManager
*/
class IconFontManager
{
/**
* @var IconFontLoaderInterface[]
*/
protected $loaders = array();

/**
* @var IconFontLoaderInterface
*/
protected $defaultLoader = null;

/**
* @param IconFontLoaderInterface $loader
* @param string $serviceId
*/
public function addLoader(IconFontLoaderInterface $loader, $serviceId)
{
$this->loaders[$serviceId] = $loader;
}

/**
* @param IconFontLoaderInterface $loader
*/
public function setDefaultLoader(IconFontLoaderInterface $loader)
{
$this->defaultLoader = $loader;
}

/**
* @param string $serviceId
* @return IconFontLoaderInterface
*/
public function getLoader($serviceId)
{
return $this->loaders[$serviceId];
}

/**
* @return IconFontLoaderInterface[]
*/
public function getLoaders()
{
return $this->loaders;
}

/**
* @return IconFontLoaderInterface|null
*/
public function getDefaultLoader()
{
return $this->defaultLoader;
}
}
7 changes: 6 additions & 1 deletion Resources/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ KunstmaanMediaBundle_media_chooser:
KunstmaanMediaBundle_media_folder:
resource: "@KunstmaanMediaBundle/Controller/FolderController.php"
type: annotation
prefix: /admin/media/folder
prefix: /admin/media/folder

KunstmaanMediaBundle_icon_font:
resource: "@KunstmaanMediaBundle/Controller/IconFontController.php"
type: annotation
prefix: /admin/media/icon-font
21 changes: 20 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ parameters:
kunstmaan_media.menu.adaptor.class: 'Kunstmaan\MediaBundle\Helper\Menu\MediaMenuAdaptor'
kunstmaan_media.listener.doctrine.class: 'Kunstmaan\MediaBundle\EventListener\DoctrineMediaListener'
kunstmaan_media.form.type.media.class: 'Kunstmaan\MediaBundle\Form\Type\MediaType'
kunstmaan_media.form.type.iconfont.class: 'Kunstmaan\MediaBundle\Form\Type\IconFontType'
kunstmaan_media.icon_font_manager.class: 'Kunstmaan\MediaBundle\Helper\IconFont\IconFontManager'
kunstmaan_media.icon_font.default_loader.class: 'Kunstmaan\MediaBundle\Helper\IconFont\DefaultIconFontLoader'
kunstmaan_media.media_creator_service.class: 'Kunstmaan\MediaBundle\Helper\Services\MediaCreatorService'


services:
liip_imagine.data.loader.stream.profile_photos:
class: "%liip_imagine.data.loader.stream.class%"
Expand Down Expand Up @@ -44,6 +46,23 @@ services:
tags:
- { name: form.type, alias: media }

form.type.iconfont:
class: "%kunstmaan_media.form.type.iconfont.class%"
arguments: ["@kunstmaan_media.icon_font_manager"]
tags:
- { name: form.type, alias: iconfont }

kunstmaan_media.icon_font_manager:
class: "%kunstmaan_media.icon_font_manager.class%"
calls:
- [ setDefaultLoader, [ "@kunstmaan_media.icon_font.default_loader" ] ]

kunstmaan_media.icon_font.default_loader:
class: "%kunstmaan_media.icon_font.default_loader.class%"
arguments: ["@kernel"]
tags:
- { name: 'kunstmaan_media.icon_font.loader' }

kunstmaan_media.media_creator_service:
class: "%kunstmaan_media.media_creator_service.class%"
arguments: ["@service_container"]
Loading

0 comments on commit 5044abd

Please sign in to comment.