Error when creating config parameter in bundle - php

I obtained this error when I want to create a config paramter to a bundle.
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
There is no extension able to load the configuration for
"mrc_morales_tyre"
This is the code:
app/config/config.yml
mrc_morales_tyre:
db_driver: orm
Configuration.php
namespace MrcMorales\TyreBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {#inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('mrc_morales_tyre');
$supportedDrivers = array('orm');
$rootNode
->children()
->scalarNode('db_driver')
->validate()
->ifNotInArray($supportedDrivers)
->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($supportedDrivers))
->end()
->end()
->end();
return $treeBuilder;
}
}
TyreExtension.php
namespace MrcMorales\TyreBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Config\Definition\Processor;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class TyreExtension extends Extension
{
/**
* {#inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
}
}
TyreBundle.php
namespace MrcMorales\TyreBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use MrcMorales\TyreBundle\DependencyInjection\Compiler\ValidationPass;
class TyreBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new ValidationPass());
}
}
ValidationPass is to load validation.yml in not usual folder and it works.
Thanks

Resolved:
Symfony by default expects than config var name is the name of extension, in this case tyre.
Then I need to change the Extension name to MrcMoralesTyreExtension but then we need override the method getContainerExtension() in:
TyreBundle.php
public function getContainerExtension()
{
return new MrcMoralesTyreExtension();
}

Related

Symfony not load translation

I have a Symfony web application written in Symfony 2.8.
All translations work correctly in dev mode ,But the translations of second language not loading in production mode.
If I enable debug in app.php , translations will load completely.
$kernel = new AppKernel('prod', false);
TO
$kernel = new AppKernel('prod', true);
But it is not a good choice.
My config.yml is:
parameters:
locale: fa
framework:
#esi: ~
translator: { fallbacks: ["%locale%" , en] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
I put the messages.fa.yml and messages.en.yml files in app directory.
Try follow my instructions:
1) Move your trans.yml files into src->nameBundle->Resources->translations
2) In src->nameBunndle create or update DependencyInjection folder
3) In DependencyInjection folder create Configuration.php and NameExtension.php where name is your name of bundle.
4) Code Configuration.php :
<?php
namespace NameBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {#inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('name'); // name you bundle
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
return $treeBuilder;
}
}
5) Code NameExtension.php :
<?php
namespace NameBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class NameExtension extends Extension
{
/**
* {#inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}

Symfony2 error Looked for namespace "services", found none

i have an error starting with Symfony2.
InvalidArgumentException in YamlFileLoader.php line 356:
There is no extension able to load the configuration for "services" (in /Users/jm/Documents/Websites/www.brsymfony.dev/src/CalcBundle/DependencyInjection/../Resources/config/services.yml). Looked for namespace "services", found none
My services.yml look like and it has whitespaces correctly:
services:
Calculator:
class: CalcBundle\Services\Calculator
arguments:
- #doctrine.orm.default_entity_manager
ListOperations:
class: CalcBundle\Services\ListOperations
arguments:
- #doctrine.orm.default_entity_manager
Can anyone help me?
--
Yes, i have this code on CalcExtension.php inside DependencyInjection folder:
<?php
namespace CalcBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Class AppExtension
*/
class CalcExtension extends Extension
{
/**
* Loads a specific configuration.
*
* #param array $config An array of configuration values
* #param ContainerBuilder $container A ContainerBuilder instance
*
* #throws \InvalidArgumentException When provided tag is not defined in this extension
*
* #api
*/
public function load(array $config, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../Resources/config')
);
$loader->load('services.yml');
}
}
Thanks in advance :)
Have you created the extension on the DependencyInjection folder? Should be CalcBundle/DependencyInjection/CalcExtension.php
You have the documentation with examples here
http://symfony.com/doc/current/cookbook/bundles/extension.html
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
The services.yml should be located on CalcBundle/Resources/config/services.yml
Be careful because examples are creating parameters and services together into config.yml, you have to adapt it to your case (services.yml and parameters.yml separated, I would recommend your way)
You have the documentation with examples here
https://symfony.com/doc/current/book/service_container.html

Symfony 2 - configuration to Command Container

