A
A
Alexander Evgenievich2015-02-26 16:19:28
symfony
Alexander Evgenievich, 2015-02-26 16:19:28

How to get classpath from namespace in Symfony2?

Actually, the essence of the question is in the title. What service can do this action?
-----
wrote a class in the tests, which builds a schema in the database for me and loads fixtures, after clearing the database. Fixtures are located in different bundles. So that I don’t write the path from each bundle with my hands, I want to pull the path from the Namespace, since the doctrine needs the full path to the folder or class to load fixtures. I need to go to class.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Evgenievich, 2015-02-27
@banderos120

Here is the fixture loading class. Maybe someone will come in handy.

<?php


namespace Retouch\AppBundle\Tests;


use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\DependencyInjection\Container;

/**
 * Class FixturesLoader
 */
class FixturesLoader
{
    /**
     * @var Application
     */
    protected $console;

    /**
     * @var Container
     */
    protected $container;

    /**
     * @param Container $container
     */
    public function __construct(Container $container)
    {
        $this->container = $container;
        $this->console = new Application($this->container->get('kernel'));
        $this->console->setAutoExit(false);
    }

    /**
     * @param array $fixtures
     */
    public function loadFixture(array $fixtures = array())
    {
        $this->runConsole('doctrine:schema:drop --force');
        $this->runConsole('doctrine:schema:create');

        $loader = new ContainerAwareLoader($this->container);

        foreach($fixtures as $fixture){
            if($fixture instanceof FixtureInterface){
                $loader->addFixture($fixture);
            }
        }

        $fixtures = $loader->getFixtures();

        if(!$fixtures){
            return;
        }

        $doctrine = $this->container->get('doctrine');
        $em = $doctrine->getManager();

        $purger = new ORMPurger($em);

        $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE);

        $executor = new ORMExecutor($em, $purger);
        $executor->execute($fixtures, false);

    }

    /**
     * @param $command
     * @return int
     * @throws \Exception
     */
    protected function runConsole($command)
    {
        $command .= ' --env=test --quiet';

        $input = new StringInput($command);

        return $this->console->run($input);
    }
}

A
Alex, 2015-02-26
@shoomyst

I still didn’t understand anything, but apparently I need to look towards kernel->getBundles() + bundle->getPath()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question