Skip to content

Instantly share code, notes, and snippets.

@unstoppablecarl
Last active January 27, 2017 17:38
Show Gist options
  • Select an option

  • Save unstoppablecarl/e00ca4ceb4a60e505839155fa021d607 to your computer and use it in GitHub Desktop.

Select an option

Save unstoppablecarl/e00ca4ceb4a60e505839155fa021d607 to your computer and use it in GitHub Desktop.

Revisions

  1. unstoppablecarl revised this gist Jan 27, 2017. 1 changed file with 11 additions and 9 deletions.
    20 changes: 11 additions & 9 deletions app.php
    Original file line number Diff line number Diff line change
    @@ -2,16 +2,16 @@

    // 5.3
    class Foo {
    protected $cache = true;
    protected $cache = false;
    protected $dep;

    public function __construct(Dep $dep, $cache = true) {
    public function __construct(Dep $dep, $cache = false) {
    $this->dep = $dep;
    $this->cache = $cache;
    }
    }

    app(Foo::class, ['cache' => false]);
    app(Foo::class, ['cache' => true]);


    // 5.4 solution 1
    @@ -24,20 +24,21 @@ public function __construct(Dep $dep, $cache = true) {


    // 5.4 solution 2
    class Bar {
    protected $cache = true;
    class Foo {
    protected $cache = false;
    protected $dep;

    public function __construct(Dep $dep, \Illuminate\Config\Repository $config) {
    $this->dep = $dep;
    $this->cache = $config->get('package.cache');
    }
    }
    app(Foo::class);


    // 5.4 solution 3
    class Cam {
    protected $cache = true;
    class Foo {
    protected $cache = false;
    protected $dep;

    public function __construct(Dep $dep) {
    @@ -52,8 +53,9 @@ public function cache($value = null) {
    }
    }

    $this->app->bind(Cam::class, function ($app) {
    $obj = app(Cam::class);

    $this->app->bind(Foo::class, function ($app) {
    $obj = app(Foo::class);
    $cache = config('package.cache');
    $obj->cache($cache);
    });
  2. unstoppablecarl revised this gist Jan 27, 2017. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions app.php
    Original file line number Diff line number Diff line change
    @@ -2,58 +2,58 @@

    // 5.3
    class Foo {
    protected $active = true;
    protected $cache = true;
    protected $dep;

    public function __construct(Dep $dep, $active = true) {
    public function __construct(Dep $dep, $cache = true) {
    $this->dep = $dep;
    $this->active = $active;
    $this->cache = $cache;
    }
    }

    app(Foo::class, ['active' => false]);
    app(Foo::class, ['cache' => false]);


    // 5.4 solution 1
    $this->app->bind(Foo::class, function ($app) {
    $depClass = app(Dep::class);
    $active = config('package.active');
    $cache = config('package.cache');

    return new Foo($depClass, $active);
    return new Foo($depClass, $cache);
    });


    // 5.4 solution 2
    class Bar {
    protected $active = true;
    protected $cache = true;
    protected $dep;

    public function __construct(Dep $dep, \Illuminate\Config\Repository $config) {
    $this->dep = $dep;
    $this->active = $config->get('package.active');
    $this->cache = $config->get('package.cache');
    }
    }


    // 5.4 solution 3
    class Cam {
    protected $active = true;
    protected $cache = true;
    protected $dep;

    public function __construct(Dep $dep) {
    $this->dep = $dep;
    }

    public function active($value = null) {
    public function cache($value = null) {
    if ($value !== null) {
    $this->active = $value;
    $this->cache = $value;
    }
    return $this->active;
    return $this->cache;
    }
    }

    $this->app->bind(Cam::class, function ($app) {
    $obj = app(Cam::class);
    $active = config('package.active');
    $obj->active($active);
    $cache = config('package.cache');
    $obj->cache($cache);
    });
  3. unstoppablecarl created this gist Jan 27, 2017.
    59 changes: 59 additions & 0 deletions app.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    <?php

    // 5.3
    class Foo {
    protected $active = true;
    protected $dep;

    public function __construct(Dep $dep, $active = true) {
    $this->dep = $dep;
    $this->active = $active;
    }
    }

    app(Foo::class, ['active' => false]);


    // 5.4 solution 1
    $this->app->bind(Foo::class, function ($app) {
    $depClass = app(Dep::class);
    $active = config('package.active');

    return new Foo($depClass, $active);
    });


    // 5.4 solution 2
    class Bar {
    protected $active = true;
    protected $dep;

    public function __construct(Dep $dep, \Illuminate\Config\Repository $config) {
    $this->dep = $dep;
    $this->active = $config->get('package.active');
    }
    }


    // 5.4 solution 3
    class Cam {
    protected $active = true;
    protected $dep;

    public function __construct(Dep $dep) {
    $this->dep = $dep;
    }

    public function active($value = null) {
    if ($value !== null) {
    $this->active = $value;
    }
    return $this->active;
    }
    }

    $this->app->bind(Cam::class, function ($app) {
    $obj = app(Cam::class);
    $active = config('package.active');
    $obj->active($active);
    });