herrera-io/file-locator

此包已被放弃且不再维护。没有推荐替代包。

一个简单的文件定位库。

1.0.0 2013-01-16 19:23 UTC

This package is not auto-updated.

Last update: 2021-12-07 01:34:11 UTC


README

Build Status

一个简单的文件定位库。

摘要

为了向更大的项目提供文件定位能力,创建了 FileLocator 库。它可以用于模板引擎、配置文件加载器等。

安装

将其添加到您的Composer依赖列表中

$ composer require herrera-io/file-locator=1.*

使用

该库包含 FileSystemLocator,可用于搜索一个或多个目录路径。可以将单个目录路径或路径数组传递给构造函数。

<?php

use Herrera\FileLocator\Locator\FileSystemLocator;

$locator = new FileSystemLocator('/path/to/dir');
$locator = new FileSystemLocator(array(
    '/path/to/dir1',
    '/path/to/dir2',
    '/path/to/dir3' // etc
));

$file = $locator->locate('file.ini'); // return the first "file.ini" found
$files = $locator->locate('file.ini', false); // find all named "file.ini"

您还可以通过实现接口 LocatorInterface 来创建自己的自定义文件定位器。库中捆绑的所有定位器类也都实现了此接口,允许直接替换。

<?php

use Herrera\FileLocator\Locator\LocatorInterface;

class MyLocator implements LocatorInterface
{
    public function locate($file, $first = true)
    {
        // do something to locate $file

        // if $first is true, return the first matching result
            // if nothing is found, return null

        // if $first is false, generate a list of found $file(s) and return it
            // if nothing is found, return an empty array
    }
}

您可以通过使用 Collection 类来组合一个或多个文件定位器类。这可以用于链式连接一个或多个文件定位器,并且可以用作实际的文件定位器。

<?php

use Herrera\FileLocator\Collection;
use Herrera\FileLocator\Locator\FileSystemLocator;
use My\Library\CustomLocator;

$locators = new Collection();
$locators->add(new FileSystemLocator('/path/to/dir'));
$locators->add(new CustomLocator());

$file = $locator->locate('file.ini'); // return the first "file.ini" found
$files = $locator->locate('file.ini', false); // find all named "file.ini"