Symfony 2 - configuration to Command Container - php

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

Related

Symfony doesn't load my custom validator defined as service

I'm trying to inject EntityManagerInterface in my custom validator, but I have this error:
Attempted to load class "validator_check_client_id" from the global namespace.
Did you forget a "use" statement?
My code is:
CheckClientId.php
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
/**
* #Annotation
*/
class CheckClientId extends Constraint{
public $message = 'Test';
public function validatedBy(){
return 'validator_check_client_id';
}
}
CheckClientIdValidator.php
namespace App\Validator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class CheckClientIdValidator extends ConstraintValidator{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager) {
$this->entityManager = $entityManager;
}
public function validate($value, Constraint $constraint){
//Todo
return;
}
}
services.yaml
validator.check_client_id:
class: App\Validator\CheckClientIdValidator
autowire: false
arguments:
$entityManager: '#doctrine.orm.entity_manager'
tags:
- {
name: validator.constraint_validator,
alias: validator_check_client_id,
}
Should I do an extra step to register my service? I think that symfony cannot find it because it is not properly registered.
I also tried to do what this answer says symfony validator as service not working, but I get a similar error:
Attempted to load class "app.validator.blog.slug_unique" from the global namespace.
Did you forget a "use" statement?
Do you have any idea what is happening?
Thank you for your help!
Thanks to autowire and autoconfig, all you really have to do is to get your class names right. No changes to services.yaml. Here is a working example using a fresh 5.2 project.
# src/Validator/CheckClientId.php
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
/** #Annotation */
class CheckClientId extends Constraint
{
public $message = 'CheckClientId';
}
# =============================
# src/Validator/CheckClientIdValidator
namespace App\Validator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
class CheckClientIdValidator extends ConstraintValidator
{
public function __construct(private EntityManagerInterface $entityManager)
{
}
public function validate($value, Constraint $constraint)
{
dump('validate ' . $value);
throw new UnexpectedValueException($value, 'string');
}
}
# ===============================
# Test Entity
# src/Entity/Client.php
namespace App\Entity;
use App\Validator as AcmeAssert;
class Client
{
/** #AcmeAssert\CheckClientId */
public $id = 42;
}
# ====================================
# Test Command
# src/Command/ValidateCommand
namespace App\Command;
use App\Entity\Client;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class ValidateCommand extends Command
{
protected static $defaultName = 'test:validator';
public function __construct(private ValidatorInterface $validator)
{
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$client = new Client();
$errors = $this->validator->validate($client);
echo $errors;
return Command::SUCCESS;
}
}
Get the test command working and move on from there.

pass repository to class from Command Symfony

I have a code in Symfony 3.2, where I am trying to pass repository from Commmand class to my custom class, where I am running the websockets
<?php
namespace MyBundle\Command;
use MyBundle\Server\Notification;
use Ratchet\Http\HttpServer;
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ServerCommand extends ContainerAwareCommand
{
/**
* Configure a new Command Line
*/
protected function configure()
{
$this->setName('Project:notification:server')
->setDescription('Start the notification server.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getApplication()->getKernel()->getContainer()->get('My.repository.car');‏
$server = IoServer::factory(new HttpServer(
new WsServer(
new Notification($this->getContainer(),$em)
)
), 8080);
$server->run();
}
}
for All my db queries I am using repositories and there is no Entity. I have done this before, and it was working fine.
But now when I run command from terminal I am getting this error
Parse error: parse error in
/Applications/XAMPP/xamppfiles/htdocs/lavtaxi/src/MyBundle/Command/ServerCommand.php
on line 28
on line 28 there is
"$server = IoServer::factory(new HttpServer( " part
this is my services.yml
services:
My.repository.car:
class: MyBundle\Repository\CarRepository
arguments: ["#doctrine.orm.entity_manager"]
and this is Notification.php where I am trying to get data and send via sockets
<?php
namespace MyBundle\Server;
use Doctrine\ORM\EntityManager;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Notification implements MessageComponentInterface
{
protected $connections = array();
private $users = array();
protected $container;
private $drivers = array();
private $repository = null;
public function __construct(ContainerInterface $container, $em)
{
$this->repository = $em;
$this->container = $container;
$this->users = [];
$this->drivers = [];
}
/**
* A new websocket connection
*
* #param ConnectionInterface $conn
*/
public function onOpen(ConnectionInterface $conn)
{
$this->connections[] = $conn;
$this->users[$conn->resourceId] = $conn;
$this->drivers = $this->repository->getCarsData();
$conn->send('..:: Hello from the Notification Center ::..');
$conn->send(json_encode($this->drivers));
echo "New connection \n";
}
}
Error is disappears when I remove the repository calling part above, so I know that problem is not on IoServer

Error when creating config parameter in bundle

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();
}

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