Skip to content

Commit 3b98c1a

Browse files
committed
Protect the config array by allowing only valid configuration keys to be defined.
1 parent 473164f commit 3b98c1a

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

src/Annotations/Annotations.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@
2121
*/
2222

2323
namespace ElementaryFramework\Annotations;
24+
use ElementaryFramework\Annotations\Exceptions\AnnotationException;
2425

2526
/**
26-
* Thin, static class with shortcut methods for inspection of Annotations
27+
* Thin, static class with shortcut methods for inspection of annotations
2728
*
2829
* Using this static wrapper is optional - if your application uses a service container
2930
* or a dependency injection container, you most likely want to configure an instance
@@ -34,29 +35,53 @@ abstract class Annotations
3435
/**
3536
* @var array Configuration for any public property of AnnotationManager.
3637
*/
37-
public static $config;
38+
private static $_config;
3839

3940
/**
4041
* @var AnnotationManager Singleton AnnotationManager instance
4142
*/
42-
private static $manager;
43+
private static $_manager;
44+
45+
/**
46+
* Sets a config value.
47+
*
48+
* @param string $key The config key.
49+
* @param mixed $value The config value.
50+
*
51+
* @throws AnnotationException
52+
*/
53+
public static function setConfig(string $key, $value)
54+
{
55+
switch ($key) {
56+
case "autoload":
57+
case "cache":
58+
case "suffix":
59+
case "namespace":
60+
case "debug":
61+
self::$_config[$key] = $value;
62+
break;
63+
64+
default:
65+
throw new AnnotationException("Invalid config key.");
66+
}
67+
}
4368

4469
/**
4570
* @return AnnotationManager a singleton instance
4671
*/
4772
public static function getManager()
4873
{
49-
if (!isset(self::$manager)) {
50-
self::$manager = new AnnotationManager;
74+
if (!isset(self::$_manager)) {
75+
self::$_manager = new AnnotationManager;
5176
}
5277

53-
if (\is_array(self::$config)) {
54-
foreach (self::$config as $key => $value) {
55-
self::$manager->$key = $value;
78+
if (\is_array(self::$_config)) {
79+
foreach (self::$_config as $key => $value) {
80+
self::$_manager->$key = $value;
5681
}
5782
}
5883

59-
return self::$manager;
84+
return self::$_manager;
6085
}
6186

6287
/**
@@ -76,7 +101,7 @@ public static function getUsage(string $class): UsageAnnotation
76101
}
77102

78103
/**
79-
* Inspects class Annotations
104+
* Inspects class annotations
80105
*
81106
* @see AnnotationManager::getClassAnnotations()
82107
*
@@ -93,7 +118,7 @@ public static function ofClass($class, string $type = null): array
93118
}
94119

95120
/**
96-
* Inspects method Annotations
121+
* Inspects method annotations
97122
*
98123
* @see AnnotationManager::getMethodAnnotations()
99124
*

src/Annotations/UsageAnnotation.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,32 @@
2323
namespace ElementaryFramework\Annotations;
2424

2525
/**
26-
* This Annotation is mandatory, and must be applied to all Annotations.
26+
* This Annotation is mandatory, and must be applied to all annotations.
2727
*/
2828
class UsageAnnotation extends Annotation
2929
{
3030
/**
31-
* @var boolean Set this to TRUE for Annotations that may be applied to classes.
31+
* @var boolean Set this to TRUE for annotations that may be applied to classes.
3232
*/
3333
public $class = false;
3434

3535
/**
36-
* @var boolean Set this to TRUE for Annotations that may be applied to properties.
36+
* @var boolean Set this to TRUE for annotations that may be applied to properties.
3737
*/
3838
public $property = false;
3939

4040
/**
41-
* @var boolean Set this to TRUE for Annotations that may be applied to methods.
41+
* @var boolean Set this to TRUE for annotations that may be applied to methods.
4242
*/
4343
public $method = false;
4444

4545
/**
46-
* @var boolean $multiple Set this to TRUE for Annotations that allow multiple instances on the same member.
46+
* @var boolean $multiple Set this to TRUE for annotations that allow multiple instances on the same member.
4747
*/
4848
public $multiple = false;
4949

5050
/**
51-
* @var boolean $inherited Set this to TRUE for Annotations that apply to members of child classes.
51+
* @var boolean $inherited Set this to TRUE for annotations that apply to members of child classes.
5252
*/
5353
public $inherited = false;
5454
}

0 commit comments

Comments
 (0)