88use Illuminate \Support \Arr ;
99use Illuminate \Support \Str ;
1010use ReflectionFunction ;
11+ use ReflectionMethod ;
1112
1213class Helper
1314{
@@ -53,6 +54,35 @@ protected static function isItemOrderInvalid($item, $array)
5354 return $ item ['order ' ] === false || $ item ['order ' ] >= count ($ array );
5455 }
5556
57+ /**
58+ * Gets the parameter of a callable thing (from is_callable) and returns it's arguments using reflection
59+ *
60+ * @param callable $callable
61+ * @return \ReflectionParameter[]
62+ * @throws \ReflectionException
63+ * @throws \InvalidArgumentException
64+ */
65+ private static function reflectCallableParameters ($ callable )
66+ {
67+ /*
68+ loosely after https://github.com/technically-php/callable-reflection/blob/main/src/CallableReflection.php#L72-L86.
69+ Licence is compatible, both project use MIT
70+ */
71+ if ($ callable instanceof Closure) {
72+ $ reflection = new ReflectionFunction ($ callable );
73+ } else if (is_string ($ callable ) && function_exists ($ callable )) {
74+ $ reflection = new ReflectionFunction ($ callable );
75+ } else if (is_string ($ callable ) && str_contains ($ callable , ':: ' )) {
76+ $ reflection = new ReflectionMethod ($ callable );
77+ } else if (is_object ($ callable ) && method_exists ($ callable , '__invoke ' )) {
78+ $ reflection = new ReflectionMethod ($ callable , '__invoke ' );
79+ } else {
80+ throw new \InvalidArgumentException ("argument is not callable or the code is wrong " );
81+ }
82+
83+ return $ reflection ->getParameters ();
84+ }
85+
5686 /**
5787 * Determines if content is callable or blade string, processes and returns.
5888 *
@@ -69,9 +99,8 @@ public static function compileContent($content, array $data, array|object $param
6999 return static ::compileBlade ($ content , static ::getMixedValue ($ data , $ param ));
70100 }
71101
72- if ($ content instanceof Closure) {
73- $ reflection = new ReflectionFunction ($ content );
74- $ arguments = $ reflection ->getParameters ();
102+ if (is_callable ($ content )) {
103+ $ arguments = self ::reflectCallableParameters ($ content );
75104
76105 if (count ($ arguments ) > 0 ) {
77106 return app ()->call ($ content , [$ arguments [0 ]->name => $ param ]);
0 commit comments