1<?php
2/**
3 * This file is part of the static reflection component.
4 *
5 * PHP Version 5
6 *
7 * Copyright (c) 2009-2011, Manuel Pichler <mapi@pdepend.org>.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 *   * Redistributions of source code must retain the above copyright
15 *     notice, this list of conditions and the following disclaimer.
16 *
17 *   * Redistributions in binary form must reproduce the above copyright
18 *     notice, this list of conditions and the following disclaimer in
19 *     the documentation and/or other materials provided with the
20 *     distribution.
21 *
22 *   * Neither the name of Manuel Pichler nor the names of his
23 *     contributors may be used to endorse or promote products derived
24 *     from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
29 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
30 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 *
39 * @category  PHP
40 * @package   pdepend\reflection
41 * @author    Manuel Pichler <mapi@pdepend.org>
42 * @copyright 2009-2011 Manuel Pichler. All rights reserved.
43 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
44 * @version   SVN: $Id$
45 * @link      http://pdepend.org/
46 */
47
48namespace pdepend\reflection;
49
50/**
51 * Simple cache class that can be used to keep already found reflection class
52 * instances in memory for faster access.
53 *
54 * @category  PHP
55 * @package   pdepend\reflection
56 * @author    Manuel Pichler <mapi@pdepend.org>
57 * @copyright 2009-2011 Manuel Pichler. All rights reserved.
58 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
59 * @version   Release: @package_version@
60 * @link      http://pdepend.org/
61 */
62class ReflectionClassCache
63{
64    /**
65     * Cached reflection class instances.
66     *
67     * @var array(\ReflectionClass)
68     */
69    private $_classes = array();
70
71    /**
72     * This method checks if a class for the given <b>$className</b> already
73     * exists in the cache.
74     *
75     * @param string $className Name of the searched class.
76     *
77     * @return boolean
78     */
79    public function has( $className )
80    {
81        return isset( $this->_classes[$this->_normalizeClassName( $className )] );
82    }
83
84    /**
85     * This method will restore a previously created reflection class instance
86     * for the given <b>$className</b>.
87     *
88     * @param string $className Name of the searched class.
89     *
90     * @return \ReflectionClass
91     */
92    public function restore( $className )
93    {
94        if ( $this->has( $className ) )
95        {
96            return $this->_classes[$this->_normalizeClassName( $className )];
97        }
98        throw new \LogicException( 'Class ' . $className . ' does not exist' );
99    }
100
101    /**
102     * This method stores the given reflection class within the cache.
103     *
104     * @param \ReflectionClass $class The newly created reflection class.
105     *
106     * @return void
107     */
108    public function store( \ReflectionClass $class )
109    {
110        $this->_classes[$this->_normalizeClassName( $class->getName() )] = $class;
111    }
112
113    /**
114     * Normalizes a class or interface name.
115     *
116     * @param string $className A class or interface name.
117     *
118     * @return string
119     */
120    private function _normalizeClassName( $className )
121    {
122        return ltrim( strtolower( $className ), '\\' );
123    }
124}