Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.

Commit 5196ebe

Browse files
authored
Merge pull request #14 from modulusphp/feature/extendable-feature
Feature/extendable feature
2 parents 1ae249e + 2ad15cf commit 5196ebe

File tree

6 files changed

+166
-1
lines changed

6 files changed

+166
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Modulus\Support\Exceptions;
4+
5+
use Exception;
6+
7+
class CannotAddMethodException extends Exception
8+
{
9+
/**
10+
* $message
11+
*
12+
* @var string
13+
*/
14+
protected $message = 'Function has already been added';
15+
16+
/**
17+
* __construct
18+
*
19+
* @return void
20+
*/
21+
public function __construct()
22+
{
23+
$args = debug_backtrace()[2];
24+
25+
foreach ($args as $key => $value) {
26+
$this->{$key} = $value;
27+
}
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Modulus\Support\Exceptions;
4+
5+
use Exception;
6+
7+
class CannotCallMethodException extends Exception
8+
{
9+
/**
10+
* __construct
11+
*
12+
* @param string $message
13+
* @return void
14+
*/
15+
public function __construct(string $message)
16+
{
17+
$args = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 3)[1];
18+
19+
foreach ($args as $key => $value) {
20+
$this->{$key} = $value;
21+
}
22+
23+
$this->message = $message;
24+
}
25+
}

Extendable.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace Modulus\Support;
4+
5+
use Closure;
6+
use Modulus\Support\Exceptions\CannotAddMethodException;
7+
use Modulus\Support\Exceptions\CannotCallMethodException;
8+
9+
trait Extendable
10+
{
11+
/**
12+
* A list of custom functions
13+
*
14+
* @var array $functions
15+
*/
16+
public static $functions = [];
17+
18+
/**
19+
* A list of custom static functions
20+
*
21+
* @var array $staticFunctions
22+
*/
23+
public static $staticFunctions = [];
24+
25+
/**
26+
* Add custom function
27+
*
28+
* @param string $method
29+
* @param Closure $closure
30+
* @return mixed
31+
*/
32+
public static function bind(string $method, Closure $closure)
33+
{
34+
$trace = debug_backtrace()[1];
35+
36+
if (
37+
$trace['class'] == 'Modulus\Framework\Upstart\Prototype' &&
38+
$trace['function'] == 'bind' &&
39+
count($trace['args']) == 3
40+
) {
41+
if (!array_key_exists($method, Self::$functions)) {
42+
return Self::$functions[$method] = [$method => $closure];
43+
}
44+
45+
throw new CannotAddMethodException;
46+
}
47+
48+
throw new CannotCallMethodException('Call to ' . self::class . '::bind() is not allowed');
49+
}
50+
51+
/**
52+
* Add custom static function
53+
*
54+
* @param string $method
55+
* @param Closure $closure
56+
* @return mixed
57+
*/
58+
public static function static(string $method, Closure $closure)
59+
{
60+
$trace = debug_backtrace()[1];
61+
62+
if (
63+
$trace['class'] == 'Modulus\Framework\Upstart\Prototype' &&
64+
$trace['function'] == 'static' &&
65+
count($trace['args']) == 3
66+
) {
67+
if (!array_key_exists($method, Self::$staticFunctions)) {
68+
return Self::$staticFunctions[$method] = [$method => $closure];
69+
}
70+
71+
throw new CannotAddMethodException;
72+
}
73+
74+
throw new CannotCallMethodException('Call to ' . self::class . '::static() is not allowed');
75+
}
76+
77+
/**
78+
* Call custom function
79+
*
80+
* @param string $method
81+
* @param array $args
82+
* @return mixed
83+
*/
84+
public function __call(string $method, array $args)
85+
{
86+
if (array_key_exists($method, Self::$functions)) {
87+
return call_user_func_array(Self::$functions[$method][$method], array_merge([$this], $args));
88+
}
89+
}
90+
91+
/**
92+
* Call custom static function
93+
*
94+
* @param string $method
95+
* @param array $args
96+
* @return mixed
97+
*/
98+
public static function __callStatic(string $method, array $args)
99+
{
100+
if (array_key_exists($method, Self::$staticFunctions)) {
101+
return call_user_func_array(Self::$staticFunctions[$method][$method], $args);
102+
}
103+
}
104+
}

File.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
use Exception;
66
use Modulus\Support\Config;
7+
use Modulus\Support\Extendable;
78
use Modulus\Support\Exceptions\UnknownStorageException;
89
use Modulus\Support\Exceptions\StorageUnusableException;
910

1011
class File
1112
{
13+
use Extendable;
14+
1215
/**
1316
* $info
1417
*

Filesystem.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Modulus\Support;
44

5+
use Modulus\Support\Extendable;
6+
57
class Filesystem
68
{
9+
use Extendable;
10+
711
/**
812
* Filesystem::PUBLIC
913
*/

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "modulusphp/support",
33
"description": "Support component for Modulus",
4-
"version": "1.9.2.4",
4+
"version": "1.9.3.0",
55
"license": "MIT",
66
"type": "package",
77
"authors": [{

0 commit comments

Comments
 (0)