就像提示一样,您不应该在代码中硬编码依赖项。"ConfigInterface"
您可以创建一个来获取所需的公共依赖项,而不是使用外观。然后创建一个"LaravelConfig class"
(或 Laravel4Config.php)并实现这些方法。
对于快速修复答案,“捕获底层外观实例”:
namespace Face\SocialHandlers;
//use Illuminate\Support\Facades\App;
//use Illuminate\Support\Facades\Config;
class FacebookHandler implements SocialHandlerInterface {
protected $config;
protected $app;
public function __construct()
{
$this->config = \Illuminate\Support\Facades\Config::getFacadeRoot();
$this->app = \Illuminate\Support\Facades\App::getFacadeRoot();
}
public function registrar($perfil) {
$this->config->get('facebook');
}
}
对于真正的答案,可能很乏味,但从长远来看是好的,而不是使用外观使用界面。
interface SocialConfigInterface
{
public function getConfigurationByKey($key)
}
然后
class Laravel4Config implements SocialConfigInterface
{
protected $config;
public function __construct()
{
$this->config = \Illuminate\Support\Facades\Config::getFacadeRoot(); //<-- hard coded, but as expected since it's a class to be used with Laravel 4
}
public function getConfigurationByKey($key)
{
return $this->config->get($key);
}
}
和你的代码
namespace Face\SocialHandlers;
//use Illuminate\Support\Facades\App;
//use Illuminate\Support\Facades\Config;
class FacebookHandler implements SocialHandlerInterface {
protected $config;
public function __construct(SocialConfigInterface $config)
{
$this->config = $config;
}
public function registrar($perfil) {
$this->config->get('facebook');
}
}
这样,如果您想在框架之间进行更改,您只需要创建一个SocialConfigInterface
实现,或者想象 Laravel 5 不会使用 Facades 的场景,您希望您的代码独立于“外部更改”,这是控制 IoC 的反转