Symfony2 bundle configuration parameters - symfony

My team develops a very large e-commerce project. Up to now, we had something like 40 configuration parameters in that bundle and I didn't know their final relations nor had a way to structure them properly, so we ended up using the standard parameters structure in the app/config.yml file. But, somewhere along the way I decided to move the config to the semantic configuration structure - so I can validate it, have a clean structure, easily extend it by means of bundle inheritance and so on.
The problem is I can't import our bundle parameters to another bundle config, now.
Here's what I could do before:
(...)
parameters:
company.bundle.email.user: user#domain.com
company.bundle.email.pass: #MyBeAUtiFULpA55w0Rd!
(...)
swiftmailer:
(...)
username: %company.bundle.email.user%
password: %company.bundle.email.pass%
Here's what I'm trying to do now and it doesn't work at all:
(...)
company_bundle:
(...)
settings:
(...)
mailer:
(...)
user: user#domain.com
pass: #MyBeAUtiFULpA55w0Rd!
(...)
swiftmailer:
(...)
username: %company_bundle.settings.mailer.user%
password: %company_bundle.settings.mailer.pass%
(...)
Of course the company_bundle node contains the semantic configuration, processed by the DIC and defined in Company\Bundle\DependencyInjection\Configuration.php.
All I end up with for now is the following error:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "company_bundle.settings.mailer.user".
So the question is: how to make my bundle parameters accessible in another bundle configuration parameters (I've already taken care of including my bundle configuration always before the Swiftmailer bundle)?
NOTICE: Swiftmailer bundle configuration is shown here only for demonstration purposes, I want to be able to reuse my bundle parameters across some other bundles as well.
What's the motivation behind that?
Well I want our product to be configured by a person who doesn't know Symfony well - so she must be able to open our bundle config file and just tweak the parameters, which will then be used in some Symfony bundles we use. So I want all application configuration to be accessible in our bundle's semantic conf, since I'd want not to expose the whole configuration to that person (I've already divided everything into separate config files, one of them containing the configuration for our bundle, the rest - for all the other ones).
So, is what I'm asking even possible?

As the parameters are to be modified in each installation (they probably differ at least between development and production environments), so (as Sgoettschkes said in comment) external file is the best solution.
In Symfony2 by default place for such parameters is parameters.yml file located in app/config/ dir.
In standard installation you can find for example such entries:
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
(...)
These params are used in config.yml this way:
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
(...)
So you can define your params (in parameters.yml):
parameters:
(...)
company_bundle_email_user: user#domain.com
company_bundle_email_pass: #MyBeAUtiFULpA55w0Rd!
and use them (in config.yml):
swiftmailer:
(...)
username: %company_bundle_email_user%
password: %company_bundle_email_pass%

Related

Exclude fields from FOSUserBundle using JMS Serializer working on dev but not on prod environment

I would like to exclude fields when exposing my API from my user class that extends FOSUser.
I've setup JMS Serializer on the global config file and created a FOSUB config to only expose the fields I need.
Global Config:
app/config/config.yml
jms_serializer:
metadata:
directories:
FOSUB:
namespace_prefix: "FOS\\UserBundle"
path: "#AppBundle/Resources/config/serializer/fos"
FOS config file:
src/AppBundle/Resources/config/serializer/fos/Model.user.yml
FOS\UserBundle\Model\User:
exclusion_policy: ALL
properties:
id:
expose: true
email:
expose: true
roles:
expose: true
This config is working perfectly on my local machine however it doesn't work when deployed on prod. Both use same stack, my guess is that on prod somehow the serializer can't find FOS config file.
Any help would be much appreciated.
The issue was somehow related to the naming of the config file.
While in local (macos) the file name Model.user.yml was working, in production (centos) it didn't work. So I had to rename the file to Model.User.yml then it worked fine on both.
I tried to find some documentation related to this issue but couldn't find any.
Take away: Make sure that the config file name represent exactly the entity you want to override.

Provide Doctrine with custom cache factory for second level cache

Background: we're using Symfony 3.1 + Doctrine 2.5.5 + symfony doctrine bundle.
While trying to enable second level caching for our entities, we have encountered the following issue. If we use NONSTRICT_READ_WRITE, everything works fine. However, when we tried to use READ_WRITE, we got the following error
0)
Type error: Argument 2 passed to Doctrine\ORM\Cache\Persister\Entity\ReadWriteCachedEntityPersister::__construct() must be an instance of Doctrine\ORM\Cache\ConcurrentRegion, instance of Doctrine\ORM\Cache\Region\DefaultRegion given, called in vendor/doctrine/orm/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php on line 133
As far as I understood, we need to actually implement our own version of ConcurrentRegion and CacheFactory to make it work (FileLockRegion does not suit us due to its usage of file system to handle cache locks). So I wrote those implementations, but the main issue now lies in following: I cannot find where to put my custom factory class' name in the configuration. We have tried the following locations in config:
1)
doctrine:
orm:
auto_generate_proxy_classes: "%kernel.debug%"
default_entity_manager: master
second_level_cache:
default_cache_factory:
class: AppBundle\Cache\MyCacheFactory
This fails due to
Unrecognized option "second_level_cache" under "doctrine.orm"
even though in our other project using Symfony 2.8 option "second_level_cache" does not throw any errors.
So we went to doctrine bundle code and found the following node description (vendor/doctrine/doctrine-bundle/DependencyInjection/Configuration.php:492)
->arrayNode('second_level_cache')
->children()
->append($this->getOrmCacheDriverNode('region_cache_driver'))
->scalarNode('region_lock_lifetime')->defaultValue(60)->end()
->booleanNode('log_enabled')->defaultValue($this->debug)->end()
->scalarNode('region_lifetime')->defaultValue(0)->end()
->booleanNode('enabled')->defaultValue(true)->end()
->scalarNode('factory')->end()
->end()
So we decided we should try this config in our master entity manager section:
2)
second_level_cache:
region_cache_driver:
type: memcache
enabled: true
log_enabled: true
factory: AppBundle\Cache\MyCacheFactory
regions:
hour_region:
lifetime: 3600
However, even though this config is considered valid, when we actually try to access the entity with configured caching, we get the error 0), which makes us think that this option is being ignored by doctrine/symfony.
Is there any way to do it via .yml config at all? Doctrine docs only propose to implement CacheFactory and provide a PHP code example, but it's still quite unclear where should this PHP code go, even if we decide to abandon the idea of putting our class in .yml config and go the PHP way.
Use type - filelock for configurate FilelockRegion
regions:
default:
cache_driver:
type: service
id: 'Doctrine\Common\Cache\RedisCache'
lifetime: 3600
type: filelock

