Php Laravel依赖项注入绑定

Php Laravel依赖项注入绑定,php,laravel,dependency-injection,inversion-of-control,Php,Laravel,Dependency Injection,Inversion Of Control,今天早上我刚刚更新了我的项目,这反过来又更新了我的Laravel项目中的一些依赖项。自从这次更新以来,我注意到有一件事已经停止工作了 在我的一个命令处理程序中,我注入了一个文件系统依赖项,并以以下方式使用它: use Illuminate\Contracts\Filesystem\Filesystem; // NOTE: This is an interface ... class MyCommandHandler { protected $filesystem; public

今天早上我刚刚更新了我的项目,这反过来又更新了我的Laravel项目中的一些依赖项。自从这次更新以来,我注意到有一件事已经停止工作了

在我的一个命令处理程序中,我注入了一个
文件系统
依赖项,并以以下方式使用它:

use Illuminate\Contracts\Filesystem\Filesystem; // NOTE: This is an interface
...
class MyCommandHandler {
    protected $filesystem;

    public function __construct(Filesystem $filesystem)
    {
        $this->filesystem = $filesystem;
    }

    public function handle()
    {
        if ($this->filesystem->exists('/directory/that/does/not/exist'))
        {
            // Do something
        }
    }
}
我注意到,在使用不存在的目录对注入的
$filesystem
对象调用
exists
方法时,条件会像目录确实存在一样传递,我最终会在代码中遇到问题

我使用了
get_class()
来输出
$filesystem
对象的类,结果它变成了
illumb\filesystem\FilesystemAdapter
。进一步调查后,我发现我的文件系统操作最终由
League\Flysystem
包处理,而以前它们是由
illighted\filesystem\filesystem
处理的

我不完全清楚为什么对Flysystem的更改破坏了我的代码,但我有几个关于Laravel中依赖注入的问题

  • 键入提示一个接口并让Laravel从IoC容器中解析它更好吗(就像我在这里做的)还是键入提示一个具体的类更好(在我的例子中是inject
    illighte\Filesystem\Filesystem

  • 接口->实现的绑定在哪里定义?此外,可以在不更改核心代码的情况下修改它们吗

  • 干杯

    编辑

    控制器中的一些测试代码。输出
    是|否

    use Illuminate\Contracts\Filesystem\Filesystem;
    ...
    
    public function index(Filesystem $fs)
    {
        $testPath = '/path/that/does/not/exist';
        echo $fs->exists($testPath) ? 'yes' : 'no';
        echo '|' . (file_exists($testPath) ? 'yes' : 'no');
    
    编辑2-部分答案

    我已经做了更多的挖掘工作,并找到了我在
    Flysystem
    包实现方面遇到问题的原因:

    Flysystem包使用磁盘(在config/filesystems.php中定义)工作,默认为本地磁盘。事实证明,这个包使用这里为每个磁盘定义的配置,在我的例子中是本地磁盘。默认情况下,本地磁盘以应用程序的存储路径为根,然后将所有路径视为相对于该路径

    因此,当我执行
    $fs->exists('/does/not/exist')
    时,路径实际上被视为
    /var/www/myproject/storage/app/does/not/exist

    可以定义任意数量的磁盘,并使用
    illumb\Contracts\Filesystem\Factory
    包执行与该路径相关的操作,如下所示:

    config/filesystems.php

    'disks' => [
    
        'local' => [
            'driver' => 'local',
            'root'   => storage_path().'/app',
        ],
    
        'public' => [
            'driver' => 'local',
            'root'   => public_path()
        ],
    
    任何控制器/命令/任何其他内容

    use Illuminate\Contracts\Filesystem\Factory as Filesystem;
    ...
    class ClassName {
        protected $filesystem;
    
        public function __construct(Filesystem $fs)
        {
            $this->filesystem = $fs->disk('public'); // Refers to public disk in config/filesystems.php
        }
    
        public function method()
        {
            $this->filesystem->exists('path'); // /var/www/myproject/public/path
        }
    }
    

    非常感谢。我不知道如何查看
    存储/App
    -我所有的文件都在那里!您是否考虑过将您的编辑更改为下面的答案?