Merge branch '2.7'
* 2.7: [travis] Do no tar in // [translation][performances] move loading resources into Translator initialize. allow to cache entity loader based on query builder [Assets] Make templating.helper.assets service available again for BC reasons [2.3] Static Code Analysis for Components
This commit is contained in:
commit
aeaa9a3680
11
.travis.sh
11
.travis.sh
@ -10,16 +10,15 @@ if [ -z "$components" ]; then
|
||||
exit 1
|
||||
fi
|
||||
echo '{"packages": {' > packages.json
|
||||
components=$(
|
||||
for c in $components; do
|
||||
for c in $components; do
|
||||
sed -i ':a;N;$!ba;s#^{\n\(\s*\)\("name"\)#{\n\1"repositories": \[{ "type": "composer", "url": "file://'$(pwd)'/" }\],\n\1\2#' $c/composer.json
|
||||
n=$(php -r '$n=json_decode(file_get_contents("'$c'/composer.json"));echo $n->name;')
|
||||
echo '"'$n'": {"'$branch'.x-dev": ' >> packages.json
|
||||
cat $c/composer.json >> packages.json
|
||||
echo '"version": "'$branch.x-dev'",\n "dist": {"type": "tar", "url": "file://'$(pwd)/$c'/package'$branch'.tar"}\n}},' >> packages.json
|
||||
echo $c
|
||||
done;
|
||||
)
|
||||
done;
|
||||
sed -i ':a;N;$!ba;s/\n}\n"/,\n "/g' packages.json
|
||||
sed -i ':a;N;$!ba;s/}},$/\n}}\n}}/' packages.json
|
||||
echo "$components" | parallel --gnu "cd {}; tar -cf package$branch.tar --exclude='package*.tar' *"
|
||||
for c in $components; do
|
||||
(cd $c && tar -cf package$branch.tar --exclude='package*.tar' *)
|
||||
done
|
||||
|
@ -330,6 +330,51 @@ UPGRADE FROM 2.x to 3.0
|
||||
* The `request` service was removed. You must inject the `request_stack`
|
||||
service instead.
|
||||
|
||||
* The `templating.helper.assets` was moved to `templating_php.xml`. You can
|
||||
use the `assets.package` service instead.
|
||||
|
||||
Before:
|
||||
|
||||
```php
|
||||
use Symfony\Component\Templating\Helper\CoreAssetsHelper;
|
||||
|
||||
class DemoService
|
||||
{
|
||||
private $assetsHelper;
|
||||
|
||||
public function __construct(CoreAssetsHelper $assetsHelper)
|
||||
{
|
||||
$this->assetsHelper = $assetsHelper;
|
||||
}
|
||||
|
||||
public function testMethod()
|
||||
{
|
||||
return $this->assetsHelper->getUrl('thumbnail.png', null, $this->assetsHelper->getVersion());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
After:
|
||||
|
||||
```php
|
||||
use Symfony\Component\Asset\Packages;
|
||||
|
||||
class DemoService
|
||||
{
|
||||
private $assetPackages;
|
||||
|
||||
public function __construct(Packages $assetPackages)
|
||||
{
|
||||
$this->assetPackages = $assetPackages;
|
||||
}
|
||||
|
||||
public function testMethod()
|
||||
{
|
||||
return $this->assetPackages->getUrl('thumbnail.png').$this->assetPackages->getVersion();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
* The `enctype` method of the `form` helper was removed. You should use the
|
||||
new method `start` instead.
|
||||
|
||||
|
@ -12,10 +12,16 @@
|
||||
namespace Symfony\Bridge\Doctrine\Form\Type;
|
||||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
|
||||
|
||||
class EntityType extends DoctrineType
|
||||
{
|
||||
/**
|
||||
* @var ORMQueryBuilderLoader[]
|
||||
*/
|
||||
private $loaderCache = array();
|
||||
|
||||
/**
|
||||
* Return the default loader object.
|
||||
*
|
||||
@ -27,6 +33,7 @@ class EntityType extends DoctrineType
|
||||
*/
|
||||
public function getLoader(ObjectManager $manager, $queryBuilder, $class)
|
||||
{
|
||||
if (!$queryBuilder instanceof QueryBuilder) {
|
||||
return new ORMQueryBuilderLoader(
|
||||
$queryBuilder,
|
||||
$manager,
|
||||
@ -34,6 +41,49 @@ class EntityType extends DoctrineType
|
||||
);
|
||||
}
|
||||
|
||||
$queryBuilderHash = $this->getQueryBuilderHash($queryBuilder);
|
||||
$loaderHash = $this->getLoaderHash($manager, $queryBuilderHash, $class);
|
||||
|
||||
if (!isset($this->loaderCache[$loaderHash])) {
|
||||
$this->loaderCache[$loaderHash] = new ORMQueryBuilderLoader(
|
||||
$queryBuilder,
|
||||
$manager,
|
||||
$class
|
||||
);
|
||||
}
|
||||
|
||||
return $this->loaderCache[$loaderHash];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param QueryBuilder $queryBuilder
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getQueryBuilderHash(QueryBuilder $queryBuilder)
|
||||
{
|
||||
return hash('sha256', json_encode(array(
|
||||
'sql' => $queryBuilder->getQuery()->getSQL(),
|
||||
'parameters' => $queryBuilder->getParameters(),
|
||||
)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ObjectManager $manager
|
||||
* @param string $queryBuilderHash
|
||||
* @param string $class
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getLoaderHash(ObjectManager $manager, $queryBuilderHash, $class)
|
||||
{
|
||||
return hash('sha256', json_encode(array(
|
||||
'manager' => spl_object_hash($manager),
|
||||
'queryBuilder' => $queryBuilderHash,
|
||||
'class' => $class,
|
||||
)));
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return 'entity';
|
||||
|
@ -11,7 +11,11 @@
|
||||
|
||||
namespace Symfony\Bridge\Doctrine\Tests\Form\Type;
|
||||
|
||||
use Symfony\Bridge\Doctrine\Form\DoctrineOrmTypeGuesser;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;
|
||||
use Symfony\Component\Form\FormBuilder;
|
||||
use Symfony\Component\Form\Forms;
|
||||
use Symfony\Component\Form\Test\TypeTestCase;
|
||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity;
|
||||
use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity;
|
||||
@ -22,6 +26,7 @@ use Symfony\Bridge\Doctrine\Form\DoctrineOrmExtension;
|
||||
use Doctrine\ORM\Tools\SchemaTool;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
|
||||
use Symfony\Component\PropertyAccess\PropertyAccess;
|
||||
|
||||
class EntityTypeTest extends TypeTestCase
|
||||
{
|
||||
@ -766,6 +771,68 @@ class EntityTypeTest extends TypeTestCase
|
||||
));
|
||||
}
|
||||
|
||||
public function testLoaderCaching()
|
||||
{
|
||||
$entity1 = new SingleIntIdEntity(1, 'Foo');
|
||||
$entity2 = new SingleIntIdEntity(2, 'Bar');
|
||||
$entity3 = new SingleIntIdEntity(3, 'Baz');
|
||||
|
||||
$this->persist(array($entity1, $entity2, $entity3));
|
||||
|
||||
$repository = $this->em->getRepository(self::SINGLE_IDENT_CLASS);
|
||||
$qb = $repository->createQueryBuilder('e')->where('e.id IN (1, 2)');
|
||||
|
||||
$entityType = new EntityType(
|
||||
$this->emRegistry,
|
||||
PropertyAccess::createPropertyAccessor()
|
||||
);
|
||||
|
||||
$entityTypeGuesser = new DoctrineOrmTypeGuesser($this->emRegistry);
|
||||
|
||||
$factory = Forms::createFormFactoryBuilder()
|
||||
->addType($entityType)
|
||||
->addTypeGuesser($entityTypeGuesser)
|
||||
->getFormFactory();
|
||||
|
||||
$formBuilder = $factory->createNamedBuilder('form', 'form');
|
||||
|
||||
$formBuilder->add('property1', 'entity', array(
|
||||
'em' => 'default',
|
||||
'class' => self::SINGLE_IDENT_CLASS,
|
||||
'query_builder' => $qb,
|
||||
));
|
||||
|
||||
$formBuilder->add('property2', 'entity', array(
|
||||
'em' => 'default',
|
||||
'class' => self::SINGLE_IDENT_CLASS,
|
||||
'query_builder' => $qb,
|
||||
));
|
||||
|
||||
$formBuilder->add('property3', 'entity', array(
|
||||
'em' => 'default',
|
||||
'class' => self::SINGLE_IDENT_CLASS,
|
||||
'query_builder' => $qb,
|
||||
));
|
||||
|
||||
$form = $formBuilder->getForm();
|
||||
|
||||
$form->submit(array(
|
||||
'property1' => 1,
|
||||
'property2' => 1,
|
||||
'property3' => 2,
|
||||
));
|
||||
|
||||
$reflectionClass = new \ReflectionObject($entityType);
|
||||
$reflectionProperty = $reflectionClass->getProperty('loaderCache');
|
||||
$reflectionProperty->setAccessible(true);
|
||||
|
||||
$loaders = $reflectionProperty->getValue($entityType);
|
||||
|
||||
$reflectionProperty->setAccessible(false);
|
||||
|
||||
$this->assertCount(1, $loaders);
|
||||
}
|
||||
|
||||
protected function createRegistryMock($name, $em)
|
||||
{
|
||||
$registry = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
|
||||
|
@ -40,6 +40,7 @@ class FrameworkExtension extends Extension
|
||||
*
|
||||
* @param array $configs
|
||||
* @param ContainerBuilder $container
|
||||
*
|
||||
* @throws LogicException
|
||||
*/
|
||||
public function load(array $configs, ContainerBuilder $container)
|
||||
@ -686,6 +687,8 @@ class FrameworkExtension extends Extension
|
||||
foreach ($dirs as $dir) {
|
||||
$container->addResource(new DirectoryResource($dir));
|
||||
}
|
||||
|
||||
$files = array();
|
||||
$finder = Finder::create()
|
||||
->files()
|
||||
->filter(function (\SplFileInfo $file) {
|
||||
@ -695,10 +698,15 @@ class FrameworkExtension extends Extension
|
||||
;
|
||||
|
||||
foreach ($finder as $file) {
|
||||
// filename is domain.locale.format
|
||||
list($domain, $locale, $format) = explode('.', $file->getBasename(), 3);
|
||||
$translator->addMethodCall('addResource', array($format, (string) $file, $locale, $domain));
|
||||
if (!isset($files[$locale])) {
|
||||
$files[$locale] = array();
|
||||
}
|
||||
|
||||
$files[$locale][] = (string) $file;
|
||||
}
|
||||
|
||||
$translator->replaceArgument(4, $files);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@
|
||||
<parameter key="templating.loader.cache.class">Symfony\Component\Templating\Loader\CacheLoader</parameter>
|
||||
<parameter key="templating.loader.chain.class">Symfony\Component\Templating\Loader\ChainLoader</parameter>
|
||||
<parameter key="templating.finder.class">Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinder</parameter>
|
||||
<parameter key="templating.helper.assets.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
@ -58,5 +59,14 @@
|
||||
</service>
|
||||
|
||||
<service id="templating.loader" alias="templating.loader.filesystem" />
|
||||
|
||||
<!--
|
||||
This service will be moved to templating_php.xml in version 3.0, it exists here for BC reasons.
|
||||
-->
|
||||
<service id="templating.helper.assets" class="%templating.helper.assets.class%">
|
||||
<tag name="templating.helper" alias="assets" />
|
||||
<argument /> <!-- default package -->
|
||||
<argument type="collection" /> <!-- named packages -->
|
||||
</service>
|
||||
</services>
|
||||
</container>
|
||||
|
@ -15,7 +15,6 @@
|
||||
<parameter key="templating.helper.translator.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper</parameter>
|
||||
<parameter key="templating.helper.form.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper</parameter>
|
||||
<parameter key="templating.helper.stopwatch.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\StopwatchHelper</parameter>
|
||||
<parameter key="templating.helper.assets.class">Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper</parameter>
|
||||
<parameter key="templating.form.engine.class">Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine</parameter>
|
||||
<parameter key="templating.form.renderer.class">Symfony\Component\Form\FormRenderer</parameter>
|
||||
<parameter key="templating.globals.class">Symfony\Bundle\FrameworkBundle\Templating\GlobalVariables</parameter>
|
||||
@ -66,12 +65,6 @@
|
||||
<argument type="service" id="translator" />
|
||||
</service>
|
||||
|
||||
<service id="templating.helper.assets" class="%templating.helper.assets.class%">
|
||||
<tag name="templating.helper" alias="assets" />
|
||||
<argument /> <!-- default package -->
|
||||
<argument type="collection" /> <!-- named packages -->
|
||||
</service>
|
||||
|
||||
<service id="templating.helper.form" class="%templating.helper.form.class%">
|
||||
<tag name="templating.helper" alias="form" />
|
||||
<argument type="service" id="templating.form.renderer" />
|
||||
|
@ -44,6 +44,7 @@
|
||||
<argument key="cache_dir">%kernel.cache_dir%/translations</argument>
|
||||
<argument key="debug">%kernel.debug%</argument>
|
||||
</argument>
|
||||
<argument type="collection" /> <!-- translation resources -->
|
||||
</service>
|
||||
|
||||
<service id="translator.logging" class="Symfony\Component\Translation\LoggingTranslator" public="false">
|
||||
|
@ -232,18 +232,11 @@ abstract class FrameworkExtensionTest extends TestCase
|
||||
public function testTranslator()
|
||||
{
|
||||
$container = $this->createContainerFromFile('full');
|
||||
|
||||
$this->assertTrue($container->hasDefinition('translator.default'), '->registerTranslatorConfiguration() loads translation.xml');
|
||||
$this->assertEquals('translator.default', (string) $container->getAlias('translator'), '->registerTranslatorConfiguration() redefines translator service from identity to real translator');
|
||||
$resources = $container->getDefinition('translator.default')->getArgument(4);
|
||||
|
||||
$resources = array();
|
||||
foreach ($container->getDefinition('translator.default')->getMethodCalls() as $call) {
|
||||
if ('addResource' == $call[0]) {
|
||||
$resources[] = $call[1];
|
||||
}
|
||||
}
|
||||
|
||||
$files = array_map(function ($resource) { return realpath($resource[1]); }, $resources);
|
||||
$files = array_map(function ($resource) { return realpath($resource); }, $resources['en']);
|
||||
$ref = new \ReflectionClass('Symfony\Component\Validator\Validation');
|
||||
$this->assertContains(
|
||||
strtr(dirname($ref->getFileName()).'/Resources/translations/validators.en.xlf', '/', DIRECTORY_SEPARATOR),
|
||||
|
@ -0,0 +1 @@
|
||||
folder: répertoire
|
@ -95,13 +95,50 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
public function testTransWithCachingWithInvalidLocale()
|
||||
{
|
||||
$loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface');
|
||||
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), '\Symfony\Bundle\FrameworkBundle\Tests\Translation\TranslatorWithInvalidLocale');
|
||||
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), array(), 'loader', '\Symfony\Bundle\FrameworkBundle\Tests\Translation\TranslatorWithInvalidLocale');
|
||||
$translator->setLocale('invalid locale');
|
||||
|
||||
$this->setExpectedException('\InvalidArgumentException');
|
||||
$translator->trans('foo');
|
||||
}
|
||||
|
||||
public function testLoadRessourcesWithCaching()
|
||||
{
|
||||
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
|
||||
$resourceFiles = array(
|
||||
'fr' => array(
|
||||
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
|
||||
),
|
||||
);
|
||||
|
||||
// prime the cache
|
||||
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), $resourceFiles, 'yml');
|
||||
$translator->setLocale('fr');
|
||||
|
||||
$this->assertEquals('répertoire', $translator->trans('folder'));
|
||||
|
||||
// do it another time as the cache is primed now
|
||||
$translator = $this->getTranslator($loader, array('cache_dir' => $this->tmpDir), array(), 'yml');
|
||||
$translator->setLocale('fr');
|
||||
|
||||
$this->assertEquals('répertoire', $translator->trans('folder'));
|
||||
}
|
||||
|
||||
public function testLoadRessourcesWithoutCaching()
|
||||
{
|
||||
$loader = new \Symfony\Component\Translation\Loader\YamlFileLoader();
|
||||
$resourceFiles = array(
|
||||
'fr' => array(
|
||||
__DIR__.'/../Fixtures/Resources/translations/messages.fr.yml',
|
||||
),
|
||||
);
|
||||
|
||||
$translator = $this->getTranslator($loader, array(), $resourceFiles, 'yml');
|
||||
$translator->setLocale('fr');
|
||||
|
||||
$this->assertEquals('répertoire', $translator->trans('folder'));
|
||||
}
|
||||
|
||||
protected function getCatalogue($locale, $messages)
|
||||
{
|
||||
$catalogue = new MessageCatalogue($locale);
|
||||
@ -182,15 +219,17 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
return $container;
|
||||
}
|
||||
|
||||
public function getTranslator($loader, $options = array(), $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
|
||||
public function getTranslator($loader, $options = array(), $resources = array(), $loaderFomat = 'loader', $translatorClass = '\Symfony\Bundle\FrameworkBundle\Translation\Translator')
|
||||
{
|
||||
$translator = new $translatorClass(
|
||||
$this->getContainer($loader),
|
||||
new MessageSelector(),
|
||||
array('loader' => array('loader')),
|
||||
$options
|
||||
array($loaderFomat => array($loaderFomat)),
|
||||
$options,
|
||||
$resources
|
||||
);
|
||||
|
||||
if ('loader' === $loaderFomat) {
|
||||
$translator->addResource('loader', 'foo', 'fr');
|
||||
$translator->addResource('loader', 'foo', 'en');
|
||||
$translator->addResource('loader', 'foo', 'es');
|
||||
@ -198,6 +237,7 @@ class TranslatorTest extends \PHPUnit_Framework_TestCase
|
||||
$translator->addResource('loader', 'foo', 'pt_BR'); // Brazilian Portuguese
|
||||
$translator->addResource('loader', 'foo', 'fr.UTF-8');
|
||||
$translator->addResource('loader', 'foo', 'sr@latin'); // Latin Serbian
|
||||
}
|
||||
|
||||
return $translator;
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ class Translator extends BaseTranslator
|
||||
{
|
||||
protected $container;
|
||||
protected $loaderIds;
|
||||
protected $resourceFiles;
|
||||
|
||||
protected $options = array(
|
||||
'cache_dir' => null,
|
||||
@ -42,13 +43,15 @@ class Translator extends BaseTranslator
|
||||
* @param MessageSelector $selector The message selector for pluralization
|
||||
* @param array $loaderIds An array of loader Ids
|
||||
* @param array $options An array of options
|
||||
* @param array $resourceFiles An array of resource directories
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array())
|
||||
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array(), $resourceFiles = array())
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->loaderIds = $loaderIds;
|
||||
$this->resourceFiles = $resourceFiles;
|
||||
|
||||
// check option names
|
||||
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
|
||||
@ -66,6 +69,7 @@ class Translator extends BaseTranslator
|
||||
protected function initializeCatalogue($locale)
|
||||
{
|
||||
$this->initialize();
|
||||
$this->loadResources($locale);
|
||||
parent::initializeCatalogue($locale);
|
||||
}
|
||||
|
||||
@ -77,4 +81,19 @@ class Translator extends BaseTranslator
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function loadResources($locale)
|
||||
{
|
||||
$locales = array_merge(array($locale), $this->computeFallbackLocales($locale));
|
||||
foreach ($locales as $locale) {
|
||||
if (isset($this->resourceFiles[$locale])) {
|
||||
foreach ($this->resourceFiles[$locale] as $file) {
|
||||
// filename is domain.locale.format
|
||||
list($domain, $locale, $format) = explode('.', basename($file), 3);
|
||||
$this->addResource($format, $file, $locale, $domain);
|
||||
}
|
||||
unset($this->resourceFiles[$locale]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -123,8 +123,9 @@ class Response
|
||||
*/
|
||||
public function getHeader($header, $first = true)
|
||||
{
|
||||
$normalizedHeader = str_replace('-', '_', strtolower($header));
|
||||
foreach ($this->headers as $key => $value) {
|
||||
if (str_replace('-', '_', strtolower($key)) == str_replace('-', '_', strtolower($header))) {
|
||||
if (str_replace('-', '_', strtolower($key)) === $normalizedHeader) {
|
||||
if ($first) {
|
||||
return is_array($value) ? (count($value) ? $value[0] : '') : $value;
|
||||
}
|
||||
|
Reference in New Issue
Block a user