-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiko.php
More file actions
143 lines (122 loc) · 4.27 KB
/
Piko.php
File metadata and controls
143 lines (122 loc) · 4.27 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* This file is part of Piko - Web micro framework
*
* @copyright 2019-2024 Sylvain PHILIP
* @license LGPL-3.0; see LICENSE.txt
* @link https://github.com/piko-framework/core
*/
declare(strict_types=1);
/**
* Piko is the helper class for the Piko framework.
*
* @author Sylvain PHILIP <contact@sphilip.com>
*/
class Piko
{
/**
* The aliases container.
*
* @var string[]
*/
protected static $aliases = [];
/**
* Registers a path alias.
*
* A path alias is a short name representing a long path (a file path, a URL, etc.)
*
* @param string $alias The alias name (e.g. "@web"). It must start with a '@' character.
* @param string $path the path corresponding to the alias.
* @return void
* @throws InvalidArgumentException if $path is an invalid alias.
* @see Piko::getAlias()
*/
public static function setAlias(string $alias, string $path): void
{
if ($alias[0] != '@') {
throw new InvalidArgumentException('Alias must start with the @ character');
}
static::$aliases[$alias] = $path;
}
/**
* Translates a path alias into an actual path.
*
* @param string $alias The alias to be translated.
* @return string|bool The path corresponding to the alias. False if the alias is not registered.
*/
public static function getAlias(string $alias)
{
if ($alias[0] != '@') {
return $alias;
}
$pos = strpos($alias, '/');
$root = $pos === false ? $alias : substr($alias, 0, $pos);
if (isset(static::$aliases[$root])) {
return $pos === false ? static::$aliases[$root] : static::$aliases[$root] . substr($alias, $pos);
}
return false;
}
/**
* Generic factory method.
*
* @param class-string|array<string, mixed> $type The object type.
* If it is a string, it should be the fully qualified name of the class.
* If an array given, it must contain the key 'class' with the value corresponding
* to the fully qualified name of the class. It should also contain the key 'construct' to give an array of
* constuctor arguments
* @param array<string, mixed> $properties A key-value paired array corresponding to the object public properties.
* @return object
*/
public static function createObject($type, array $properties = []): object
{
if (is_array($type)) {
$properties = $type;
if (!isset($properties['class'])) {
throw new InvalidArgumentException('Missing "class" key in the class definition array.');
}
$type = $properties['class'];
unset($properties['class']);
}
if (!is_string($type)) {
throw new InvalidArgumentException('Type must be string.');
} elseif (!class_exists($type)) {
throw new InvalidArgumentException(sprintf('Class %s not found', $type));
}
$reflection = new ReflectionClass($type);
if (isset($properties['construct'])) {
$constructArgs = $properties['construct'];
unset($properties['construct']);
}
$object = isset($constructArgs) && is_array($constructArgs) ?
$reflection->newInstanceArgs($constructArgs) : $reflection->newInstance();
if (count($properties)) {
static::configureObject($object, $properties);
}
return $object;
}
/**
* Configure public properties of an object.
*
* @param object $object The object instance.
* @param array<string, mixed> $data A key-value array corresponding to the public properties of an object.
* @return void
*/
public static function configureObject(object $object, array $data = []): void
{
$reflection = new ReflectionClass($object);
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property) {
/* @var $property \ReflectionProperty */
if (isset($data[$property->name])) {
$property->setValue($object, $data[$property->name]);
}
}
}
/**
* Reset aliases
*/
public static function reset(): void
{
static::$aliases = [];
}
}