2016-08-26 40 views
1

我試圖實現一個自定義類(關於英格蘭/威爾士的商業銀行假期),它實現了一個接口。Laravel 5 - 無法綁定接口

我的文件夾結構是:

App\Library\WorkDay.php 
App\Library\Holiday\HolidayInterface.php 
App\Library\Holiday\HolidayEnglandWales.php 
App\Providers\HolidayInterfaceProvider.php 
Tests\WorkDayTest.php 

HolidayInterface:

<?php 

namespace App\Library\Holiday; 

/** 
* Holiday Interface 
* 
* @author andy.roberts 
*/ 

interface HolidayInterface { 

/** 
* Retrieve a list of holidays for supplied year 
* 
* @param int $year Holiday year 
* @param int $subsitute Subsitute holiday on weekend 
*/ 
public function getHoliday($year); 

/** 
* Add a single holiday event 
* 
* @param string $name 
* @param int $timestamp 
*/ 
public function addHoliday($name, $timestamp); 

/** 
* Remove single holiday event 
* 
* @param int $timestamp 
*/ 
public function removeHoliday($timestamp); 
} 

HolidayEnglandWales:

<?php 

namespace App\Library\Holiday; 

use App\Library\Holiday\HolidayInterface; 


/** 
* Public and bank holidays in England and Wales 
* 
* @author andy.roberts 
*/ 

class HolidayEnglandWales implements HolidayInterface { 
    .... 
} 

HolidayInterfaceProvider:

<?php 

namespace App\Providers; 

use Illuminate\Support\ServiceProvider; 

use App\Library\Holiday\HolidayEnglandWales; 

class HolidayInterfaceProvider extends ServiceProvider 
{ 

    /** 
    * Bootstrap the application services. 
    * 
    * @return void 
    */ 
    public function boot() 
    { 
     // 
    } 

    /** 
    * Register the application services. 
    * 
    * @return void 
    */ 
    public function register() 
    { 
     $this->app->bind('App\Library\Holiday\HolidayInterface', function(){ 

      return new HolidayEnglandWales(); 

     }); 
    } 


} 

工作日:

<?php 


namespace App\Library; 

/** 
* Calculate the number of working days between two dates 
* 
* This is achieved by a simple algorithm which calculates 
* the number of complete weeks and remaining days within 
* any given period. 
* 
* Each complete week is multiplied by the number of 
* working days, and the remaining days enumerated. 
* 
* Public holidays are included in the calculation. 
* 
* @author andy.roberts 
*/ 

class WorkDay { 

    const DAY = 86400; 
    const WEEK = 604800; 
    const MONDAY = 1; 
    const TUESDAY = 2; 
    const WEDNESDAY = 3; 
    const THURSDAY = 4; 
    const FRIDAY = 5; 
    const SATURDAY = 6; 
    const SUNDAY = 7; 

    ... 

    public function __construct(HolidayInterface $holiday, $params = array()) { 

     $this->_nonWorkingDay = array(self::SATURDAY, self::SUNDAY); 

     if(isset($params['includeEndDay'])) { 
      $this->_includeEndDay = ($params['includeEndDay'] == true) ? true : false; 
     } 

     $this->_holiday = $holiday; 
    } 

} 

WorkDayTest:

<?php 

use Illuminate\Foundation\Testing\WithoutMiddleware; 
use Illuminate\Foundation\Testing\DatabaseMigrations; 
use Illuminate\Foundation\Testing\DatabaseTransactions; 
use App\Models\User; 
use Carbon\Carbon; 
use Symfony\Component\Console\Output\ConsoleOutput; 

use App\Library\WorkDay; 
use App\Library\Holiday\HolidayEnglandWales; 


/** 
* Working Days Test Case 
*/ 
class WorkDayTest extends TestCase 
{ 

    /** 
    * Prepares the environment before running a test. 
    */ 
    /* 
    protected function setUp() { 
     parent::setUp(); 
     date_default_timezone_set('Europe/London'); 
    } 
    */ 


    /** 
    * Working days in a single month 
    * 
    * January 2010 
    * 
    * 31 days 
    * 21 working days 
    * 1 holiday (New Years Day) 
    */ 
    public function testWorkingDayInSingleMonth() { 
     $workDay = new WorkDay(new HolidayEnglandWales()); 
     $this->assertEquals($workDay->count('2010-01-01', '2010-01-31'), 20); 
    } 
} 

在WorkDayTest當這條線運行:

$workDay = new WorkDay(new HolidayEnglandWales()); 

此錯誤產生:

enter image description here

該類似乎沒有被綁定到接口,我不確定問題是什麼。我試過作曲家更新無濟於事。我已經將提供程序添加到App \ Config中的providers數組中,順便說一句。

任何幫助,將不勝感激。

回答

0

我想你應該你的類和接口的加入:在寄存器()函數AppServiceProvider

實施例:

$this->app->bind(
    \Rar\Services\HelperService::class, 
    \Rar\Services\Impl\HelperServiceImpl::class 
); 

這裏HelperService是一類 和HelperServiceImpl是一個接口

+0

我已經做到了。我想我已經解決了這個問題。在WorkDay.php中,我將構造函數更改爲公共函數__construct(\ App \ Library \ Holiday \ HolidayInterface $ holiday,$ params = array()),這似乎奏效了。將密切關注它。 –