Skip to content

Best way to override ServerRunCommand and ServerStartCommand? #20429

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

Closed
pocky opened this issue Nov 6, 2016 · 4 comments
Closed

Best way to override ServerRunCommand and ServerStartCommand? #20429

pocky opened this issue Nov 6, 2016 · 4 comments

Comments

@pocky
Copy link

pocky commented Nov 6, 2016

Hello,

I am using a non-standard folder structure and I just discovered a "problem" with server:run and server:start (for me it's a DX) :

        $documentRoot = $input->getOption('docroot');

        if (null === $documentRoot) {
            $documentRoot = $this->getContainer()->getParameter('kernel.root_dir').'/../web';
        }

For my distribution, the $documentRoot should be : $this->getContainer()->getParameter('kernel.root_dir').'/../../web'

I don't want to tell to each dev wich using my distribution to add the --docroot option (or using an alias via composer scripts/make) so I'm looking to the best way to change this.

Solutions :

1- ServerRunCommand and ServerStartCommand should be used as services to allow overriding
2- Add a getDocumentRoot option in Kernel and use the parameter instead of relative path (simpler)
3- Create a symfony/server-bundle with all related server commands as dependency of framework-bundle

What's your opinion?

@chalasr
Copy link
Member

chalasr commented Nov 7, 2016

@pocky Hello,

If you don't want to pass this option each time, here is a quick way to do it:

<?php

namespace AppBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ServerRunCommand as BaseServerRunCommand;

class ServerRunCommand extends BaseServerRunCommand
{
    protected function configure()
    {
        parent::configure();

        $this->getDefinition()->getOption('docroot')->setDefault('your/custom/docroot');
    }
}

Like this, calling bin/console server:run use your custom docroot by default.

I may miss something but in my opinion having the docroot option should be enough for most use cases, I don't think it is worth making it configurable, AFAIK we never did such for any command in the past.

@pocky
Copy link
Author

pocky commented Nov 7, 2016

@chalasr Correct! ;)

But if I want to share my AppBundle with you (and your symfony-standard), this change will broke your server:run.

@chalasr
Copy link
Member

chalasr commented Nov 16, 2016

But if I want to share my AppBundle with you (and your symfony-standard), this change will broke your server:run.

Since the need is quite specific to your structure, you could register the command as a service and have the command in a place that cannot be published/reused (e.g. in app/Command if your structure allows it), outside of any bundle.

@chalasr
Copy link
Member

chalasr commented Jan 7, 2017

@pocky See #21190, it should help here

edit: In 3.3, you can e.g. decorate the service:

# app/config/services.yml
services:
    app.command.server_run:
        decorates: web_server.command.server_run
        arguments: ['%your_custom_docroot%', '%kernel.environment%']

fabpot added a commit that referenced this issue Jan 8, 2017
…ntainer (chalasr)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[WebServerBundle] Decouple server commands from the container

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20429
| License       | MIT
| Doc PR        | maybe

This removes the need for injecting the container in the new `server:*` commands, registering them as services when used in the framework and thus making them even more discoverable and extensible.
It would then be easy to reconsider extracting them in a `WebServer` component instead of having a bundle only. IMHO it would make sense to use these commands outside of the framework.

If the idea can be considered I'll add some tests at least ensuring that these commands are bootstrap-able. This must be done before that they are covered by the BC promise (3.3).

Commits
-------

2e63025 [WebServerBundle] Decouple server:* commands from the container
@fabpot fabpot closed this as completed Jan 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants