1<?php
2/*
3 * This file is part of php-token-stream.
4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11/**
12 * A caching factory for token stream objects.
13 */
14class PHP_Token_Stream_CachingFactory
15{
16    /**
17     * @var array
18     */
19    protected static $cache = [];
20
21    /**
22     * @param string $filename
23     *
24     * @return PHP_Token_Stream
25     */
26    public static function get($filename)
27    {
28        if (!isset(self::$cache[$filename])) {
29            self::$cache[$filename] = new PHP_Token_Stream($filename);
30        }
31
32        return self::$cache[$filename];
33    }
34
35    /**
36     * @param string $filename
37     */
38    public static function clear($filename = null)
39    {
40        if (is_string($filename)) {
41            unset(self::$cache[$filename]);
42        } else {
43            self::$cache = [];
44        }
45    }
46}
47