I am trying to define service outside src, in App\Service to be exact. I know that service in Symfony is just a class therefore I should be able to put it anywhere.
That's what I've got at the moment:
app\config\services.yml
parameters:
serializer_service_class: App\Service\SerializeService
services:
app.serializer:
class: '%serializer_service_class%'
autowire: true
Service is just a simple serializing class:
SerializeService.php
<?php
namespace App\Service;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
/**
* Class SerializeService
* #package App\Service
*/
class SerializeService
{
/**
* #var Serializer
*/
private $serializer;
/**
* #var array
*/
private $encoders;
/**
* #var array
*/
private $normalizer;
/**
* SerializeService constructor.
*/
public function __construct()
{
$this->encoders = array(new XmlEncoder(), new JsonEncoder());
$this->normalizer = new ObjectNormalizer();
$this->normalizer->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
$this->serializer = new Serializer($this->normalizer, $this->encoders);
}
public function serialize2Json($item)
{
return $this->serializer->serialize($item, 'json');
}
}
Then I get this error message:
Attempted to load class "SerializeService" from namespace "App\Service".
Did you forget a "use" statement for another namespace?
I've really tried to find solution for this and I have tried different options. If it is somewhere on stackoverflow please point me to this post.
Thanks in advance.
As Pib Ball said your autoloader is probably missing some infos.
I've tried the following setup
myProjectRoot/
app/
config/
Resources/
Service/
SerializeService.php <- your service file
I have used your code, no changes.
I have the same error as you, then I've edited my composer.json as following
................
"autoload": {
"psr-4": {
"": "src/",
"App\\": "app/",
}
},
Then executed the command composer update and then your service should be loaded.
Hope it may helps you
Related
I have a service who intercepts the events of Sentry. I'm using a function called beforeSend.
I would to load a json file who contains the data to scrub or to keep. It's a service and I build my constructor with a similar way than others, but the "$this" context doesn't exist when I'm in the debugger in this function.
The kernel is in the Global variables, but I think it's not a good idea... I only would to get the root dir and it's all, but I don't find how to do this in this class... The constructor seems useless.
Someone could help me with a similar experience ?
EDIT :
Service :
namespace App\Services;
use Sentry\Event;
use Symfony\Component\HttpKernel\KernelInterface;
class SentryBeforeSendService
{
private static $rootDir;
public function __construct(KernelInterface $kernel)
{
self::$rootDir = $kernel->getRootDir();
}
/**
* Scrubs the value of all TARGET_PARAMETERS
* in the event's request.
*
* #param Event $event
*
* #return Event
*/
public function beforeSend(Event $event)
{
$rootDir = self::$rootDir;
$event->setRequest(self::scrubRequest($event->getRequest(), $rootDir));
try {
$composerData = json_decode(file_get_contents($rootDir.'/../composer.json'), true);
$version = $composerData['version'];
$event->setRelease($version);
} catch (\Exception $e) {
//do nothing
}
return $event;
}
/**
* Scrubs GET and POST parameters
*
* #param array $request
*
* #return array
*/
private static function scrubRequest(array $request, $rootDir)
{
// DO SOMETHING WITH $rootDir to scrub data with external file
}}
services.yml :
app.service.sentry_before_send:
class: 'App\Services\SentryBeforeSendService'
arguments: ['#kernel']
config_prod.yml :
sentry:
dsn: "%sentry_dsn%"
options:
environment: "%sentry_environment%"
# release: '%env(VERSION)%' #overridden from composer.json version in SentryBeforeSendService::beforeSend
before_send: 'App\Services\SentryBeforeSendService::beforeSend'
But it seems the construct never happened.
Thank you very much.
I was unable to inject a parameter, but I found a way to get the project_root from my method. Half victory ...
config_prod.yml:
sentry:
dsn: "%sentry_dsn%"
options:
environment: "%sentry_environment%"
# release: '%env(VERSION)%' #overridden from composer.json version in SentryBeforeSendService::beforeSend
before_send: 'App\Services\SentryBeforeSendService::beforeSend'
project_root: '%kernel.project_dir%'
Service :
<?php
namespace App\Services;
use Sentry\Event;
use Sentry\State\Hub;
class SentryBeforeSendService
{
private static $projectRoot;
/**
* Scrubs the value of all TARGET_PARAMETERS
* in the event's request.
*
* #param Event $event
*
* #return Event
*/
public function beforeSend(Event $event)
{
$sentryClient = Hub::getCurrent()->getClient();
self::$projectRoot = $sentryClient->getOptions()->getProjectRoot();
$event->setRequest(self::scrubRequest($event->getRequest()));
try {
$composerData = json_decode(file_get_contents(self::$projectRoot.'/composer.json'), true);
$version = $composerData['version'];
$event->setRelease($version);
} catch (\Exception $e) {
//do nothing
}
return $event;
}}
Hope it'll help someone else.
Thank you for answers.
You can inject the kernel.project_dir parameter in your service constructor with a named parameter:
In your services.yml file:
services:
_defaults:
bind:
string $kernelProjectDir: '%kernel.project_dir%'
Then in your service:
public function __construct(string $kernelProjectDir)
{
I just made the migration from symfony 4.1 to 4.4
I have this error:
Argument 1 passed to App\EventListener\KernelRequestListener::__construct() must be an instance of Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage, instance of Symfony\Component\Security\Core\Authentication\Token\Storage\UsageTrackingTokenStorage given, called in C:\xampp\htdocs\chat-project-symfony\var\cache\dev\Container06Mjwya\srcApp_KernelDevDebugContainer.php on line 1130
While if you look at my KernelRequestListener :
<?php
namespace App\EventListener;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
//..
class KernelRequestListener
{
private $tokenStorage;
/**
* KernelRequestListener constructor.
* #param TokenStorage $tokenStorage
* ...
*/
public function __construct(TokenStorage $tokenStorage/*...*/)
{
$this->tokenStorage = $tokenStorage;
//..
}
}
Here is my config/services.yaml file:
#...
services:
#..
App\EventListener\KernelRequestListener:
arguments: [ '#security.token_storage' ]
tags:
- { name: kernel.event_listener, event: kernel.request }
- { name: kernel.event_listener, event: kernel.response }
I don't know why symfony tell me that I'm using Symfony\Component\Security\Core\Authentication\Token\Storage\UsageTrackingTokenStorage while it's clearing written Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage
I already tried to clear the cache folder and also delete the cache folder and it didn't change.
How can I fix this ?
Thank you
I don't know why symfony tell me that I'm using Symfony\Component\Security\Core\Authentication\Token\Storage\UsageTrackingTokenStorage while it's clearing written Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage
It's not symfony but PHP's type checking feature. You are stating that your Listener wants a TokenStorage but symfony is passing to it different class, thus the error.
So, as #JaredFarrish pointed, you should be using TokenStorageInterface in your constructor, like this:
namespace App\EventListener;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
//..
class KernelRequestListener
{
private $tokenStorage;
/**
* KernelRequestListener constructor.
* #param TokenStorageInterface $tokenStorage
* ...
*/
public function __construct(TokenStorageInterface $tokenStorage/*...*/)
{
$this->tokenStorage = $tokenStorage;
//..
}
}
It's a common practice to use interfaces where they exists, because this way you will loose coupling with other classes and provide a way to unit test your classes.
Take a look: https://github.com/symfony/security-bundle/blob/master/Resources/config/security.xml#L22 they switched class for #security.token_storage service, because of deprecation. But when you use an interface you don't care of anything underlying, you just know that you will have your methods because of interface contract.
I fixed it changing this line:
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
With this one:
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface as TokenStorage;
I have a strange problem with Symfony2. I'm working on a project with 40-50 entities, and with one of them this code doesn't work:
$user = $em->getRepository('CompanyUserBundle:User')
->findOneBy([
'username' => $person->getUsername()
]);
I get the error Unknown Entity namespace alias 'CompanyUserBundle'.. The strange thing is that if I change my code to:
$user = $em->getRepository('Company\UserBundle\Entity\User')
->findOneBy([
'username' => $person->getUsername()
]);
It works perfectly... So, what's wrong here? I'm using the getRepository('Alias:Entity') construction all around the code, and there isn't any other problem...
I don't know if it's important, but User is a child entity from FOSUserBundle BaseUser class.
EDIT
Here is my User class (it's located at src/Company/UserBundle/Entity):
namespace Company\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* #ORM\Table(name="fos_user")
* #ORM\Entity
*/
class User extends BaseUser
{
/**
*
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
And here a part of my AppKernel.php:
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\SecurityBundle\SecurityBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
/* ... other, non interesting, bundles... */
new Company\UserBundle\UserBundle(),
new FOS\UserBundle\FOSUserBundle(),
new FR3D\LdapBundle\FR3DLdapBundle(),
new JMS\AopBundle\JMSAopBundle(),
new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
new JMS\DiExtraBundle\JMSDiExtraBundle($this),
);
}
}
More information: as #herr said in the comments, using
$user = $em->getRepository('UserBundle:User')
->findOneBy([
'username' => $person->getUsername()
]);
works fine... But I don't understand why. Why in this bundle the namespace alias is UserBundle instead of CompanyUserBundle? I know that this must be a really silly error, but I can't see it...
You can check in your UserBundle's Bundle class file it will with UserBundle.php instead of CompanyUserBundle.php.
Just try using
$em->getRepository('UserBundle:User')
instead
$em->getRepository('CompanyUserBundle:User')
I think it's namespace / BundleClassName may be only UserBundle not CompanyUserBundle in this case.
Thanks.
I got the same error but the reason was different, because Entity's were in different bundle/package.
The problem caused by the mapping misconfiguration. Changing alias to the correct one solved the issue
doctrine:
dbal:
# ...
orm:
# ...
mappings:
App:
# ...
prefix: 'Company\UserBundle\Entity'
alias: CompanyUserBundle # <-- this alias is used when you do CompanyUserBundle:User
for more information: https://symfony.com/doc/current/reference/configuration/doctrine.html
I want to make
behat.yml -
default:
extensions:
Behat\MinkExtension\Extension:
base_url: 'my-url'
a parameter pulled from parameters.yml... Is this possible? I made a mink_base_url parameter in parameters.yml and then added
imports:
- { resource: parameters.yml }
to behat.yml. No matter what I do, I get this
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
The service "behat.mink.context.initializer" has a dependency on a non-existent parameter "mink_base_url"
Behat configuration is in no way related to Symfony's. It's true that Behat uses Symfony's DI container, but it's a separate instance.
If wanted to implement it, you'd probably need to create your own Behat extension to support the imports section.
This worked for me with Symfony 3. Just omit base_url from behat.yml, and set it from the container parameters. Thanks to #DanielM for providing the hint.
<?php
use Behat\MinkExtension\Context\MinkContext;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FeatureContext extends MinkContext {
/**
* FeatureContext constructor.
* #param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* #BeforeScenario
*/
public function setUpTestEnvironment()
{
$this->setMinkParameter('base_url', $this->container->getParameter('my_url'));
}
}
It is possible to access the symfony parameters within behat yaml as using
- '%%name_of_the_parameter%%'
Double percentage sign (%%) does the trick.
If you just want to access base_url, you can get it once mink has been started.
$this->getMinkParameter('base_url');
Here's an example :
class AbstractBehatContext extends MinkContext {
/**
* The base url as set behat.yml
* #var bool
*/
protected $baseUrl;
/**
* #BeforeScenario
*/
public function getBaseUrl() {
$this->baseUrl = $this->getMinkParameter('base_url');
}
}
Note, this needs to be able to access Mink, so it won't work in __construct or in #BeforeSuite. Additionally #BeforeScenario will be called at the start of every scenario which is going to set it pointlessly a lot.
I want to get an array from a yaml file inside one of my services, and I am a little confused of how to inject the file to use in my services.yml.
# /path/to/app/src/Bundle/Resources/config/services.yml
parameters:
do_something: Bundle\DoSomething
yaml.parser.class: Symfony\Component\Yaml\Parser
yaml.config_file: "/Resources/config/config.yml" # what do I put here to win!
services:
yaml_parser:
class: %yaml.parser.class%
do_parsing:
class: %do_something%
arguments: [ #yaml_parser, %yaml.config_file% ]
In my service I have
# /path/to/app/src/Bundle/DoSomething.php
<?php
namespace Bundle;
use \Symfony\Component\Yaml\Parser;
class DoSemething
{
protected $parser;
protected $parsed_yaml_file;
public function __construct(Parser $parser, $file_path)
{
$this->parsed_yaml_file = $parser->parse(file_get_contents(__DIR__ . $file_path));
}
public function useParsedFile()
{
foreach($parsed_yaml_file as $k => $v)
{
// ... etc etc
}
}
}
This may be the completely wrong approach, if I should be doing something else please let me know!
First I'll explain why I implemented my solution for you to decide if this case is right for you.
I needed a way to easily load custom .yml files in my bundle (for lots of bundles) so adding a separate line to app/config.yml for every file seemed like a lot of hassle for every setup.
Also I wanted most of the configs to be already loaded by default so end-user wouldn't even need to worry about configuring most of the time, especially not checking that every config file is setup correctly.
If this seems like a similar case for you, read on. If not, just use Kris solution, is a good one too!
Back when I encountered a need for this feature, Symfony2 didnt't provide a simple way to achieve this, so here how I solved it:
First I created a local YamlFileLoader class which was basically a dumbed down Symfony2 one:
<?php
namespace Acme\DemoBundle\Loader;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Config\Loader\FileLoader;
/**
* YamlFileLoader loads Yaml routing files.
*/
class YamlFileLoader extends FileLoader
{
/**
* Loads a Yaml file.
*
* #param string $file A Yaml file path
*
* #return array
*
* #throws \InvalidArgumentException When config can't be parsed
*/
public function load($file, $type = null)
{
$path = $this->locator->locate($file);
$config = Yaml::parse($path);
// empty file
if (null === $config) {
$config = array();
}
// not an array
if (!is_array($config)) {
throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $file));
}
return $config;
}
/**
* Returns true if this class supports the given resource.
*
* #param mixed $resource A resource
* #param string $type The resource type
*
* #return Boolean True if this class supports the given resource, false otherwise
*
* #api
*/
public function supports($resource, $type = null)
{
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION) && (!$type || 'yaml' === $type);
}
}
Then I updated DIC Extension for my bundle (it's usually generated automatically if you let Symfony2 create full bundle architecture, if not just create a DependencyInjection/<Vendor&BundleName>Extension.php file in your bundle directory with following content:
<?php
namespace Acme\DemoBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Acme\DemoBundle\Loader\YamlFileLoader;
/**
* 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 AcmeDemoExtension extends Extension
{
/**
* {#inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
// until here everything is default config (for your DIC services)
$ymlLoader = new YamlFileLoader(new FileLocator(__DIR__.'/../Resources/config'));
$container->setParameter('param_name', $ymlLoader->load('yaml_file_name'))); // load yml file contents as an array
}
}
And now you can access/pass your yaml config as simple service parameter (i.e. %param_name% for services.yml)
I solved it this way:
Services.yml
#/path/to/app/src/Bundle/Resources/config/services.yml
parameters:
example.class: Path\To\Bundle\Service\Class
example.yaml_config_file: "%kernel.root_dir%/../src/Path/To/Bundle/Resources/config/config.yml"
services:
example_service:
class: %example.class%
arguments: [%example.yaml_config_file% ]
Service class
# /path/to/app/src/Bundle/Service/Example.php
<?php
namespace Bundle\Service;
use \Symfony\Component\Yaml\Yaml;
class Example
{
private $parsed_yaml_file;
public function __construct($yaml_config_file)
{
$this->parsed_yaml_file = Yaml::parse($yaml_config_file);
}
}
You can use the kernel.root_dir parameter:
parameters:
yaml.config_file: "%kernel.root_dir%/../src/Path/To/MyBundle/Resources/config/config.yml"
If you're using Symfony 3.3 or higher, you can now also use the new kernel.project_dir parameter.
This parameter points to the highest level directory containing a composer file.