Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc to override app/Resources location #7228

Closed
theofidry opened this issue Dec 6, 2016 · 9 comments
Closed

Add doc to override app/Resources location #7228

theofidry opened this issue Dec 6, 2016 · 9 comments

Comments

@theofidry
Copy link
Contributor

For a project of mine, I moved app/Resources into resources to keep more in line with the company project structure. On the way I had a few quirks, namely with the routing.

When having the following:

# resources/config/routing.yml
app:
    resource: '../../src/Infrastructure/Action'
    type:     'annotation'

In a regular application, this relative path is being located from app/Resources so it should result in app/Resources/../../src/Infrastructure/Action. Whilst the path does make sense (src/Infrastructure/Action exists), app/Resources does not exist so resolving this path e.g. with realpath() will fail.

One could get around that by using the @AppBundle notation, but this notation is not usable with .. so if AppBundle is not place at a higher level than src/Infrastructure/Action, e.g. when you have src/Infrastructure/Bundle/AppBundle.php, this trick cannot work.

When you get at that point, you either take the path of the least resistance and just create a app/Resources/.gitkeep file and don't care or you are a stubborn sore loser and try to keep going.

So here's how I eventually got around that: First override the file_locator service:

# resources/services.yml
services:
    file_locator:
        class: Symfony\Component\HttpKernel\Config\FileLocator
        arguments:
            - '@kernel'
            - '%kernel.root_dir%/../resources'

I don't know exactly where in the application this service is really used, but I do know it's used very early in the bootstrapping process so this in any case is not enough. Not finding a better solution, I changed:

// app/AppKernel.php

class AppKernel
{
    //...

    /**
     * @inheritdoc
     */
    public function locateResource($name, $dir = null, $first = true)
    {
        if (__DIR__.'/Resources' === $dir) {
            $dir = realpath(__DIR__.'/../resources');
        }
        
        return parent::locateResource($name, $dir, $first);
    }
}

I'm not sure is there something more elegant that could be done neither if this should be documented. If it should let me know where I'll be happy to do a PR about it, if you feel it's not worth it just close the issue.

@stof
Copy link
Member

stof commented Dec 6, 2016

Imported resources should be relative to the resource where the import is done. So this looks weird to me.

@theofidry
Copy link
Contributor Author

@stof I personally changed it slightly to not have relative path as I was afraid people would get confused by it:

# resources/config/routing.yml
app:
    resource: '@Resources/../src/Infrastructure/Action'
    type:     'annotation'
// app/AppKernel.php

class AppKernel
{
    //...

    /**
     * @inheritdoc
     */
    public function locateResource($name, $dir = null, $first = true)
    {
        if (preg_match('/^@Resources/', $name)) {
            $name = __DIR__.'/../resources/';
        }

        if (__DIR__.'/Resources' === $dir) {
            $dir = realpath(__DIR__.'/../resources');
        }
        
        return parent::locateResource($name, $dir, $first);
    }
}

@javiereguiluz
Copy link
Member

I'd add this to this article: "How to override anything in Symfony" We have some articles about overriding things in bundles ... but I'm not sure if we have that other article (which could explain how to override the location of the Doctrine entities, the location of cache/ and logs/, etc.)

@theofidry
Copy link
Contributor Author

@javiereguiluz
Copy link
Member

Let's ask @wouterj, @xabbuh and @weaverryan if they think it's a good idea to create a "How to override everything in Symfony" article to explain all of that in a single article. Thanks!

@fabpot
Copy link
Member

fabpot commented Jan 10, 2017

I think it's not needed anymore with #21231

@theofidry
Copy link
Contributor Author

theofidry commented Jan 11, 2017

@javiereguiluz should I close the issue as symfony/symfony#21231 fixes it or should be keep it for a "Override everything" page for which a few things are missing?

@javiereguiluz
Copy link
Member

@theofidry I agree that we should close this because of symfony/symfony#21231. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants