summaryrefslogtreecommitdiff
path: root/tests/UnitTests/ResourceTests/Custom/Ambiguous/PHPunitplugins/resource.ambiguous.php
blob: 85ffadd5cbb2478b8cde3ffd92e41ab69707f168 (plain)
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
<?php

use Smarty\Exception;
use Smarty\Resource\FilePlugin;
use Smarty\Smarty;
use Smarty\Template;
use Smarty\Template\Source;

/**
 * Ambiguous Filename Custom Resource Example
 *

 * @author  Rodney Rehm
 */
class Smarty_Resource_AmbiguousPlugin extends FilePlugin
{
    protected $directory;
    protected $segment;

    public function __construct($directory)
    {
        $this->directory = rtrim($directory ?? '', "/\\") . DIRECTORY_SEPARATOR;
        //        parent::__construct();
    }

    public function setSegment($segment)
    {
        $this->segment = $segment;
    }

    /**
     * populate Source Object with meta data from Resource
     *
     * @param Source   $source    source object
     * @param Template $_template template object
     */
    public function populate(Source $source, Template $_template = null)
    {
        $segment = '';
        if ($this->segment) {
            $segment = rtrim($this->segment, "/\\") . DIRECTORY_SEPARATOR;
        }

        $source->uid = sha1($segment . '#' . $source->getResourceName());
        if ($_template->getSmarty()->getCompileCheck() && !isset($source->timestamp)) {
            $source->timestamp = @filemtime($this->directory . $segment . $source->name);
            $source->exists = !!$source->timestamp;
        }
    }

	public function getContent(Source $source) {

		$segment = '';
		if ($this->segment) {
			$segment = rtrim($this->segment, "/\\") . DIRECTORY_SEPARATOR;
		}

		if ($source->exists) {
			return file_get_contents($this->directory . $segment . $source->name);
		}
		throw new Exception(
			'Unable to read ' . ($source->isConfig ? 'config' : 'template') .
			" {$source->type} '{$source->name}'"
		);
	}
}