I have defined in app/config.yml sample:
module_my_module
key: value1
key2: value2
And a bundle with DependecyInjection Configuration.php:
<?php
namespace Modules\Bundle\MyModuleBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('module_configuration');
$rootNode->children()
->scalarNode('key')->defaultValue('')->end()
->scalarNode('key2')->defaultValue('')->end()
->end()
;
return $treeBuilder;
}
}
A problem is how to call config.Bundle/Command class:
namespace Modules\Bundle\MyBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
//use \Modules\Bundle\NodeSocketBundle\DependencyInjection as DependencyInjection;
class MyCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getParams("key.val"); // GEt a key from config.yml
}
}
Config.yml:
module_configuration
key: val
You must inject your params in the Container in the Extension Bundle Class definition.
In your case you must have something similar to:
<?php
namespace Modules\Bundle\MyModuleBundle\DependencyInjection;
class MyModuleExtension extends Extension
{
/**
* {#inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('...');
$container->setParameter('my_first_key', $config['key']);
$container->setParameter('my_second_key', $config['key2']);
}
}
Then you can access to the value, in your Commandclass as:
class MyCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getContainer()->getParameter("my_first_key");
}
}
Hope this help

override ResourceOwner class for Wordpress

I went to override WordpressResourceOwner into HWIOAuthBundle as it didn`t return user information response
I notice that every ResourceOwner work as Service and declared into oauth.xml
i had override registration twig into HWIOAuthBundle and create new bundle inherit from HWIOAuthBundle like answer of this question but I can`t work the same with WordpressResourceOwner
after searching I had found we can override class of WordpressResourceOwner Service
Steps:
1) create new ResourceOwner
EX:
namespace project\OAuthBundle\OAuth\ResourceOwner;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth2ResourceOwner;
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
/**
* Override WordpressResourceOwner class into HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\WordpressResourceOwner
*
* #author ahmedhamdy
*
*/
class WordpressResourceOwner extends GenericOAuth2ResourceOwner
{
/**
* {#inheritDoc}
*/
protected $paths = array(
'identifier' => 'ID',
'nickname' => 'username',
'realname' => 'display_name',
'email' => 'email',
'profilepicture' => 'avatar_URL',
);
/**
* {#inheritDoc}
*/
protected function doGetUserInformationRequest($url, array $parameters = array())
{
$apiAccessToken = $parameters['apiAccessToken'];
$headers = array(
0 => 'authorization: Bearer '.$apiAccessToken,
);
return $this->httpRequest($url,null,$headers);
}
/**
* {#inheritDoc}
*/
public function getUserInformation(array $accessToken, array $extraParameters = array())
{
$url = $this->normalizeUrl($this->options['infos_url'], array(
'access_token' => $accessToken['access_token']
));
$parameters = array(
'apiAccessToken' => $accessToken['access_token'],
);
$content = $this->doGetUserInformationRequest($url,$parameters)->getContent();
$response = $this->getUserResponse();
$response->setResponse($content);
$response->setResourceOwner($this);
$response->setOAuthToken(new OAuthToken($accessToken));
return $response;
}
/**
* {#inheritDoc}
*/
protected function configureOptions(OptionsResolverInterface $resolver)
{
parent::configureOptions($resolver);
$resolver->setDefaults(array(
'authorization_url' => 'https://public-api.wordpress.com/oauth2/authorize',
'access_token_url' => 'https://public-api.wordpress.com/oauth2/token',
'infos_url' => 'https://public-api.wordpress.com/rest/v1/me',
));
}
}
2) create CompilerPass class to override service class like Symfony2 documentation
EX:
namespace project\OAuthBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* OverrideWordpressResourceOwner
*
* #author ahmedhamdy
*/
class OverrideWordpressResourceOwner implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$definition = $container->getDefinition("hwi_oauth.abstract_resource_owner.wordpress");
$definition->setClass('ProfileTree\OAuthBundle\OAuth\ResourceOwner\WordpressResourceOwner');
}
}
3) we must override build method into Bundle class like Symfony2 documentation
EX:
namespace Project\OAuthBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use ProfileTree\OAuthBundle\DependencyInjection\Compiler\OverrideWordpressResourceOwner;
class ProfileTreeOAuthBundle extends Bundle
{
public function getParent()
{
return 'HWIOAuthBundle';
}
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new OverrideWordpressResourceOwner());
}
}
4) last step calling compile method for ContainerBuilder into Bundle Extension like Symfony2 documentation
EX:
namespace Project\OAuthBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class ProfileTreeOAuthExtension extends Extension
{
/**
* {#inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$container->compile();
}
}

Symfony2: my first service

I'm struggling with the error:
The autoloader expected class "ElectricAnimal\CardsinthepostBundle\Services\MyService" to be
defined in file
"/vhosts/domains/cardsinthepost.com/public/app/../src/ElectricAnimal/CardsinthepostBundle/Services/MyService.php".
You probably have a typo in the namespace or the class name.
But that class is defined in exactly that file!
/src/ElectricAnimal/CardsinthepostBundle/Services/MyService.php:
<?php
// Bundle/ElectricAnimalCardsinthepost/Services/MyService.php
namespace Bundle\ElectricAnimalCardsinthepost\Services;
class MyService
{
public function __construct()
{
}
public function sum($n1, $n2) {
return $n1 + $n2;
}
}
In my controller I have:
<?php
class DefaultController extends Controller
{
public function indexAction()
{
$number = $this->get('my_service')->sum(12, 37);
return new Response('<pre>' . $number . '</pre>');
}
}
?>
Additional info:
/src/ElectricAnimal/CardsinthepostBundle/DependencyInjection/ElectricAnimalCardsinthepostExtension.php:
<?php
namespace ElectricAnimal\CardsinthepostBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class ElectricAnimalCardsinthepostExtension extends Extension
{
/**
* {#inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
/src/ElectricAnimal/CardsinthepostBundle/Resources/config/services.yml:
services:
my_service:
class: ElectricAnimal\CardsinthepostBundle\Services\MyService
My answer:
MyService.php was using slightly the wrong namespace. Needed:
// Bundle/ElectricAnimal/Cardsinthepost/Services/MyService.php
namespace ElectricAnimal\CardsinthepostBundle\Services;
instead of
// Bundle/ElectricAnimalCardsinthepost/Services/MyService.php
namespace Bundle\ElectricAnimalCardsinthepost\Services;
Symfony2 naming drives me round the bend sometimes!
Had already constructed question so hope useful for someone to have this answer.

Categories