Symfony2 // [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException] unable to parse file…

When reorganizing my bundles, I decided to load my services from my bundle dependency injection.
My bundle name is “UserBundle” and my vendor name “xxx”.

So, I created an extension file from my bundle called “xxxUserExtension.php” in “UserBundle/DependencyInjection/”

This was my content file:

<?php

namespace xxx\UserBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * 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 xxxUserExtension 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('user.yml');
        $loader->load('forms.yml');        
        $loader->load('validation.xml');       
    }
}

And of course, I’ve got this error:

    [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
    Unable to parse file "C:\wamp\www\xxx\src\Headoo\UserBundle\DependencyInjection/../Resources/config\user.yml".

Maybe you want to know the cause, right?

This error simply occured because I was loading an yml file with an xmlFileLoader. Also, i was loading an xml file, we never have to load two different file types with a single file type loader.

Look here:

        $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('user.yml');
        $loader->load('forms.yml');
        $loader->load('validation.xml'); 

So to solve this error, you have to use only one extension for your services and choose the correct loader.
This is the correct file:

<?php

namespace xxx\UserBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * 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 xxxUserExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('user.yml');
        $loader->load('forms.yml');               
    }
}

Hope, this helped.

Symfony2 // [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException] unable to parse file…

What is dependency injection pattern, how to understand it?

Dependency Injection (DI) is a design pattern commonly used by many frameworks, and, is a good practice in development.

Understanding dependency injection is like a child game… really!

Let’s take an example with your phone battery. If you have an Iphone you probably have a phone integrated battery that you can’t remove, and, in many other smartphones, you have removable batteries.
All of these phones depend on a battery.

So, dependency injection is the fact of injecting any battery (dependency) in your phone without worrying about its name, its emplacement, its content. We also say that it helps reducing couplage.

Integrated batteries = no DI = high couplage.
Removable batteries = DI = low couplage.

Live example:

//No dependency injection, high code couplage
function phone (){
    $battery = new batterry();
    $battery->on();
}

//With dependency injection, low code couplage
function phone(Service $service){
    $battery = $service->get('battery');
    $battery->on();
}

Without dependency injection, your dependency is integrated inside your method, we say that the dependency and method are coupled. But, what if you need to change the battery class and what if you have dozens of methods like this? Surely you won’t want to update parts of your code everytime.

With dependency injection, you call a kind of service which is binded to a generic battery class. Everytime you’ll have to change the mother class, you’ll have to specify it in your service configuration without modifying your working code.

Edouard Kombo // @edouardkombo. Never stop learning.

What is dependency injection pattern, how to understand it?

Cabin principle or how to define an object oriented code

CABIN (Concrete ABstraction of INterfaces) is just a principle sticked to the notion of object i’ve defined here https://creativcoders.wordpress.com/2014/05/05/what-exactly-is-an-object-oriented-code/ few days ago.

Let’s remind it.
I’ve defined an object oriented code like a concrete class that strictly implements interfaces and extend abstract classes.

Ok, so you’re telling us a class that doesn’t implement an interface and extend abstract classes is not really an object?
Yes, exactly, and let me explain you why.
You surely agree with me that your smartphone is simply an object that help you communicate with the world, right?
What’s really happening in this case, is that your smartphone is just a shape, an abstract shape thought by a company, and containing a complex interface that you concretely use from.

It is as simpler as that and no more complicated.
An object in the real life is only a concrete use of an abstracted shape implementing a specific interface.

The same equals for objected oriented code, then, CABIN stands for Concrete ABstract of INterfaces.

Edouard Kombo // @edouardkombo. Never stop learning.

Cabin principle or how to define an object oriented code