1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Stream\Wrapper;
38
39use Hoa\Consistency;
40
41/**
42 * Class \Hoa\Stream\Wrapper.
43 *
44 * Manipulate wrappers.
45 *
46 * @copyright  Copyright © 2007-2017 Hoa community
47 * @license    New BSD License
48 */
49class Wrapper
50{
51    /**
52     * Register a wrapper.
53     *
54     * @param   string  $protocol     The wrapper name to be registered.
55     * @param   string  $className    Class name which implements the protocol.
56     * @param   int     $flags        Should be set to `STREAM_IS_URL` if
57     *                                `$protocol` is a URL protocol. Default is 0,
58     *                                local stream.
59     * @return  bool
60     * @throws  \Hoa\Stream\Wrapper\Exception
61     */
62    public static function register($protocol, $className, $flags = 0)
63    {
64        if (true === self::isRegistered($protocol)) {
65            throw new Exception(
66                'The protocol %s is already registered.',
67                0,
68                $protocol
69            );
70        }
71
72        if (false === class_exists($className)) {
73            throw new Exception(
74                'Cannot use the %s class for the implementation of ' .
75                'the %s protocol because it is not found.',
76                1,
77                [$className, $protocol]
78            );
79        }
80
81        return stream_wrapper_register($protocol, $className, $flags);
82    }
83
84    /**
85     * Unregister a wrapper.
86     *
87     * @param   string  $protocol    The wrapper name to be unregistered.
88     * @return  bool
89     */
90    public static function unregister($protocol)
91    {
92        // Silent errors if `$protocol` does not exist. This function already
93        // returns `false` in this case, which is the strict expected
94        // behaviour.
95        return @stream_wrapper_unregister($protocol);
96    }
97
98    /**
99     * Restore a previously unregistered build-in wrapper.
100     *
101     * @param   string  $protocol    The wrapper name to be restored.
102     * @return  bool
103     */
104    public static function restore($protocol)
105    {
106        // Silent errors if `$protocol` does not exist. This function already
107        // returns `false` in this case, which is the strict expected
108        // behaviour.
109        return @stream_wrapper_restore($protocol);
110    }
111
112    /**
113     * Check if a protocol is registered or not.
114     *
115     * @param   string  $protocol    Protocol name.
116     * @return  bool
117     */
118    public static function isRegistered($protocol)
119    {
120        return in_array($protocol, self::getRegistered());
121    }
122
123    /**
124     * Get all registered wrapper.
125     *
126     * @return  array
127     */
128    public static function getRegistered()
129    {
130        return stream_get_wrappers();
131    }
132}
133
134/**
135 * Flex entity.
136 */
137Consistency::flexEntity('Hoa\Stream\Wrapper\Wrapper');
138