Skip to content

Commit

Permalink
Implement assets, all helpers, minification, globals
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Aug 2, 2016
1 parent b1c98fa commit ae72a7e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"description": "Pug template engine for Symfony",
"type": "library",
"require": {
"pug-php/pug": "^2.0",
"pug-php/pug-assets": "^1.0"
"pug-php/pug": "^2.1.3",
"pug-php/pug-assets": "^1.0.1"
},
"license": "MIT",
"authors": [
Expand Down
77 changes: 69 additions & 8 deletions src/Jade/JadeSymfonyEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Jade;

use Symfony\Bridge\Twig\AppVariable;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Templating\EngineInterface;
use Jade\Jade;
use Jade\Symfony\JadeEngine as Jade;
use Pug\Assets;

class JadeSymfonyEngine implements EngineInterface, \ArrayAccess
Expand All @@ -16,7 +19,7 @@ class JadeSymfonyEngine implements EngineInterface, \ArrayAccess

public function __construct($kernel)
{
if (empty($kernel) || !method_exists($kernel, 'getCacheDir')) {
if (empty($kernel) || !($kernel instanceof Kernel)) {
throw new \InvalidArgumentException("It seems you did not set the new settings in services.yml, please add \"@kernel\" to templating.engine.pug service arguments, see https://github.com/pug-php/pug-symfony#readme", 1);
}

Expand All @@ -26,21 +29,79 @@ public function __construct($kernel)
mkdir($cache);
}
$container = $kernel->getContainer();
$this->container = $container;
$environment = $container->getParameter('kernel.environment');
$appDir = $container->getParameter('kernel.root_dir');
$assetsDirectories = array($appDir . '/Resources/assets');
$srcDir = dirname($appDir) . '/src';
$baseDir = null;
foreach (scandir($srcDir) as $directory) {
if (is_null($baseDir) && is_dir($srcDir . '/Resources/views')) {
$baseDir = $appDir . '/Resources/views';
}
$assetsDirectories[] = $srcDir . '/' . $directory . '/Resources/assets';
}
if (is_null($baseDir)) {
$baseDir = $appDir . '/Resources/views';
}
$this->jade = new Jade(array(
'prettyprint' => $kernel->isDebug(),
'assetDirectory' => $assetsDirectories,
'baseDir' => $baseDir,
'cache' => substr($environment, 0, 3) === 'dev' ? false : $cache,
'environment' => $environment,
'extension' => array('.pug', '.jade'),
'cache' => substr($environment, 0, 3) === 'dev' ? $cache : false,
'assetDirectory' => __DIR__ . '/../Resources/assets',
'outputDirectory' => __DIR__ . '/../../../web',
'environment' => $environment,
'preRender' => array($this, 'preRender'),
'prettyprint' => $kernel->isDebug(),
));
foreach (array_slice(func_get_args(), 1) as $helper) {
$this->registerHelpers($container, array_slice(func_get_args(), 1));
$this->assets = new Assets($this->jade);
$app = new AppVariable();
$app->setDebug($kernel->isDebug());
$app->setEnvironment($environment);
$app->setRequestStack($container->get('request_stack'));
$app->setTokenStorage($container->get('security.token_storage'));
$this->jade->share('app', $app);
}

/**
* Pug code transformation to do before Pug render.
*
* @param string $pugCode code input
*
* @return string
*/
public function preRender($pugCode)
{
return preg_replace('/(?<=\=\>|[=\.,:\?\(])\s*asset\s*\(/', '$view[\'assets\']->getUrl(', $pugCode);
}

protected function registerHelpers(ContainerInterface $services, $helpers)
{
$this->helpers = array();
foreach (array(
'actions',
'assets',
'code',
'form' ,
'logout_url',
'request',
'router',
'security',
'session',
'slots',
'stopwatch',
'translator',
) as $helper) {
if ($instance = $services->get('templating.helper.' . $helper)) {
$this->helpers[$helper] = $instance;
}
}
foreach ($helpers as $helper) {
$name = preg_replace('`^(?:.+\\\\)([^\\\\]+?)(?:Helper)?$`', '$1', get_class($helper));
$name = strtolower(substr($name, 0, 1)) . substr($name, 1);
$this->helpers[$name] = $helper;
}
$this->assets = new Assets($this->jade);
}

public function getOption($name)
Expand Down
25 changes: 25 additions & 0 deletions src/Jade/Symfony/JadeEngine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Jade\Symfony;

use Jade\Jade;

class JadeEngine extends Jade
{
/**
* Compile PHP code from a Pug input or a Pug file.
*
* @param string input
*
* @throws \Exception
*
* @return string
*/
public function compile($input)
{
$php = parent::compile($input);
$php = preg_replace('/(\\Jade\\Compiler::getPropertyFromAnything\((?:[^()]++|(?R))*+\))\(((?:[^()]++|(?R))*+)\)/', 'call_user_func($1, $2)', $php);

return $php;
}
}

0 comments on commit ae72a7e

Please sign in to comment.