Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to store simple 3rd party class in Symfony2?

Tags:

php

symfony

I'm new to Symfony2 and I have faced some simple problem but I'm not sure how to manage with it. I need to use one simple 3rd party class and I'm not sure where and how to store it in project structure. Should I store is a Service in my Bundle or maybe I should store it in vendors directory? And if I'll store it in vendors isn't it a bad practice to store there the libs that isn't Symfony supported vendors?

like image 737
Alex Berdnikov Avatar asked Dec 21 '12 16:12

Alex Berdnikov


3 Answers

Usually you include those in your project with Composer. I suggest you to take a look at packagist to look if there is a Composer package for your class, otherwise you can't require it with composer.

Composer puts your classes in the vendor directory, you should put all 'vendors' (3th party libraries) there. Take a look on where to put them in that directory, so that the Composer autoloader can autoload it.

After that, it is recommend to create a bundle for that specific class. It is a best practise to create a service there. For instance, if your class is Foo you create a Acme\FooBundle which loads the Foo service:

// src/Acme/FooBundle/DependencyInjection/AcmeFooExtension.php
<?php

namespace Acme\FooBundle\DependencyInjection;

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

class AcmeFooExtension extends Extension
{
    /**
     * this method loads the Service Container services.
     */
    public function load(array $configs, ContainerBuilder $container)
    {
         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));

         // load the src/Acme/FooBundle/Resources/config/services.xml file
         $loader->load('services.xml');
    }
<!-- src/Acme/FooBundle/Resources/config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <!-- Loads the \Foo class as a acme_foo.foo service -->
        <service id="acme_foo.foo"
            class="\Foo"
        ></service>
    </services>

</container>
like image 74
Wouter J Avatar answered Nov 05 '22 01:11

Wouter J


Symfony itself stores the 3rd Party libraries at the vendors folder. Is a good practice to put your 3rd party class there too

If you don't know how to do it, probably this question will help.

like image 1
Renan Ivo Avatar answered Nov 05 '22 03:11

Renan Ivo


I belive that using service container will be a good practice. Anyway, service container is made up for storing third party depindencies and saving loose coupling.

Look the docs, there is written how and why service container should be used.

Good luck.

like image 1
Alexey Sidash Avatar answered Nov 05 '22 01:11

Alexey Sidash