2016-08-26 2 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, btw 공급자 배열에 공급자를 추가했습니다.

도움을 주시면 감사하겠습니다. 레지스터() 함수에 AppServiceProvider

:

답변

0

나는 당신이 당신의 클래스와 인터페이스를 추가한다고 생각합니다.

예 :

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

여기 HelperService 클래스 이며 HelperServiceImpl 내가 그런 짓을하는 인터페이스

+0

입니다. 나는이 문제를 해결했다고 생각한다. WorkDay.php에서 생성자를 public function __construct (\ App \ Library \ Holiday \ HolidayInterface $ holiday, $ params = array())로 변경했습니다. 그것을 계속 지켜 볼 것입니다. –