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

Protected vs private in service provider #303

Open
verot opened this issue Nov 19, 2015 · 4 comments
Open

Protected vs private in service provider #303

verot opened this issue Nov 19, 2015 · 4 comments

Comments

@verot
Copy link

verot commented Nov 19, 2015

Would it be possible to set all the methods from BootstrapperL5ServiceProvider to be protected instead of private?

If so, we can then extend the service provider with something like this:

<?php namespace App\Services\Html;

class FormServiceProvider extends \Bootstrapper\BootstrapperL5ServiceProvider {

    /**
     * Register the form builder instance.
     *
     * @return void
     */
    protected function registerFormBuilder()
    {
        $this->app->bindShared(
            'illuminate::html',
            function ($app) {
                return new \Illuminate\Html\HtmlBuilder($app->make('url'));
            }
        );
        $this->app->bindShared(
            'bootstrapper::form',
            function ($app) {
                $form = new FormBuilder(
                    $app->make('illuminate::html'),
                    $app->make('url'),
                    $app['session.store']->getToken()
                );

                return $form->setSessionStore($app['session.store']);
            },
            true
        );
    }
}

alongside this:

<?php namespace App\Services\Html;

class FormBuilder extends \Bootstrapper\Form {

    /**
     * Create a submit button element.
     */
    public function submit($value = null, $options = [])
    {
        // ...
        return parent::submit($value, $options);
    }
}
@PatrickRose
Copy link
Collaborator

I'm kind of against this - I feel you should just override the binding or add a new binding for your version.

Willing to be convinced though

@verot
Copy link
Author

verot commented Nov 26, 2015

Well, the above code is the only way I managed to be able to extend both FormBuilder and HtmlBuider at the same time. But I may very well have done it the wrong way, and am perfectly willing to be convinced if there is another way.

@PatrickRose
Copy link
Collaborator

You could inside your own service provider bind your version of the Form with the binding of appname::form - or run a service provider after the Bootstrapping one to rebind bootstrapper::form to use your version.

In my mind though, bindings aren't extendable which is why they're private instead of public. It doesn't make sense that bootstrapper::form returns a non-bootstrapper class.

I'm currently looking at V6 and how we can support this sort of thing though - I'll keep it in mind

@verot
Copy link
Author

verot commented Dec 1, 2015

You could inside your own service provider [...] run a service provider after the Bootstrapping one to rebind bootstrapper::form to use your version.

Yes, I have done this, much simpler indeed. Just need to make sure that my service provider runs after the Boostrapper one.

In app.php

Bootstrapper\BootstrapperL5ServiceProvider::class,
App\Providers\AppServiceProvider::class,

In app/Providers/AppServiceProvider.php

    /**
     * Register the HTML builder instance.
     *
     * @return void
     */
    protected function registerHtmlBuilder()
    {
        $this->app->bindShared('html', function ($app) {
            return new \App\Services\HtmlBuilder($app['url']);
        });
    }

    /**
     * Register the form builder instance.
     *
     * @return void
     */
    protected function registerFormBuilder()
    {
        $this->app->bindShared('illuminate::html',function ($app) {
                return new \Illuminate\Html\HtmlBuilder($app->make('url'));
            }
        );
        $this->app->bindShared('bootstrapper::form',function ($app) {
                $form = new \App\Services\FormBuilder(
                    $app->make('illuminate::html'),
                    $app->make('url'),
                    $app['session.store']->getToken()
                );
                return $form->setSessionStore($app['session.store']);
            },
            true
        );
    }

In app/Services/HtmlBuilder.php

class HtmlBuilder extends \Collective\Html\HtmlBuilder { ... }

In app/Services/FormBuilder.php

class FormBuilder extends \Bootstrapper\Form { ... }

Thank you!

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

2 participants