Commit f210d1ea authored by Igor Wiedler's avatar Igor Wiedler

Merge branch 'master' into swiftmailer-docs

* master:
  changed Swiftmailer autoloader to use the Swiftmailer one (it automatically loads the init file)
  added a way to render a controller from a template (like in Symfony2)
  fixed typo
  added SessionCsrfProvider support in FormExtension when SessionExtension is registered
  fixed priority of the session listener (matches the priority set in Symfony FrameworkBundle)
  updated Twig vendor
  Updated the doc for the SessionExtension
  Made the default locale configurable
  updated domain names
  added a tip about a possible problem with phar archives
parents 50fddd1c 5d075703
......@@ -34,5 +34,5 @@ Read the [documentation][3] for more information.
Silex is licensed under the MIT license.
[1]: http://symfony.com
[2]: http://silex-project.org/get/silex.phar
[3]: http://silex-project.org/documentation
[2]: http://silex.sensiolabs.org/get/silex.phar
[3]: http://silex.sensiolabs.org/documentation
......@@ -7,6 +7,8 @@ between requests.
Parameters
----------
* **session.default_locale**: The locale used by default in the session.
* **session.storage.options**: An array of options that is passed to the
constructor of the ``session.storage`` service.
......
......@@ -2,7 +2,7 @@ TwigExtension
=============
The *TwigExtension* provides integration with the `Twig
<http://www.twig-project.org/>`_ template engine.
<http://twig.sensiolabs.org/>`_ template engine.
Parameters
----------
......@@ -62,16 +62,23 @@ The Twig extension provides a ``twig`` service::
This will render a file named ``views/hello.twig``.
.. tip::
In any Twig template, the ``app`` variable refers to the Application object.
So you can access any services from within your view. For example to access
``$app['request']->getHost()``, just put this in your template:
The TwigExtension also registers the application as a global
named ``app``. So you can access any services from within your
view. For example to access ``$app['request']->getHost()``,
just put this in your template:
.. code-block:: jinja
.. code-block:: jinja
{{ app.request.host }}
{{ app.request.host }}
A ``render`` function is also register to help you render another controller
from a template:
.. code-block:: jinja
{{ render('/sidebar') }}
{# or if you are also using UrlGeneratorExtension #}
{{ render(path('sidebar')) }}
For more information, check out the `Twig documentation
<http://www.twig-project.org>`_.
<http://twig.sensiolabs.org>`_.
......@@ -45,4 +45,4 @@ It's really that easy!
Installing Silex is as easy as it can get. Download the `silex.phar`_ file
and you're done!
.. _silex.phar: http://silex-project.org/get/silex.phar
.. _silex.phar: http://silex.sensiolabs.org/get/silex.phar
Services
========
Silex is not only a microframework. It is also a micro service container.
It does this by extending `Pimple <http://pimple-project.org>`_
which provides service goodness in just 44 NCLOC.
Silex is not only a microframework. It is also a micro service container. It
does this by extending `Pimple <http://pimple.sensiolabs.org>`_ which provides
service goodness in just 44 NCLOC.
Dependency Injection
--------------------
......
......@@ -559,7 +559,7 @@ command:
$ php silex.phar update
This will automatically download a new ``silex.phar`` from
``silex-project.org`` and replace the existing one.
``silex.sensiolabs.org`` and replace the existing one.
Pitfalls
--------
......@@ -577,6 +577,7 @@ the following may help.
phar.readonly = Off
phar.require_hash = Off
detect_unicode = Off
If you are on Suhosin you will also have to set this:
......
......@@ -108,14 +108,14 @@ require_once 'phar://silex.phar/autoload.php';
if ('cli' === php_sapi_name() && basename(__FILE__) === basename($_SERVER['argv'][0]) && isset($_SERVER['argv'][1])) {
switch ($_SERVER['argv'][1]) {
case 'update':
$remoteFilename = 'http://silex-project.org/get/silex.phar';
$remoteFilename = 'http://silex.sensiolabs.org/get/silex.phar';
$localFilename = __DIR__.'/silex.phar';
file_put_contents($localFilename, file_get_contents($remoteFilename));
break;
case 'check':
$latest = trim(file_get_contents('http://silex-project.org/get/version'));
$latest = trim(file_get_contents('http://silex.sensiolabs.org/get/version'));
if ($latest != Silex\Application::VERSION) {
printf("A newer Silex version is available (%s).\n", $latest);
......
......@@ -14,6 +14,7 @@ namespace Silex\Extension;
use Silex\Application;
use Silex\ExtensionInterface;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\DefaultCsrfProvider;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\Form\Extension\Core\CoreExtension;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension as FormValidatorExtension;
......@@ -44,6 +45,10 @@ class FormExtension implements ExtensionInterface
});
$app['form.csrf_provider'] = $app->share(function () use ($app) {
if (isset($app['session'])) {
return new SessionCsrfProvider($app['session'], $app['form.secret']);
}
return new DefaultCsrfProvider($app['form.secret']);
});
......
......@@ -32,18 +32,22 @@ class SessionExtension implements ExtensionInterface
$this->app = $app;
$app['session'] = $app->share(function () use ($app) {
return new Session($app['session.storage']);
return new Session($app['session.storage'], $app['session.default_locale']);
});
$app['session.storage'] = $app->share(function () use ($app) {
return new NativeSessionStorage($app['session.storage.options']);
});
$app['dispatcher']->addListener(KernelEvents::REQUEST, array($this, 'onKernelRequest'), -255);
$app['dispatcher']->addListener(KernelEvents::REQUEST, array($this, 'onKernelRequest'), 128);
if (!isset($app['session.storage.options'])) {
$app['session.storage.options'] = array();
}
if (!isset($app['session.default_locale'])) {
$app['session.default_locale'] = 'en';
}
}
public function onKernelRequest($event)
......
......@@ -73,7 +73,9 @@ class SwiftmailerExtension implements ExtensionInterface
});
if (isset($app['swiftmailer.class_path'])) {
$app['autoloader']->registerPrefix('Swift_', $app['swiftmailer.class_path']);
require_once $app['swiftmailer.class_path'].'/Swift.php';
Swift::registerAutoload($app['swiftmailer.class_path'].'/swift_init.php');
}
}
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex\Extension;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Twig extension.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TwigCoreExtension extends \Twig_Extension
{
public function getFunctions()
{
return array(
'render' => new \Twig_Function_Method($this, 'render', array('needs_environment' => true, 'is_safe' => array('html'))),
);
}
public function render(\Twig_Environment $twig, $uri)
{
$globals = $twig->getGlobals();
$request = $globals['app']['request'];
$subRequest = Request::create($uri, 'get', array(), $request->cookies->all(), array(), $request->server->all());
if ($request->getSession()) {
$subRequest->setSession($request->getSession());
}
$response = $globals['app']->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
if (!$response->isSuccessful()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
}
return $response->getContent();
}
public function getName()
{
return 'silex';
}
}
......@@ -32,13 +32,14 @@ class TwigExtension implements ExtensionInterface
array(
'charset' => $app['charset'],
'debug' => $app['debug'],
'strict_variables' => !$app['debug'],
'strict_variables' => $app['debug'],
),
isset($app['twig.options']) ? $app['twig.options'] : array()
);
$twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']);
$twig->addGlobal('app', $app);
$twig->addExtension(new TwigCoreExtension());
if (isset($app['symfony_bridges'])) {
if (isset($app['url_generator'])) {
......
......@@ -47,4 +47,29 @@ class TwigExtensionTest extends \PHPUnit_Framework_TestCase
$response = $app->handle($request);
$this->assertEquals('Hello john!', $response->getContent());
}
public function testRenderFunction()
{
$app = new Application();
$app->register(new TwigExtension(), array(
'twig.templates' => array(
'hello' => '{{ render("/foo") }}',
'foo' => 'foo',
),
'twig.class_path' => __DIR__.'/../../../../vendor/twig/lib',
));
$app->get('/hello', function () use ($app) {
return $app['twig']->render('hello');
});
$app->get('/foo', function () use ($app) {
return $app['twig']->render('foo');
});
$request = Request::create('/hello');
$response = $app->handle($request);
$this->assertEquals('foo', $response->getContent());
}
}
Subproject commit 517c233679e2d0b19860609c0654847cb86c9602
Subproject commit 8c086d7560ae01f3f4a12d56df7862c67a6061b0
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment