How does composer know symfony environment - symfony

i try to deploy an app in "integration" environment but after the "composer update" command, the parameters.yml file generated doesn't contains may integration parameters
Here is what i did :
I created a "config_integ.yml" file in app/config with :
# app/config/config_integ.yml
imports:
- { resource: parameters_integ.yml }
- { resource: config.yml }
framework:
profiler: { only_exceptions: false }
the parameters_integ.yml file contains parameters for integration environment. example of the beggining :
parameters:
database_host: localhost
database_port: null
database_name: test_integ
database_user: root
the parameters.yml.dist contains these parameters with default values :
parameters:
database_host: 127.0.0.1
database_port: ~
database_name: symfony
database_user: root
How can i tell to composer to take the parameters_integ.yml file to build the parameter.yml file ? Because i will have a prod environment after, i can't write my parameters in parameters.yml.dist file.
Tell me if you need other informations
Thanks for your help

1) You should include composer.json and composer.lock in your VCS
2) You should not include parameters.yml in your VCS
3) When deploying Sf application you should use composer install
http://symfony.com/doc/current/deployment.html#c-install-update-your-vendors
difference between composer install and composer update (+ composer.lock): https://blog.engineyard.com/2014/composer-its-all-about-the-lock-file
4) I can think of a few ways for creating parameters.yml in the integration environment:
you manually create parameters.yml file and place it beside parameters.dist.yml
or
if there is no parameters.yml or parameters.yml has some missing parameters, when you run composer install, you will be prompted to enter missing parameters and parameters.yml will be created/updated during the composer install process
or
you can have an app/config/parameters folder containing parameters_prod.yml and parameters_integ.yml files, and one of those files is automatically copied into parameter.yml file during deployment (most complicated solution)
If you want integration environment to be accessible via a browser, you should also create a front controller for it. Copy the web/app.php file to web/app_integ.php and edit the environment to be integration:
// web/app_integ.php
// ...
// change just this line
$kernel = new AppKernel('integ', false);
// ...
More info at https://symfony.com/doc/current/configuration/environments.html#creating-a-new-environment
And the answer to the question from the title, "How does composer know symfony environment" - I think composer has no idea about Sf environment :)
The composer has require-dev option for packages that you do not need in your production, packages required for running tests (phpunit for example), some debugging stuff during development, etc.
For an example this is how one of my composer.json files looks like:
...
"require-dev": {
"sensio/generator-bundle": "^3.0",
"symfony/phpunit-bridge": "^3.0",
"doctrine/doctrine-fixtures-bundle": "^2.3",
"nelmio/alice": "^2.1",
"deployer/deployer": "^4.0"
},
...
If I run composer install --no-dev, these packages won't be installed in the vendor directory.
But, how Sf knows when to include/omit some packages in the app.
That is defined inside AppKernel.php.
In the composer.json, under "require-dev" key I defined that I don't want doctrine fixtures in production.
So, inside AppKernel.php I put these lines of code:
if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
...
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
}
And if I run my app in production mode, there will not be DoctrineFixturesBundle.

Best practice is to set your default parameters in your app/config/parameters.yml.dist file.
See this link:
http://symfony.com/doc/current/best_practices/configuration.html#canonical-parameters
When you run:
composer update
it will use the parameters.yml.dist and overwrite the parameters.yml with the values in the dist file.

Related

How to generate document with Symfony 3.4 using Symfony flex

