PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.17.0
GiveWP – Donation Plugin and Fundraising Platform v4.17.0
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
give / src / Framework / Support / Facades / Facade.php

Facade.php in GiveWP – Donation Plugin and Fundraising Platform 4.17.0, at src/Framework/Support/Facades/Facade.php

55 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Framework\Support\Facades;
4
5 /**
6 * Class Facade
7 *
8 * This class provides a way of taking a normal instance class and creating a static facade out of it. It does it in
9 * such a way, though, that the facade is still mockable. It does this by instantiating the decorated class through the
10 * Give Service Container (SC). So by injecting a mock singleton of the decorated class in the SC, it can be mocked.
11 *
12 * To use this, simply make a new facade class which extends this once, then implement the getFacadeClass and return the
13 * class to be decorated, for example: return MyClass::class;
14 *
15 * To help the IDE, take the methods from the decorated class and add them your class docblock. So if Repository had an
16 * insert method, you would add "@method static Model insert()" to the top.
17 *
18 * @since 2.12.0
19 */
20 abstract class Facade
21 {
22 /**
23 * Static helper for calling the facade methods
24 *
25 * @since 2.12.0
26 *
27 * @param string $name
28 * @param array $arguments
29 *
30 * @return mixed
31 */
32 public static function __callStatic($name, $arguments)
33 {
34 // Make sure the static class is a singleton and get instance
35 give()->singletonIf(static::class);
36 $staticInstance = give(static::class);
37
38 // Make sure the accessed class is a singleton and get instance
39 $accessorClass = $staticInstance->getFacadeAccessor();
40 give()->singletonIf($accessorClass);
41 $accessorInstance = give($accessorClass);
42
43 return $accessorInstance->$name(...$arguments);
44 }
45
46 /**
47 * Retrieves the fully qualified class name or alias for the class being decorated
48 *
49 * @since 2.12.0
50 *
51 * @return string
52 */
53 abstract protected function getFacadeAccessor();
54 }
55