Blame view

vendor/wyrihaximus/twig-view/src/Lib/RelativeScanner.php 1.75 KB
c4650843   Etienne Pallier   Ajout du dossier ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php

namespace WyriHaximus\TwigView\Lib;

use Cake\Core\App;

/**
 * Class RelativeScanner
 * @package WyriHaximus\TwigView\Lib
 */
class RelativeScanner
{
    /**
     * Return all sections (app & plugins) with an Template directory.
     *
     * @return array
     */
    public static function all()
    {
        return static::strip(Scanner::all());
    }

    /**
     * Return all templates for a given plugin.
     *
     * @param string $plugin The plugin to find all templates for.
     *
     * @return mixed
     */
    public static function plugin($plugin)
    {
        return static::strip([
            $plugin => Scanner::plugin($plugin),
        ])[$plugin];
    }

    /**
     * Strip the absolute path of template's paths for all given sections.
     *
     * @param string $sections Sections to iterate over.
     *
     * @return array
     */
    protected static function strip($sections)
    {
        foreach ($sections as $section => $paths) {
            $sections[$section] = static::stripAbsolutePath($paths, $section == 'APP' ? null : $section);
        }
        return $sections;
    }

    /**
     * Strip the absolute path of template's paths.
     *
     * @param array       $paths  Paths to strip.
     * @param string|null $plugin Hold plugin name or null for App.
     *
     * @return array
     */
    protected static function stripAbsolutePath(array $paths, $plugin = null)
    {
        foreach (App::path('Template', $plugin) as $templatesPath) {
            array_walk($paths, function (&$path) use ($templatesPath) {
                if (substr($path, 0, strlen($templatesPath)) == $templatesPath) {
                    $path = substr($path, strlen($templatesPath));
                }
            });
        }

        return $paths;
    }
}