Symfony2 + AmazonS3 + KnpGaufretteBundle how to set key & secrete from DB - symfony

I want to set Amazon S3 settings from database and not from parameters.yml
can someone point me to right direction on how to use database (doctrine) parameters before symfony2 calls services.
#app/config/config.yml
services:
acme.aws_s3.client:
class: Aws\S3\S3Client
factory_class: Aws\S3\S3Client
factory_method: 'factory'
arguments:
-
key: %amazon_s3.key%
secret: %amazon_s3.secret%
# knp_gaufrette
knp_gaufrette:
adapters:
profile_photos:
aws_s3:
service_id: 'acme.aws_s3.client'
bucket_name: 'myBucket'
options:
directory: 'myDirectory'
acl: 'public-read'

Use your own service factory for acme.aws_s3.client service configuration.
services:
acme.aws_s3.client:
class: Aws\S3\S3Client
factory_class: My\S3ClientFactory
factory_method: createClient
arguments: [ #settings_repository ]
#settings_repository - any service that has access to db. E.g. doctrine entity repositry, or entire object manager.
My\S3ClientFactory::createClient - pretty much the same as native Aws\S3\S3Client::factory except that it would take params from db.
Hope this helpful.

Related

Symfony3 : Inject several #doctrine.orm.entity_manager in a service

I work with several connexion
Here is my config.yml :
doctrine:
dbal:
default_connection: connexion_1# specify the connexion used by default
connections:
connexion_1:
driver: "%database_driver1%"
....
connexion_2:
driver: "%database_driver2%"
...
I want to inject 2 connexion entity manager in a service.
In my service.yml i can just inject one entity_manager named "#doctrine.orm.entity_manager" . This is the default manager
#doctrine.orm.entity_manager argument
zip_command:
class: BackEndBundle\Command\ZipCommand
arguments:
- '#doctrine.orm.entity_manager'
tags:
- { name: console.command }
How can inject an other connexion ?
Option 1
According to documentation you should be able to inject your custom entity managers via doctrine.orm.{entity_manager_name_here}_entity_manager alias. You can find your entity managers' name under doctrine.orm.entity_managers in config.yml.
Option 2
If you want to have access to all of your entity managers in your service, you could inject the Doctrine\Common\Persistence\ManagerRegistry directly and use getManager(s) methods.

SonataAdmin Install issue

I trying to install SonataAdmin on my Symfony Project but at the end of the part-2 of the documention when i'm trying to go on "http://localhost:8000/admin/" I have a error : "You have requested a non-existent service "admin.category" in . (which is being imported from "C:\wamp64\www\Sonata/app/config\routing.yml"). Make sure there is a loader supporting the "sonata_admin" type."
I have no idea why, i give give my all my parameters code maybe it's can help you to understand my problem.
parameters:
#parameter_name: value
services:
# default configuration for services in *this* file
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
# makes classes in src/AppBundle available to be used as services
# this creates a service per class whose id is the fully-qualified class name
AppBundle\:
resource: '../../src/AppBundle/*'
# you can exclude directories or files
# but if a service is unused, it's removed anyway
exclude: '../../src/AppBundle/{Entity,Repository,Tests}'
# controllers are imported separately to make sure they're public
# and have a tag that allows actions to type-hint services
AppBundle\Controller\:
resource: '../../src/AppBundle/Controller'
public: true
tags: ['controller.service_arguments']
# add more services, or override services that need manual wiring
# AppBundle\Service\ExampleService:
# arguments:
# $someArgument: 'some_value'
`
The indentation is going wrong i add you a picture of this file. Service code
The sonata admin services must be public. In your config, you have default as public: false and that is why you get this error.
So you have 2 options:
Specify public: true for your admin service (in your example file)
Or the better way: create a new services file (eg admin.yml) where you dont use these defaults (the _defaults key with public: false). Public is true by default, so you don't have to specify that by _defaults. In this case you must import your new file in config.yml to work:
Top of app/config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }
- { resource: admin.yml }
app/admin.yml content:
services:
admin.category:
class: AppBundle\Admin\CategoryAdmin
arguments: [~, AppBundle\Entity\Category, ~]
tags:
- { name: sonata.admin, manager_type: orm, label: Category }
I think you did a mistake by writing your category.admin service in: Sonata/app/config/routing.yml,
instead of Sonata/src/YourAdminBundle/Resources/config/services.yml
Run this command on terminal. Because You might have missed installing
php composer.phar require sonata-project/doctrine-orm-admin-bundle
After This Add this code below to your AppKernel.php
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
// set up basic sonata requirements
// ...
new Sonata\DoctrineORMAdminBundle\SonataDoctrineORMAdminBundle(),
// ...
);
}

Connect Doctrine to memcached pool

Perhaps anybody know, how to connect Doctrine to memcached pool, to use it as a cache driver?
I've check official bundle documentation, and lot of another sources, but didn't find any examples of such connection.
Also due to source code, I could not find any options to use pool, but perhaps I miss something.
Didn't test, but the following should work:
in app/config/parameters.yml, set/add
parameters:
memcached.servers:
- { host: 127.0.0.1, port: 11211 }
- { host: 127.0.0.2, port: 11211 }
in app/config/config.yml set/add
services:
memcache:
# class 'Memcache' or 'Memcached', depending on which PHP module you use
class: Memcache
calls:
- [ addServers, [ %memcached.servers% ]]
doctrine.cache.memcached:
class: Doctrine\Common\Cache\MemcachedCache
calls:
- [setMemcached, [#memcached]]
in app/config/config_prod.yml, set
doctrine:
orm:
metadata_cache_driver:
type: service
id: doctrine.cache.memcached
query_cache_driver:
type: service
id: doctrine.cache.memcached
result_cache_driver:
type: service
id: doctrine.cache.memcached
As I said, I can't test it, but this is the combination of several known-to-work techniques.
UPDATE: solution updated based on CrazySquirrel's findings.
Thanks lxg for your ideas. I've build right configuration using your ideas. Please find correct service definition below:
application config:
result_cache_driver:
type: service
id: doctrine.cache.memcached
service.yml
memcached:
class: Memcached
calls:
- [ addServers, [ %memcached_servers% ]]
doctrine.cache.memcached:
class: Doctrine\Common\Cache\MemcachedCache
calls:
- [setMemcached, [#memcached]]

GuafretteBundle with Aws SDK2

I'm trying to use Gaufrette with Aws SDK2, and I'm getting the following error on a cache:clear after configuring:
[Aws\Common\Exception\InvalidArgumentException]
The provided signature value is not an instance of S3SignatureInterface
Here is my setup.
composer.json
"aws/aws-sdk-php": "2.6.*#dev"
config.yml
services:
src.aws_s3.client:
class: Aws\S3\S3Client
factory_class: Aws\S3\S3Client
factory_method: 'factory'
arguments:
key: %amazon_aws_key%
secret: %amazon_aws_secret_key%
region: US Standard
...
knp_gaufrette:
adapters:
media_adapter:
aws_s3:
service_id: 'src.aws_s3.client'
bucket_name: my.bucket
options:
directory: 'media'
GitHub: https://github.com/KnpLabs/KnpGaufretteBundle/issues/90
The problem was a mistake in the arguments of the service declaration (which I should've seen). It needs to be a collection like so:
config.yml
services:
src.aws_s3.client:
class: Aws\S3\S3Client
factory_class: Aws\S3\S3Client
factory_method: 'factory'
arguments:
- key: %amazon_aws_key%
secret: %amazon_aws_secret_key%

How to refer to parameters.yml in services.yml?

Here are params.
app/config/parameters.yml
parameters:
test:
enabled: true
validate: true
And this is service which I want to configure with test param from previous file.
MyBundle/Resources/config/services.yml
imports:
- { resource: "parameters.yml" }
parameters:
services:
my.form.type:
class: My\Bundle\Form\Type\MyType
arguments:
- %test%
Import doesn't work this way. How should I do it?
PS I know I can refer to #service_container. But the point is to pass array explicitly via yaml.
you can ommit ...
imports:
- { resource: "parameters.yml" }
parameters:
... parameters.yml should automatically be parsed and the parameters should be available for injection if you surround them with %.
Try:
services:
my.form.type:
class: My\Bundle\Form\Type\MyType
arguments: ["%test%"]
alias: my_form_type

Resources