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

Use request from stack only when available #210

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions Resources/config/service.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

<services>
<!-- T W I G H E L P E R S -->
<service id="twig.extension.stfalcon_tinymce" class="%stfalcon_tinymce.twig.extension.class%">
<argument type="service" id="service_container" />
<service id="twig.extension.stfalcon_tinymce" class="%stfalcon_tinymce.twig.extension.class%" autowire="true">
<tag name="twig.extension" alias="stfalcon_tinymce" />
</service>
</services>
Expand Down
2 changes: 1 addition & 1 deletion Resources/public/js/init.standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function initTinyMCE(options) {
if (_t) textareas.push(_t);
break;
case ".":
textareas = getElementsByClassName(options.selector, 'textarea');
textareas = getElementsByClassName(options.selector.substring(1), 'textarea');
break;
default :
textareas = document.getElementsByTagName('textarea');
Expand Down
61 changes: 35 additions & 26 deletions Twig/Extension/StfalconTinymceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
namespace Stfalcon\Bundle\TinymceBundle\Twig\Extension;

use Stfalcon\Bundle\TinymceBundle\Helper\LocaleHelper;
use Symfony\Component\Asset\Packages;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
* Twig Extension for TinyMce support.
*
* @author naydav <[email protected]>
*/
class StfalconTinymceExtension extends \Twig_Extension
class StfalconTinymceExtension extends AbstractExtension
{
/**
* @var ContainerInterface $container Container interface
Expand All @@ -24,27 +29,31 @@ class StfalconTinymceExtension extends \Twig_Extension
* @var String
*/
protected $baseUrl;
/**
* @var Packages
*/
private $assetPackages;
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var Environment
*/
private $twig;

/**
* Initialize tinymce helper
*
* @param ContainerInterface $container
* @param Packages $assetPackages
*/
public function __construct(ContainerInterface $container)
public function __construct(ContainerInterface $container, Packages $assetPackages, RequestStack $requestStack, Environment $twig)
{
$this->container = $container;
}

/**
* Gets a service.
*
* @param string $id The service identifier
*
* @return object The associated service
*/
public function getService($id)
{
return $this->container->get($id);
$this->assetPackages = $assetPackages;
$this->requestStack = $requestStack;
$this->twig = $twig;
}

/**
Expand All @@ -67,7 +76,7 @@ public function getParameter($name)
public function getFunctions()
{
return array(
'tinymce_init' => new \Twig_SimpleFunction(
'tinymce_init' => new TwigFunction(
'tinymce_init',
array($this, 'tinymceInit'),
array('is_safe' => array('html'))
Expand All @@ -93,12 +102,9 @@ public function tinymceInit($options = array())
$assetPackageName = (!isset($config['asset_package_name']) ? null : $config['asset_package_name']);
unset($config['asset_package_name']);

/** @var $assets \Symfony\Component\Templating\Helper\CoreAssetsHelper */
$assets = $this->getService('assets.packages');

// Get path to tinymce script for the jQuery version of the editor
if ($config['tinymce_jquery']) {
$config['jquery_script_url'] = $assets->getUrl(
$config['jquery_script_url'] = $this->assetPackages->getUrl(
$this->baseUrl.'bundles/stfalcontinymce/vendor/tinymce/tinymce.jquery.min.js',
$assetPackageName
);
Expand Down Expand Up @@ -126,8 +132,14 @@ public function tinymceInit($options = array())

// If the language is not set in the config...
if (!isset($config['language']) || empty($config['language'])) {
// get it from the request
$config['language'] = $this->container->get('request_stack')->getCurrentRequest()->getLocale();
// get it from the request, if available
$currentRequest = $this->requestStack
->getCurrentRequest()
;

if ($currentRequest) {
$config['language'] = $currentRequest->getLocale();
}
}

$config['language'] = LocaleHelper::getLanguage($config['language']);
Expand Down Expand Up @@ -180,7 +192,7 @@ public function tinymceInit($options = array())
json_encode($config)
);

return $this->getService('templating')->render('StfalconTinymceBundle:Script:init.html.twig', array(
return $this->twig->render('StfalconTinymceBundle:Script:init.html.twig', array(
'tinymce_config' => $tinymceConfiguration,
'include_jquery' => $config['include_jquery'],
'tinymce_jquery' => $config['tinymce_jquery'],
Expand Down Expand Up @@ -208,13 +220,10 @@ public function getName()
*/
protected function getAssetsUrl($inputUrl)
{
/** @var $assets \Symfony\Component\Templating\Helper\CoreAssetsHelper */
$assets = $this->getService('assets.packages');

$url = preg_replace('/^asset\[(.+)\]$/i', '$1', $inputUrl);

if ($inputUrl !== $url) {
return $assets->getUrl($this->baseUrl.$url);
return $this->assetPackages->getUrl($this->baseUrl.$url);
}

return $inputUrl;
Expand Down