Completed
Push — master ( 95d8d8...4ac61d )
by WEBEWEB
01:47
created

ObjectHelper::getDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Argument;
13
14
use Exception;
15
use ReflectionClass;
16
use ReflectionException;
17
use WBW\Library\Core\Exception\Argument\ObjectArgumentException;
18
use WBW\Library\Core\Exception\Reflection\ClassNotFoundException;
19
use WBW\Library\Core\Exception\Reflection\MethodNotFoundException;
20
use WBW\Library\Core\Exception\Reflection\SyntaxErrorException;
21
use WBW\Library\Core\IO\FileHelper;
22
23
/**
24
 * Object helper.
25
 *
26
 * @author webeweb <https://github.com/webeweb/>
27
 * @package WBW\Library\Core\Argument
28
 */
29
class ObjectHelper {
30
31
    /**
32
     * Get a class directory.
33
     *
34
     * @param mixed $object The class name or object.
35
     * @return string Returns the class directory.
36
     */
37
    public static function getDirectory($object) {
38
        $filename = (new ReflectionClass($object))->getFileName();
39
        return dirname($filename);
40
    }
41
42
    /**
43
     * Get the hooks.
44
     *
45
     * @param string $classpath The class path.
46
     * @param string $namespace The namespace.
47
     * @param string $classname The class name.
48
     * @param string $extends The class extend.
49
     * @param string $method The class method.
50
     * @return array Returns the hooks array.
51
     * @throws ClassNotFoundException Throws a hook class not found if the classname is not found.
52
     */
53
    public static function getHooks($classpath, $namespace, $classname = null, $extends = null, $method = null) {
54
55
        //
56
        $hooks = [];
57
58
        // Get the filenames.
59
        $filenames = FileHelper::getFileNames($classpath, ".php");
60
61
        // Handle each filenames.
62
        foreach ($filenames as $filename) {
63
64
            // Check the class name.
65
            if (null !== $classname && 0 === preg_match($classname, $filename)) {
66
                continue;
67
            }
68
69
            // Import the class.
70
            try {
71
                require_once $classpath . "/" . $filename;
72
            } catch (Exception $ex) {
73
                throw new SyntaxErrorException($classpath . "/" . $filename);
74
            }
75
76
            // Init. the complete class name.
77
            $completeClassname = $namespace . basename($filename, ".php");
78
79
            try {
80
                $rc = new ReflectionClass($completeClassname);
81
            } catch (ReflectionException $ex) {
82
                throw new ClassNotFoundException($completeClassname, $ex);
83
            }
84
85
            // Check the extends.
86
            if (false === (null === $extends || true === $rc->isSubclassOf($extends))) {
87
                continue;
88
            }
89
90
            // Initialize the hook.
91
            $hook = [];
92
93
            $hook["classpath"] = $classpath;
94
            $hook["namespace"] = $namespace;
95
            $hook["filename"]  = $filename;
96
            $hook["class"]     = $rc;
97
            $hook["method"]    = null;
98
99
            $hooks[] = $hook;
100
101
            // Check the method.
102
            if (null === $method) {
103
                continue;
104
            }
105
106
            try {
107
                $rm = $rc->getMethod($method);
108
109
                $hooks[count($hooks) - 1]["method"] = $rm;
110
            } catch (ReflectionException $ex) {
111
                throw new MethodNotFoundException($method, $ex);
112
            }
113
        }
114
115
        // Returns the hooks.
116
        return $hooks;
117
    }
118
119
    /**
120
     * Get the class name.
121
     *
122
     * @param mixed $object The class name or object.
123
     * @return string Returns the class name.
124
     * @throws ReflectionException Throws a Reflection exception if an error occurs.
125
     */
126
    public static function getName($object) {
127
        return (new ReflectionClass($object))->getName();
0 ignored issues
show
Bug introduced by
Consider using (new \ReflectionClass($object))->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
128
    }
129
130
    /**
131
     * Get the short class name.
132
     *
133
     * @param mixed $object The class name or object.
134
     * @return string Returns the short class name.
135
     * @throws ReflectionException Throws a Reflection exception if an error occurs.
136
     */
137
    public static function getShortName($object) {
138
        return (new ReflectionClass($object))->getShortName();
139
    }
140
141
    /**
142
     * Determines if a value is an object.
143
     *
144
     * @param mixed $value The value.
145
     * @throws ObjectArgumentException Throws an Object argument exception if the value is not of expected type.
146
     */
147
    public static function isObject($value) {
148
        if (false === is_object($value)) {
149
            throw new ObjectArgumentException($value);
150
        }
151
    }
152
153
}
154