0
votes

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).

2

2 Answers

1
votes

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.

0
votes

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).