Snc_redis exception when loading

I'm getting the following exception:
InvalidConfigurationException: The child node "clients" at path "snc_redis" must be configured.
I use Symfony 2.3 SncRedisBundle 1.1.x-dev
In config.yml I have
clients:
default:
type: predis
alias: default
dsn: redis://localhost
logging: %kernel.debug%
And I've followed the docs here for installation...
Any idea what would cause this ?
The bundle is looking for configuration values under snc_redis root (as you can see here), which you did not provide.
Now, all this wouldn't normally be a problem, but the clients child node is mandatory (as you can see here), which causes the error above.
Simply make the root configuration node in your config.yml to be snc_redis and the problem will go away. You probably simply missed it when doing copy-paste.

Unable to load an entity that is not related to a bundle

I am building a company Symfony2 library that includes several bundles and a library of classes that can be used by any code. The plan is to eventually turn this into an internal package and install it into projects via composer so that it will live in the vendor directory.
One component of the library has a pair of database entity classes and a single repository class associated with it.
Because the only logic associated with these classes is contained in the repository class, it doesn't seem to make sense to to surround it with a bundle.
I have, as yet, been unable to get Doctrine to load this repository. It initially failed with a
The class 'Dplh\Library\DplhEnum\Entity\EnumGroupRepository' was not found in the chain configured namespaces Dplh\DplhSecurityBundle\Entity
exception. (The reference to my security bundle was initially rather confusing. Turns out that this is Doctrine helping out by listing all of the known entity namespaces (see Symfony error The class XXX was not found in the chain configured namespaces XXX)).
As I understand it, this happens because Doctrine is configured to use auto-mapping and expects all entities to be defined in src/WhateverBundle/Entity directories.
This can be changed with additional Doctrine configuration for specific mappings in config.yml (http://zalas.eu/how-to-store-doctrine-entities-outside-of-a-symfony-bundle/).
So far, I have been unable to get this to work either. I expect it's a minor config thing somewhere. I added the following to config.yml:
doctrine:
dbal:
# ...
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
mappings:
DplhEnum:
type: annotation
is_bundle: false
dir: %kernel.root_dir%/../src/Dplh/Library/DplhEnum/Entity
prefix: Dplh\Library\DplhEnum\Entity
alias: DplhEnum
I get two different exceptions depending on my call to getRepository().
// Using the fully-qualified class name.
$this->getEntityManager()->getRepository('Dplh\Library\DplhEnum\Entity\EnumGroup' )
This throws
Attempted to load class "EnumGroupRepository" from namespace "Dplh\DplhEnumBundle\Entity".
Did you forget a "use" statement for another namespace?
// Using the alias.
$this->getEntityManager()->getRepository('DplhEnum\EnumGroup' )
this throws
Class 'DplhEnum\EnumGroup' does not exist
I have verified that the EnumGroupRepository.php file is in the src/Dplh/Library/DplhEnum/Entity directory and the EnumGroupRepository class is in the Dplh\Library\DplhEnum\Entity namespace and that they are in the correct directory.

How to use a different console configuration in Symfony

I followed the guideline on how to expose a semantic configuration for a bundle and configured it in my app/config.yml (through parameters.yml).
My bundle also contains some console commands. Right now this command either uses the dev or prod configuration, which is fine.
But how can I make the console commands use an additional configuration file that sets some things different than in config.yml?
E.g.
#app/config.yml
imports:
- { resource: parameters.yml }
foo:
view_mode: %view_mode%
and
#app/parameters.yml
parameters:
view_mode: 1
How can I make it e.g. use a different parameters.yml
#app/parameters_console.yml
parameters:
view_mode: 2
when called through the console? A new environment is not what I want here.
I think you need to create a custom environement
You just have to create a config_console.yml in your app/config folder and override the configuration you need.
imports:
- { resource: config_dev.yml }
foo:
view_mode: 2
Then in your application, just run
php app/console --env=console
This will run your application with default configuration of dev and with foo.view_mode = 2
You may want to note that it will create a new cache folder named console

Resources