0

From a Symfony 4 project, Is there a properly/symfony way for get the root path directory from a Fixture class ?

The final goal :

I use fixtures class for load some "default" datas in my database when I deploy my website. (May be its not a good practice)

Among theses defaults datas, I need to store the root path directory of the project because I need this information from custom utils and services class in the project.

I know there are multiple ways for that. Currently, I hard coded the absolute path ( <= best ugliest way ever ;) )

Will B.
  • 17,883
  • 4
  • 67
  • 69
spacecodeur
  • 2,206
  • 7
  • 35
  • 71
  • Typically your fixtures would reside in a single "/tests" directory in your project root directory, allowing you to use `__DIR__`. However most fixtures are expected to be isolated from your application configuration. Please update your question to show what you have tried so far and we can help you with any issues you are encountering. If you need some direction, I suggest looking at the Symfony tests, which use a [`KernelMock`](https://github.com/symfony/symfony/blob/v4.1.7/src/Symfony/Bundle/FrameworkBundle/Tests/ClientTest.php) to determine how they are using it. – Will B. Nov 05 '18 at 14:24
  • thanks for your comment, and done ;) – spacecodeur Nov 05 '18 at 14:57
  • 1
    I see, there was some confusion of a fixture, which are typically used as isolation tests. You could just use your `AppKernel` in your data fixture, for the desired environment. e.g. `$kernel = new AppKernel('dev');` Then retrieve the root dir by using `$kernel->getRootDir();` or `$kernel->getProjectDir();` depending on the context, since they use `__DIR__` by default. – Will B. Nov 05 '18 at 15:06
  • 1
    I think he should not instance another kernel !!! Instead he should get the existant instance `this->get('kernel')->getProjectDir()` – famas23 Nov 06 '18 at 04:45
  • 1
    The fyrye's solution works but I agree with AhmedEBENHASSINE . However, the context of "this" is not good from a Service Class, so when I use "$this->get('kernel')->getProjectDir()" , I get the php error "Attempted to call an undefined method named "get". Instead (fyrye solution) I inject the "KernelInterface" service in the constructor of my custom service and use a "$kernel->getProjectDir()". I'm not sure if its the better solution. – spacecodeur Nov 06 '18 at 07:57

1 Answers1

5

Symfonfy 4.1.7

Service.yml

services:
  _defaults:
    bind:
      $webDirectory:  '%kernel.project_dir%/public_html/'

Fixtures File

use Symfony\Component\HttpFoundation\File\UploadedFile;
// --
private $webDirectory, $imageLocation;
public function __construct($webDirectory)
{
    $this->webDirectory = $webDirectory;
    $this->imageLocation = $webDirectory . 'img/config/';
}
public function load(ObjectManager $manager){
    for ($i = 0; $i < 20; $i++) {
        copy($this->imageLocation . 'blog_default_50x50.jpg', $this->imageLocation . 'blog_default_50x50-copy.jpg');
        $file = new UploadedFile($this->imageLocation . 'blog_default_50x50-copy.jpg', 'Image1', null, null, null, true);
        ---
        $blogPost->setFile($file);
        $manager->persist($blogPost);
    }
    $manager->flush();
}

UploadedFile() info from https://stackoverflow.com/a/18883334/624533 (Thankyou!).

Hope this helps someone.

Sudhakar Krishnan
  • 732
  • 1
  • 8
  • 27