Skip to content

Conversation

@RobertBoes
Copy link

@RobertBoes RobertBoes commented Apr 12, 2018

Also changed the TestApplicationWrapper to use the Illuminate Container when the abstract class is a subclass of AbstractWidget

This makes it possible to have a widget class like this:

<?php

namespace App\Widgets;

use App\Contracts\UserRepository;
use Arrilot\Widgets\AbstractWidget;

class RecentlyCreatedUsers extends AbstractWidget
{
    /**
     * The configuration array.
     *
     * @var array
     */
    protected $config = [];

    private $users;

    public function __construct(array $config = [], UserRepository $userRepository)
    {
        parent::__construct($config);

        $this->users = $userRepository;
    }

    /**
     * Treat this method as a controller action.
     * Return view() or other content to display.
     */
    public function run()
    {
        $users = $this->users->recentlyCreated();

        return view('widgets.recently_created_users', [
            'config' => $this->config,
        ])->withUsers($users);
    }
}

Also changed the TestApplicationWrapper to use the Illuminate Container when the abstract class is a subclass of AbstractWidget
@RobertBoes
Copy link
Author

RobertBoes commented Apr 12, 2018

I don't exactly know why, but I can't make it work on PHP 5.5. Container::getInstance() returns null, but for the rest of the versions it does work.. 😕

The tests for 5.5 and 5.6 were failing, because I used app->make() instead of app->makeWith(), older versions of Illuminate only supported passing parameters with the function makeWith, I saw you're using make everywhere, but this isn't tested due to the test wrapper. Haven't tested it, but maybe the package overall does not work with PHP 5.5/5.6?

@arrilot
Copy link
Owner

arrilot commented Apr 15, 2018

Well, method injection is much more appropriate in this example

<?php

namespace App\Widgets;

use App\Contracts\UserRepository;
use Arrilot\Widgets\AbstractWidget;

class RecentlyCreatedUsers extends AbstractWidget
{
    /**
     * The configuration array.
     *
     * @var array
     */
    protected $config = [];

    /**
     * Treat this method as a controller action.
     * Return view() or other content to display.
     */
    public function run(UserRepository $userRepository)
    {
        $users = $userRepository->recentlyCreated();

        return view('widgets.recently_created_users', [
            'config' => $this->config,
        ])->withUsers($users);
    }
}

Widget class is like a controller with a single action, but I believe constructor injection can be still usefull. Thanks for contribution

@arrilot arrilot closed this Apr 15, 2018
@arrilot arrilot reopened this Apr 15, 2018
@arrilot arrilot merged commit ea247d4 into arrilot:master Apr 15, 2018
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

Successfully merging this pull request may close these issues.

2 participants