With Symfony flex, one of the change is that there is no default bundle when using the symfony/skeleton.
I don't find a way to use the command doctrine:mongodb:generate:documents in this context.
Could you please tell me how to use it ?
Exemple:
php bin\console doctrine:mongodb:generate:documents App
2018-02-17T18:35:22+00:00 [error] Error thrown while running command "doctrine:mongodb:generate:documents AppBundle --document Test". Message: "No bundle AppBundle was found."
In DoctrineODMCommand.php line 87:
No bundle AppBundle was found.
doctrine:mongodb:generate:documents [--document [DOCUMENT]] [--no-backup] [-h|--help] [-q|--quiet] [-v|vv|vvv|--verbose] [-V|--version] [--ansi] [--no-ansi] [-n|--no-interaction] [-e|--env ENV] [--no-debug] [--] <command> <bundle>
This is how I setup my project
composer create-project symfony/skeleton:3.4 test
composer config "platform.ext-mongo" "1.6.16" && composer require "alcaeus/mongo-php-adapter"
composer require doctrine/mongodb-odm-bundle
Thanks
Regards,
Chris
There is a similar issue with doctrine orm and doctrine:generate:entities. The Doctrine team discourages users from using these commands and therefore does no longer want to maintain them.
I think the easiest workaround I've seen so far is:
Create a Symfony 3.3 style application using the Symfony installer.
Generate the entities in the AppBundle as you did before
Change the namespace from AppBundle to App.
Move the files into your Symfony 4 project.
Some of the Symfony team also set out to provide similar code generation in a MakerBundle. As far as I can tell there is nothing for generating ODM-style entities, but you could open an issue or contribute something for this yourself.
For reference see: https://github.com/doctrine/DoctrineBundle/issues/729
The MongoDBBundle requires you have "Bundle" but with Symfony flex you are using FrameworkBundle and not "AppBundle" unless you created it before, I think you have not created it, so it tells you that it does not exist.
You can not use the "FrameworkBundle" either beacause FrameworkBundle have other namspace and other base path, so the command to generate the documents does not know where the files are.
After a lot of time figuring out how to diry-fix it:
Create custom Bundle inside your project (EX: AppCoreBundle)
Define custom mapping inside packages/doctrine_mongodb.yaml (change type value if you use xml or yaml)
doctrine_mongodb:
auto_generate_proxy_classes: '%kernel.debug%'
auto_generate_hydrator_classes: '%kernel.debug%'
connections:
default:
server: '%env(MONGODB_URL)%'
options: {}
default_database: '%env(MONGODB_DB)%'
document_managers:
default:
auto_mapping: true
mappings:
AppCoreBundle:
is_bundle: true
type: annotation
dir: '/Document'
prefix: App\CoreBundle\Document
alias: AppCoreBundle
Change the directory "src" to "App" to avoid issues then update psr-4 namaspace inside composer.json: "App\\": "App/"
Move all Documents to the Bundle Document directory and change package names.
You will also have to change ALL references to "src" directory in your project, for example in services.yaml.
Then execute: rm -rf var/cache/* && composer install to force to clear cache and refs.
Then execute: php bin/console doctrine:mongodb:generate:documents AppCoreBundle
Important!
This solution solves compatibility problems with MongoODMBundle with Symfony 4 but it is not a correct way. The right fix involves modifying the behavior of the bundle or Symfony 4.

HWIOauthbundle with FOSUserbundle Integration error

Attempted to load class "Curl" from namespace "Buzz\Client". Did you
forget a "use" statement for another namespace? 500 Internal Server
Error - ClassNotFoundException
in app/cache/dev/appDevDebugProjectContainer.php at line 1809 -
protected function getHwiOauth_HttpClientService()
{
$this->services['hwi_oauth.http_client'] = $instance = new \Buzz\Client\Curl();
$instance->setVerifyPeer(false);
$instance->setTimeout(10);
When you asking a question please specify your operating system and versions of frameworks you are using (e.g. symfony 2). Here is an answer for windows and symfony2
1) Delete manually added folders
2) If you don't have composer installed, install it
For windows
For other read this
2) Install HWIOAuthBundle
HWIOAuthBundle readme.md
option i) Using composer
Open the symfony project directory where composer.json resides.
Open command prompt (In Windows: Shift key + Right click => open command window here)
Run this
composer require hwi/oauth-bundle
OR
option ii) Add dependencies to your composer.json (not recomended)
"require": {
// other dependencies will be here //
"hwi/oauth-bundle": "^0.4.0",
"friendsofsymfony/user-bundle": "^1.3"
}
then execute following command
composer install

Why am I getting "Cannot redeclare class" on Symfony2 + vagrant + moved vendors optimalization setup?

Recently I've been working on improveing my vagrant enviroment and I've stumbled upon Running the Symfony application on Vagrant without NFS below 100ms blog post.
I've moved the vendor dir outside the VM shared directory, as suggested, by createing /tmp/app/vendor dir inside the VM and setting
export COMPOSER_VENDOR_DIR=/tmp/app/vendor
export COMPOSER_BIN_DIR=/tmp/app/bin
export SF2_ENV=vagrant
enviroment variables before running composer install.
Now, with vendors correctly installed to another path, I've modified the app/autoload.php file to detect if the app is running inside the VM by looking for the SF2_ENV enviroment variable:
if (array_key_exists('SF2_ENV', $_SERVER) && $_SERVER['SF2_ENV'] === 'vagrant') {
// vagrant enviroment detected
$loader = require sys_get_temp_dir().'/app/vendor/autoload.php';
} else {
// default
$loader = require __DIR__.'/../vendor/autoload.php';
}
This worked fine for one project, however on another project I kept getting the
PHP Fatal error: Cannot redeclare class Gedmo\Loggable\Entity\MappedSuperclass\AbstractLogEntry in /var/www/public_html/vendor/gedmo/doctrine-extensions/lib/Gedmo/Loggable/Entity/MappedSuperclass/AbstractLogEntry.php on line 13
error when trying to initialize the app cache php app/console cache:clear -e=prod.
Why was this happening?
Note: I've anwsered myself, as this is a Q&A style question (a note to my future self).
I am posting this as a reminder to my future self or anyone else interested. The problem was becouse in the second project I used Gedmo library and in my config.yml I defined:
doctrine:
orm:
entity_managers:
default:
filters:
softdeleteable:
class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
enabled: true
mappings:
gedmo_translatable:
type: annotation
prefix: Gedmo\Translatable\Entity
dir: "%kernel.root_dir%/../vendor/gedmo/doctrine-extensions/lib/Gedmo/Translatable/Entity"
alias: GedmoTranslatable
is_bundle: false
So, the problem was clearly the %kernel.root_dir% part, which was hardcoded.
Solution
I've simply changed it to %vendor_dir% and defined this parameter in my parameters.yml - for production as vendor_dir: '%kernel.root_dir%/../vendor' and for local (dev) as vendor_dir: /tmp/app/vendor.
I already have problem with symfony2 project on Vagrant. But I resolve this without change location of /vendor directory.
Just disable the nfs share and locate your projects directly on /var/www
you can use this on vagrantFile :
config.vm.synced_folder '.', '/vagrant', disabled: true
Work with your Vagrant as like you work with a remote server...
What happening when you deploying in production environment your autoload.php file ? it's not very useful to change this only for dev environment (I don't speak only for this file but general idea).

why parameters.yml loses changes when I update dependencies using Composer?

When I run: php composer.phar update after adding a line to require-dev in composer.json, I observe that parameters.yml loses all the changes I made from it initial state (when Symfony2 standard edition is first installed). What are reasons behind this?
You should store your parameters in parameters.yml.dist, because parameters.yml is regenerated from the .dist file after each composer update.
The .dist file can be added to your VCS of choice and when someone pulls the changes, Symfony will check if there are any differences between parameters.yml.dist and local parameters.yml, will ask the user to provide a value for any new parameter and it will add it to local parameters.yml file.
The indeed of this behaviour is because the script want to remove outdated params.
If you need to keep outdated params you can use keep-outdated param in the configuration:
{
"extra": {
"incenteev-parameters": {
"keep-outdated": true
}
}
}
More info in the bundle's doc here

Symfony 2 how to create a parameters_dev.yml?

i just started coding with Symfony 2. On my local machine i setup my database using app/console doctrine:database:create and doctrine:schema:create which works great.
The problem i have is, local i have diffrent parameters defined in parameters.yml than on my production environment. How can i define paramters for dev and for prod? I've already tried to create a parameters_dev.yml but this did not work.
Should i maybe create a paramters.yml on my prod server and copy it after deployment so i dont have my DB password in version control?
parameters.yml should't be handled by version control.
You have to set it on ignore in .gitignore.
Only parameters.yml.dist has to be versioned
more over if you use symfony 2.3 and install vendors using composer on prod you will be prompt to enter all the setting from parameters.yml.dist so parameters.yml will be generated automatically
In Symfony 2.7 I had to add this to appKernel.php:
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getRootDir().'/config/parameters_'.$this->getEnvironment().'.yml');
$loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
}
And then create both parameters_dev.yml and parameters_prod.yml.
Don’t forget to add this:
imports:
- { resource: parameters.yml }
on the first line of each new ymls.

Resources