PHP code example of tobento / service-dir

1. Go to this page and download the library: Download tobento/service-dir library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tobento / service-dir example snippets


use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\Dir;

$dirs = new Dirs(
    new Dir(dir: 'home/private/views', name: 'views'),
    new Dir('home/private/config', 'config'),
);

$dirs->dir('home/private/cache', 'cache');

var_dump($dirs->get('views'));
// string(19) "home/private/views/"

use Tobento\Service\Dir\DirsInterface;
use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\Dir;

$dirs = new Dirs(
    new Dir(dir: 'home/private/views', name: 'views'),
    new Dir('home/private/config', 'config'),
);

var_dump($dirs instanceof DirsInterface);
// bool(true)

use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\Dir;
use Tobento\Service\Dir\DirInterface;

$dirs = new Dirs();

// dir: DirInterface
$dirs->add(dir: new Dir('home/private/config', 'config'));

$dirs->add(new Dir('home/private/config', 'config'))
     ->add(new Dir('home/private/views', 'views'));

use Tobento\Service\Dir\Dirs;

$dirs = new Dirs();

$dirs->dir(
    dir: 'home/private/config', // string
    name: 'config', // null|string
    group: 'front', // string
    priority: 10, // int
);

$dirs->dir('home/private/views', 'views')
     ->dir('home/private/cache', 'cache');

use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\DirInterface;

$dirs = new Dirs();

$dirs->dir(
    dir: 'home/private/views/front',
    group: 'frontend',
    priority: 10,
);

$dirs->dir(
    dir: 'home/private/views/backend',
    group: 'backend',
);

// filter by group:
$dirs = $dirs->filter(fn(DirInterface $dir): bool => $dir->group() === 'frontend');

use Tobento\Service\Dir\Dirs;

$dirs = new Dirs();

$dirs->dir(
    dir: 'home/private/views/front',
    group: 'frontend',
    priority: 10,
);

$dirs->dir(
    dir: 'home/private/views/backend',
    group: 'backend',
);

$dirs = $dirs->group('frontend');

use Tobento\Service\Dir\Dirs;

$dirs = new Dirs();

$dirs->dir(
    dir: 'home/private/views/front',
    group: 'frontend',
    priority: 10,
);

$dirs->dir(
    dir: 'home/private/views/backend',
    group: 'backend',
);

$dirs = $dirs->groups(['frontend', 'backend']);

use Tobento\Service\Dir\Dirs;

$dirs = new Dirs();

$dirs->dir('home/private/views', 'views');
$dirs->dir('home/private/config', 'config');
$dirs->dir('home/private/cache', 'cache');

$dirs = $dirs->only(['views', 'cache']);

use Tobento\Service\Dir\Dirs;

$dirs = new Dirs();

$dirs->dir('home/private/views', 'views');
$dirs->dir('home/private/config', 'config');
$dirs->dir('home/private/cache', 'cache');

$dirs = $dirs->except(['views', 'cache']);

use Tobento\Service\Dir\Dirs;

$dirs = new Dirs();

$dirs->dir(
    dir: 'home/private/views/front',
    priority: 10,
);

$dirs->dir(
    dir: 'home/private/views/theme/front',
    priority: 15,
);

// sort by priority:
$dirs = $dirs->sort();

use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\DirInterface;

$dirs = new Dirs();

$dirs->dir(
    dir: 'home/private/views/front',
    priority: 10,
);

$dirs->dir(
    dir: 'home/private/views/theme/front',
    priority: 15,
);

// sort by name:
$dirs = $dirs->sort(
    fn(DirInterface $a, DirInterface $b): int => $a->name() <=> $b->name()
);

use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\DirInterface;

$dirs = new Dirs();

$dirs->dir('home/private/views', 'views');
$dirs->dir('home/private/config', 'config');

foreach($dirs->all() as $dir)
{
    var_dump($dir instanceof DirInterface);
    // bool(true)
}

// or just:
foreach($dirs as $dir)
{
    var_dump($dir instanceof DirInterface);
    // bool(true)
}

use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\DirNotFoundException;

$dirs = new Dirs();

$dirs->dir('home/private/views', 'views');
$dirs->dir('home/private/config', 'config');

var_dump($dirs->get('config'));
// string(20) "home/private/config/"

// throws DirNotFoundException if dir is not found
$dirs->get('cache');

use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\DirInterface;
use Tobento\Service\Dir\DirNotFoundException;

$dirs = new Dirs();

$dirs->dir('home/private/views', 'views');
$dirs->dir('home/private/config', 'config');

var_dump($dirs->getDir('config') instanceof DirInterface);
// bool(true)

// throws DirNotFoundException if dir is not found
$dirs->getDir('cache');

use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\DirInterface;

$dirs = new Dirs();

$dirs->dir('home/private/views', 'views');
$dirs->dir('home/private/config', 'config');

$dirs = $dirs->all('config');
// array<string, DirInterface>

use Tobento\Service\Dir\Dirs;

$dirs = new Dirs();

$dirs->dir('home/private/config', 'config');

var_dump($dirs->has('config'));
// bool(true)

var_dump($dirs->has('view'));
// bool(false)

use Tobento\Service\Dir\Dir;
use Tobento\Service\Dir\DirInterface;

$dir = new Dir(
    dir: 'home/private/config', // string
    name: 'config', // null|string
    group: 'front', // string
    priority: 10, // int
);

var_dump($dir instanceof DirInterface);
// bool(true)

var_dump($dir->dir());
// string(20) "home/private/config/"

var_dump($dir->name());
// string(6) "config"

var_dump($dir->group());
// string(5) "front"

var_dump($dir->priority());
// int(10)