@@ -11,132 +11,132 @@ |
||
11 | 11 | * @package Intraxia\Jaxion |
12 | 12 | */ |
13 | 13 | class Application extends Container implements ApplicationContract { |
14 | - /** |
|
15 | - * Singleton instance of the Application object |
|
16 | - * |
|
17 | - * @var Application |
|
18 | - */ |
|
19 | - protected static $instance = null; |
|
20 | - |
|
21 | - /** |
|
22 | - * {@inheritdoc} |
|
23 | - * |
|
24 | - * @param string $file |
|
25 | - * @param array $providers |
|
26 | - * |
|
27 | - * @throws ApplicationAlreadyBootedException |
|
28 | - */ |
|
29 | - public function __construct( $file, array $providers = array() ) { |
|
30 | - if ( static::$instance !== null ) { |
|
31 | - throw new ApplicationAlreadyBootedException; |
|
32 | - } |
|
33 | - |
|
34 | - static::$instance = $this; |
|
35 | - |
|
36 | - parent::__construct( $providers ); |
|
37 | - $this->register_constants( $file ); |
|
38 | - $this->register_core_services(); |
|
39 | - $this->load_i18n(); |
|
40 | - |
|
41 | - register_activation_hook( $file, array( $this, 'activate' ) ); |
|
42 | - register_deactivation_hook( $file, array( $this, 'deactivate' ) ); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * {@inheritDoc} |
|
47 | - */ |
|
48 | - public function boot() { |
|
49 | - $loader = $this->fetch( 'loader' ); |
|
50 | - |
|
51 | - if ( ! $loader instanceof LoaderContract ) { |
|
52 | - throw new \UnexpectedValueException; |
|
53 | - } |
|
54 | - |
|
55 | - foreach ( $this as $alias => $value ) { |
|
56 | - if ( $value instanceof HasActions ) { |
|
57 | - $loader->register_actions( $value ); |
|
58 | - } |
|
59 | - |
|
60 | - if ( $value instanceof HasFilters ) { |
|
61 | - $loader->register_filters( $value ); |
|
62 | - } |
|
63 | - } |
|
64 | - |
|
65 | - add_action( 'plugins_loaded', array( $loader, 'run' ) ); |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * {@inheritdoc} |
|
70 | - * |
|
71 | - * @codeCoverageIgnore |
|
72 | - */ |
|
73 | - public function activate() { |
|
74 | - // no-op |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * {@inheritdoc} |
|
79 | - * |
|
80 | - * @codeCoverageIgnore |
|
81 | - */ |
|
82 | - public function deactivate() { |
|
83 | - // no-op |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * {@inheritDoc} |
|
88 | - * |
|
89 | - * @return Application |
|
90 | - * @throws ApplicationNotBootedException |
|
91 | - */ |
|
92 | - public static function instance() { |
|
93 | - if ( static::$instance === null ) { |
|
94 | - throw new ApplicationNotBootedException; |
|
95 | - } |
|
96 | - |
|
97 | - return static::$instance; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * {@inheritDoc} |
|
102 | - */ |
|
103 | - public static function shutdown() { |
|
104 | - if ( static::$instance !== null ) { |
|
105 | - static::$instance = null; |
|
106 | - } |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Sets the plugin's url, path, and basename. |
|
111 | - * |
|
112 | - * @param string $file |
|
113 | - */ |
|
114 | - private function register_constants( $file ) { |
|
115 | - $this->share( 'url', plugin_dir_url( $file ) ); |
|
116 | - $this->share( 'path', plugin_dir_path( $file ) ); |
|
117 | - $this->share( 'basename', plugin_basename( $file ) ); |
|
118 | - |
|
119 | - $plugin_data = get_plugin_data( $file, false, false ); |
|
120 | - $this->share( 'version', isset( $plugin_data['Version'] ) ? $plugin_data['Version'] : null ); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Registers the built-in services with the Application container. |
|
125 | - */ |
|
126 | - private function register_core_services() { |
|
127 | - $this->share( array( 'loader' => 'Intraxia\Jaxion\Contract\Core\Loader' ), function ( $app ) { |
|
128 | - return new Loader( $app ); |
|
129 | - } ); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Load's the plugin's translation files. |
|
134 | - */ |
|
135 | - private function load_i18n() { |
|
136 | - load_plugin_textdomain( |
|
137 | - $this->fetch( 'basename' ), |
|
138 | - false, |
|
139 | - basename( $this->fetch( 'path' ) ) . '/languages/' |
|
140 | - ); |
|
141 | - } |
|
14 | + /** |
|
15 | + * Singleton instance of the Application object |
|
16 | + * |
|
17 | + * @var Application |
|
18 | + */ |
|
19 | + protected static $instance = null; |
|
20 | + |
|
21 | + /** |
|
22 | + * {@inheritdoc} |
|
23 | + * |
|
24 | + * @param string $file |
|
25 | + * @param array $providers |
|
26 | + * |
|
27 | + * @throws ApplicationAlreadyBootedException |
|
28 | + */ |
|
29 | + public function __construct( $file, array $providers = array() ) { |
|
30 | + if ( static::$instance !== null ) { |
|
31 | + throw new ApplicationAlreadyBootedException; |
|
32 | + } |
|
33 | + |
|
34 | + static::$instance = $this; |
|
35 | + |
|
36 | + parent::__construct( $providers ); |
|
37 | + $this->register_constants( $file ); |
|
38 | + $this->register_core_services(); |
|
39 | + $this->load_i18n(); |
|
40 | + |
|
41 | + register_activation_hook( $file, array( $this, 'activate' ) ); |
|
42 | + register_deactivation_hook( $file, array( $this, 'deactivate' ) ); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * {@inheritDoc} |
|
47 | + */ |
|
48 | + public function boot() { |
|
49 | + $loader = $this->fetch( 'loader' ); |
|
50 | + |
|
51 | + if ( ! $loader instanceof LoaderContract ) { |
|
52 | + throw new \UnexpectedValueException; |
|
53 | + } |
|
54 | + |
|
55 | + foreach ( $this as $alias => $value ) { |
|
56 | + if ( $value instanceof HasActions ) { |
|
57 | + $loader->register_actions( $value ); |
|
58 | + } |
|
59 | + |
|
60 | + if ( $value instanceof HasFilters ) { |
|
61 | + $loader->register_filters( $value ); |
|
62 | + } |
|
63 | + } |
|
64 | + |
|
65 | + add_action( 'plugins_loaded', array( $loader, 'run' ) ); |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * {@inheritdoc} |
|
70 | + * |
|
71 | + * @codeCoverageIgnore |
|
72 | + */ |
|
73 | + public function activate() { |
|
74 | + // no-op |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * {@inheritdoc} |
|
79 | + * |
|
80 | + * @codeCoverageIgnore |
|
81 | + */ |
|
82 | + public function deactivate() { |
|
83 | + // no-op |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * {@inheritDoc} |
|
88 | + * |
|
89 | + * @return Application |
|
90 | + * @throws ApplicationNotBootedException |
|
91 | + */ |
|
92 | + public static function instance() { |
|
93 | + if ( static::$instance === null ) { |
|
94 | + throw new ApplicationNotBootedException; |
|
95 | + } |
|
96 | + |
|
97 | + return static::$instance; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * {@inheritDoc} |
|
102 | + */ |
|
103 | + public static function shutdown() { |
|
104 | + if ( static::$instance !== null ) { |
|
105 | + static::$instance = null; |
|
106 | + } |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Sets the plugin's url, path, and basename. |
|
111 | + * |
|
112 | + * @param string $file |
|
113 | + */ |
|
114 | + private function register_constants( $file ) { |
|
115 | + $this->share( 'url', plugin_dir_url( $file ) ); |
|
116 | + $this->share( 'path', plugin_dir_path( $file ) ); |
|
117 | + $this->share( 'basename', plugin_basename( $file ) ); |
|
118 | + |
|
119 | + $plugin_data = get_plugin_data( $file, false, false ); |
|
120 | + $this->share( 'version', isset( $plugin_data['Version'] ) ? $plugin_data['Version'] : null ); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Registers the built-in services with the Application container. |
|
125 | + */ |
|
126 | + private function register_core_services() { |
|
127 | + $this->share( array( 'loader' => 'Intraxia\Jaxion\Contract\Core\Loader' ), function ( $app ) { |
|
128 | + return new Loader( $app ); |
|
129 | + } ); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Load's the plugin's translation files. |
|
134 | + */ |
|
135 | + private function load_i18n() { |
|
136 | + load_plugin_textdomain( |
|
137 | + $this->fetch( 'basename' ), |
|
138 | + false, |
|
139 | + basename( $this->fetch( 'path' ) ) . '/languages/' |
|
140 | + ); |
|
141 | + } |
|
142 | 142 | } |
@@ -14,100 +14,100 @@ |
||
14 | 14 | * @subpackage Core |
15 | 15 | */ |
16 | 16 | class Loader implements LoaderContract { |
17 | - /** |
|
18 | - * Array of action hooks to attach. |
|
19 | - * |
|
20 | - * @var array[] |
|
21 | - */ |
|
22 | - protected $actions = array(); |
|
17 | + /** |
|
18 | + * Array of action hooks to attach. |
|
19 | + * |
|
20 | + * @var array[] |
|
21 | + */ |
|
22 | + protected $actions = array(); |
|
23 | 23 | |
24 | - /** |
|
25 | - * Array of filter hooks to attach. |
|
26 | - * |
|
27 | - * @var array[] |
|
28 | - */ |
|
29 | - protected $filters = array(); |
|
24 | + /** |
|
25 | + * Array of filter hooks to attach. |
|
26 | + * |
|
27 | + * @var array[] |
|
28 | + */ |
|
29 | + protected $filters = array(); |
|
30 | 30 | |
31 | - /** |
|
32 | - * {@inheritDoc} |
|
33 | - */ |
|
34 | - public function run() { |
|
35 | - foreach ( $this->actions as $action ) { |
|
36 | - add_action( |
|
37 | - $action['hook'], |
|
38 | - array( $action['service'], $action['method'] ), |
|
39 | - $action['priority'], |
|
40 | - $action['args'] |
|
41 | - ); |
|
42 | - } |
|
31 | + /** |
|
32 | + * {@inheritDoc} |
|
33 | + */ |
|
34 | + public function run() { |
|
35 | + foreach ( $this->actions as $action ) { |
|
36 | + add_action( |
|
37 | + $action['hook'], |
|
38 | + array( $action['service'], $action['method'] ), |
|
39 | + $action['priority'], |
|
40 | + $action['args'] |
|
41 | + ); |
|
42 | + } |
|
43 | 43 | |
44 | - foreach ( $this->filters as $filter ) { |
|
45 | - add_filter( |
|
46 | - $filter['hook'], |
|
47 | - array( $filter['service'], $filter['method'] ), |
|
48 | - $filter['priority'], |
|
49 | - $filter['args'] |
|
50 | - ); |
|
51 | - } |
|
52 | - } |
|
44 | + foreach ( $this->filters as $filter ) { |
|
45 | + add_filter( |
|
46 | + $filter['hook'], |
|
47 | + array( $filter['service'], $filter['method'] ), |
|
48 | + $filter['priority'], |
|
49 | + $filter['args'] |
|
50 | + ); |
|
51 | + } |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Registers a single Service's actions with WordPress. |
|
56 | - * |
|
57 | - * @param HasActions $service |
|
58 | - */ |
|
59 | - public function register_actions( HasActions $service ) { |
|
60 | - foreach ( $service->action_hooks() as $action ) { |
|
61 | - $this->actions = $this->add( |
|
62 | - $this->actions, |
|
63 | - $action['hook'], |
|
64 | - $service, |
|
65 | - $action['method'], |
|
66 | - isset( $action['priority'] ) ? $action['priority'] : 10, |
|
67 | - isset( $action['args'] ) ? $action['args'] : 1 |
|
68 | - ); |
|
69 | - } |
|
70 | - } |
|
54 | + /** |
|
55 | + * Registers a single Service's actions with WordPress. |
|
56 | + * |
|
57 | + * @param HasActions $service |
|
58 | + */ |
|
59 | + public function register_actions( HasActions $service ) { |
|
60 | + foreach ( $service->action_hooks() as $action ) { |
|
61 | + $this->actions = $this->add( |
|
62 | + $this->actions, |
|
63 | + $action['hook'], |
|
64 | + $service, |
|
65 | + $action['method'], |
|
66 | + isset( $action['priority'] ) ? $action['priority'] : 10, |
|
67 | + isset( $action['args'] ) ? $action['args'] : 1 |
|
68 | + ); |
|
69 | + } |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Registers a single Service's filters with WordPress. |
|
74 | - * |
|
75 | - * @param HasFilters $service |
|
76 | - */ |
|
77 | - public function register_filters( HasFilters $service ) { |
|
78 | - foreach ( $service->filter_hooks() as $filter ) { |
|
79 | - $this->filters = $this->add( |
|
80 | - $this->filters, |
|
81 | - $filter['hook'], |
|
82 | - $service, |
|
83 | - $filter['method'], |
|
84 | - isset( $filter['priority'] ) ? $filter['priority'] : 10, |
|
85 | - isset( $filter['args'] ) ? $filter['args'] : 1 |
|
86 | - ); |
|
87 | - } |
|
88 | - } |
|
72 | + /** |
|
73 | + * Registers a single Service's filters with WordPress. |
|
74 | + * |
|
75 | + * @param HasFilters $service |
|
76 | + */ |
|
77 | + public function register_filters( HasFilters $service ) { |
|
78 | + foreach ( $service->filter_hooks() as $filter ) { |
|
79 | + $this->filters = $this->add( |
|
80 | + $this->filters, |
|
81 | + $filter['hook'], |
|
82 | + $service, |
|
83 | + $filter['method'], |
|
84 | + isset( $filter['priority'] ) ? $filter['priority'] : 10, |
|
85 | + isset( $filter['args'] ) ? $filter['args'] : 1 |
|
86 | + ); |
|
87 | + } |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Utility to register the actions and hooks into a single collection. |
|
92 | - * |
|
93 | - * @param array $hooks |
|
94 | - * @param string $hook |
|
95 | - * @param object $service |
|
96 | - * @param string $method |
|
97 | - * @param int $priority |
|
98 | - * @param int $accepted_args |
|
99 | - * |
|
100 | - * @return array |
|
101 | - */ |
|
102 | - protected function add( $hooks, $hook, $service, $method, $priority, $accepted_args ) { |
|
103 | - $hooks[] = array( |
|
104 | - 'hook' => $hook, |
|
105 | - 'service' => $service, |
|
106 | - 'method' => $method, |
|
107 | - 'priority' => $priority, |
|
108 | - 'args' => $accepted_args, |
|
109 | - ); |
|
90 | + /** |
|
91 | + * Utility to register the actions and hooks into a single collection. |
|
92 | + * |
|
93 | + * @param array $hooks |
|
94 | + * @param string $hook |
|
95 | + * @param object $service |
|
96 | + * @param string $method |
|
97 | + * @param int $priority |
|
98 | + * @param int $accepted_args |
|
99 | + * |
|
100 | + * @return array |
|
101 | + */ |
|
102 | + protected function add( $hooks, $hook, $service, $method, $priority, $accepted_args ) { |
|
103 | + $hooks[] = array( |
|
104 | + 'hook' => $hook, |
|
105 | + 'service' => $service, |
|
106 | + 'method' => $method, |
|
107 | + 'priority' => $priority, |
|
108 | + 'args' => $accepted_args, |
|
109 | + ); |
|
110 | 110 | |
111 | - return $hooks; |
|
112 | - } |
|
111 | + return $hooks; |
|
112 | + } |
|
113 | 113 | } |
@@ -13,309 +13,309 @@ |
||
13 | 13 | * @subpackage Core |
14 | 14 | */ |
15 | 15 | class Container implements ContainerContract { |
16 | - /** |
|
17 | - * Registered definitions. |
|
18 | - * |
|
19 | - * @var mixed[] |
|
20 | - */ |
|
21 | - private $definitions = array(); |
|
22 | - |
|
23 | - /** |
|
24 | - * Aliases to share between fetches. |
|
25 | - * |
|
26 | - * @var <string, true>[] |
|
27 | - */ |
|
28 | - private $shared = array(); |
|
29 | - |
|
30 | - /** |
|
31 | - * Aliases of all the registered services. |
|
32 | - * |
|
33 | - * @var <string, true>[] |
|
34 | - */ |
|
35 | - private $aliases = array(); |
|
36 | - |
|
37 | - /** |
|
38 | - * Array of classes registered on the container. |
|
39 | - * |
|
40 | - * @var <string, true>[] |
|
41 | - */ |
|
42 | - private $classes = array(); |
|
43 | - |
|
44 | - /** |
|
45 | - * Current position in the loop. |
|
46 | - * |
|
47 | - * @var int |
|
48 | - */ |
|
49 | - private $position; |
|
50 | - |
|
51 | - /** |
|
52 | - * 0-indexed array of aliases for looping. |
|
53 | - * |
|
54 | - * @var string[] |
|
55 | - */ |
|
56 | - private $keys = array(); |
|
57 | - |
|
58 | - /** |
|
59 | - * Create a new container with the given providers. |
|
60 | - * |
|
61 | - * Providers can be instances or the class of the provider as a string. |
|
62 | - * |
|
63 | - * @param ServiceProvider[]|string[] $providers |
|
64 | - */ |
|
65 | - public function __construct( array $providers = array() ) { |
|
66 | - foreach ( $providers as $provider ) { |
|
67 | - if ( is_string( $provider ) && class_exists( $provider ) ) { |
|
68 | - $provider = new $provider; |
|
69 | - } |
|
70 | - |
|
71 | - if ( $provider instanceof ServiceProvider ) { |
|
72 | - $this->register( $provider ); |
|
73 | - } |
|
74 | - } |
|
75 | - } |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * {@inheritdoc} |
|
80 | - * |
|
81 | - * @param string|array $alias |
|
82 | - * @param mixed $definition |
|
83 | - * |
|
84 | - * @throws DefinedAliasException |
|
85 | - * |
|
86 | - * @return $this |
|
87 | - */ |
|
88 | - public function define( $alias, $definition ) { |
|
89 | - if ( is_array( $alias ) ) { |
|
90 | - $class = current( $alias ); |
|
91 | - $alias = key( $alias ); |
|
92 | - } |
|
93 | - |
|
94 | - if ( isset( $this->aliases[ $alias ] ) ) { |
|
95 | - throw new DefinedAliasException; |
|
96 | - } |
|
97 | - |
|
98 | - $this->aliases[ $alias ] = true; |
|
99 | - $this->definitions[ $alias ] = $definition; |
|
100 | - |
|
101 | - // Closures are treated as factories unless |
|
102 | - // defined via Container::share. |
|
103 | - if ( ! $definition instanceof \Closure ) { |
|
104 | - $this->shared[ $alias ] = true; |
|
105 | - } |
|
106 | - |
|
107 | - if ( isset( $class ) ) { |
|
108 | - $this->classes[ $class ] = $alias; |
|
109 | - } |
|
110 | - |
|
111 | - return $this; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * {@inheritdoc} |
|
116 | - * |
|
117 | - * @param string|array $alias |
|
118 | - * @param mixed $definition |
|
119 | - * |
|
120 | - * @throws DefinedAliasException |
|
121 | - * |
|
122 | - * @return $this |
|
123 | - */ |
|
124 | - public function share( $alias, $definition ) { |
|
125 | - $this->define( $alias, $definition ); |
|
126 | - |
|
127 | - if ( is_array( $alias ) ) { |
|
128 | - $alias = key( $alias ); |
|
129 | - } |
|
130 | - |
|
131 | - $this->shared[ $alias ] = true; |
|
132 | - |
|
133 | - return $this; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * {@inheritdoc} |
|
138 | - * |
|
139 | - * @param string $alias |
|
140 | - * |
|
141 | - * @throws UndefinedAliasException |
|
142 | - * |
|
143 | - * @return mixed |
|
144 | - */ |
|
145 | - public function fetch( $alias ) { |
|
146 | - if ( isset( $this->classes[ $alias ] ) ) { |
|
147 | - // If the alias is a class name, |
|
148 | - // then retrieve its linked alias. |
|
149 | - // This is only registered when |
|
150 | - // registering using an array. |
|
151 | - $alias = $this->classes[ $alias ]; |
|
152 | - } |
|
153 | - |
|
154 | - if ( ! isset( $this->aliases[ $alias ] ) ) { |
|
155 | - throw new UndefinedAliasException; |
|
156 | - } |
|
157 | - |
|
158 | - $value = $this->definitions[ $alias ]; |
|
159 | - |
|
160 | - // If the shared value is a closure, |
|
161 | - // execute it and assign the result |
|
162 | - // in place of the closure. |
|
163 | - if ( $value instanceof \Closure ) { |
|
164 | - $factory = $value; |
|
165 | - $value = $factory( $this ); |
|
166 | - } |
|
167 | - |
|
168 | - // If the value is shared, save the shared value. |
|
169 | - if ( isset( $this->shared[ $alias ] ) ) { |
|
170 | - $this->definitions[ $alias ] = $value; |
|
171 | - } |
|
172 | - |
|
173 | - // Return the fetched value. |
|
174 | - return $value; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * {@inheritdoc} |
|
179 | - * |
|
180 | - * @param string $alias |
|
181 | - * |
|
182 | - * @return bool |
|
183 | - */ |
|
184 | - public function has( $alias ) { |
|
185 | - return isset( $this->aliases[ $alias ] ); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * {@inheritDoc} |
|
190 | - * |
|
191 | - * @param string $alias |
|
192 | - * |
|
193 | - * @return $this |
|
194 | - */ |
|
195 | - public function remove( $alias ) { |
|
196 | - if ( isset( $this->aliases[ $alias ] ) ) { |
|
197 | - /** |
|
198 | - * If there's no reference in the aliases array, |
|
199 | - * the service won't be found on fetching and |
|
200 | - * can be overwritten on setting. |
|
201 | - * |
|
202 | - * Pros: Quick setting/unsetting, faster |
|
203 | - * performance on those operations when doing |
|
204 | - * a lot of these. |
|
205 | - * |
|
206 | - * Cons: Objects and values set in the container |
|
207 | - * can't get garbage collected. |
|
208 | - * |
|
209 | - * If this is a problem, this may need to be revisited. |
|
210 | - */ |
|
211 | - unset( $this->aliases[ $alias ] ); |
|
212 | - } |
|
213 | - |
|
214 | - return $this; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * {@inheritDoc} |
|
219 | - * |
|
220 | - * @param ServiceProvider $provider |
|
221 | - * |
|
222 | - * @return $this |
|
223 | - */ |
|
224 | - public function register( ServiceProvider $provider ) { |
|
225 | - $provider->register( $this ); |
|
226 | - |
|
227 | - return $this; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Set a value into the container. |
|
232 | - * |
|
233 | - * @param string $id |
|
234 | - * @param mixed $value |
|
235 | - * |
|
236 | - * @see define |
|
237 | - */ |
|
238 | - public function offsetSet( $id, $value ) { |
|
239 | - $this->define( $id, $value ); |
|
240 | - } |
|
241 | - |
|
242 | - /** |
|
243 | - * Get an value from the container. |
|
244 | - * |
|
245 | - * @param string $id |
|
246 | - * |
|
247 | - * @return object |
|
248 | - * @throws UndefinedAliasException |
|
249 | - * |
|
250 | - * @see fetch |
|
251 | - */ |
|
252 | - public function offsetGet( $id ) { |
|
253 | - return $this->fetch( $id ); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Checks if a key is set on the container. |
|
258 | - * |
|
259 | - * @param string $id |
|
260 | - * |
|
261 | - * @return bool |
|
262 | - * |
|
263 | - * @see has |
|
264 | - */ |
|
265 | - public function offsetExists( $id ) { |
|
266 | - return $this->has( $id ); |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * Remove a key from the container. |
|
271 | - * |
|
272 | - * @param string $id |
|
273 | - * |
|
274 | - * @see remove |
|
275 | - */ |
|
276 | - public function offsetUnset( $id ) { |
|
277 | - $this->remove( $id ); |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * Sets the object properties to prepare for the loop. |
|
282 | - */ |
|
283 | - public function rewind() { |
|
284 | - $this->position = 0; |
|
285 | - $this->keys = array_keys( $this->aliases ); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Retrieves the service object for the current step in the loop. |
|
290 | - * |
|
291 | - * @return object |
|
292 | - */ |
|
293 | - public function current() { |
|
294 | - return $this->fetch( $this->keys[ $this->position ] ); |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * Retrieves the key for the current step in the loop. |
|
299 | - * |
|
300 | - * @return string |
|
301 | - */ |
|
302 | - public function key() { |
|
303 | - return $this->keys[ $this->position ]; |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * Increments to the next step in the loop. |
|
308 | - */ |
|
309 | - public function next() { |
|
310 | - $this->position ++; |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * Checks if the next step in the loop in valid. |
|
315 | - * |
|
316 | - * @return bool |
|
317 | - */ |
|
318 | - public function valid() { |
|
319 | - return isset( $this->keys[ $this->position ] ); |
|
320 | - } |
|
16 | + /** |
|
17 | + * Registered definitions. |
|
18 | + * |
|
19 | + * @var mixed[] |
|
20 | + */ |
|
21 | + private $definitions = array(); |
|
22 | + |
|
23 | + /** |
|
24 | + * Aliases to share between fetches. |
|
25 | + * |
|
26 | + * @var <string, true>[] |
|
27 | + */ |
|
28 | + private $shared = array(); |
|
29 | + |
|
30 | + /** |
|
31 | + * Aliases of all the registered services. |
|
32 | + * |
|
33 | + * @var <string, true>[] |
|
34 | + */ |
|
35 | + private $aliases = array(); |
|
36 | + |
|
37 | + /** |
|
38 | + * Array of classes registered on the container. |
|
39 | + * |
|
40 | + * @var <string, true>[] |
|
41 | + */ |
|
42 | + private $classes = array(); |
|
43 | + |
|
44 | + /** |
|
45 | + * Current position in the loop. |
|
46 | + * |
|
47 | + * @var int |
|
48 | + */ |
|
49 | + private $position; |
|
50 | + |
|
51 | + /** |
|
52 | + * 0-indexed array of aliases for looping. |
|
53 | + * |
|
54 | + * @var string[] |
|
55 | + */ |
|
56 | + private $keys = array(); |
|
57 | + |
|
58 | + /** |
|
59 | + * Create a new container with the given providers. |
|
60 | + * |
|
61 | + * Providers can be instances or the class of the provider as a string. |
|
62 | + * |
|
63 | + * @param ServiceProvider[]|string[] $providers |
|
64 | + */ |
|
65 | + public function __construct( array $providers = array() ) { |
|
66 | + foreach ( $providers as $provider ) { |
|
67 | + if ( is_string( $provider ) && class_exists( $provider ) ) { |
|
68 | + $provider = new $provider; |
|
69 | + } |
|
70 | + |
|
71 | + if ( $provider instanceof ServiceProvider ) { |
|
72 | + $this->register( $provider ); |
|
73 | + } |
|
74 | + } |
|
75 | + } |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * {@inheritdoc} |
|
80 | + * |
|
81 | + * @param string|array $alias |
|
82 | + * @param mixed $definition |
|
83 | + * |
|
84 | + * @throws DefinedAliasException |
|
85 | + * |
|
86 | + * @return $this |
|
87 | + */ |
|
88 | + public function define( $alias, $definition ) { |
|
89 | + if ( is_array( $alias ) ) { |
|
90 | + $class = current( $alias ); |
|
91 | + $alias = key( $alias ); |
|
92 | + } |
|
93 | + |
|
94 | + if ( isset( $this->aliases[ $alias ] ) ) { |
|
95 | + throw new DefinedAliasException; |
|
96 | + } |
|
97 | + |
|
98 | + $this->aliases[ $alias ] = true; |
|
99 | + $this->definitions[ $alias ] = $definition; |
|
100 | + |
|
101 | + // Closures are treated as factories unless |
|
102 | + // defined via Container::share. |
|
103 | + if ( ! $definition instanceof \Closure ) { |
|
104 | + $this->shared[ $alias ] = true; |
|
105 | + } |
|
106 | + |
|
107 | + if ( isset( $class ) ) { |
|
108 | + $this->classes[ $class ] = $alias; |
|
109 | + } |
|
110 | + |
|
111 | + return $this; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * {@inheritdoc} |
|
116 | + * |
|
117 | + * @param string|array $alias |
|
118 | + * @param mixed $definition |
|
119 | + * |
|
120 | + * @throws DefinedAliasException |
|
121 | + * |
|
122 | + * @return $this |
|
123 | + */ |
|
124 | + public function share( $alias, $definition ) { |
|
125 | + $this->define( $alias, $definition ); |
|
126 | + |
|
127 | + if ( is_array( $alias ) ) { |
|
128 | + $alias = key( $alias ); |
|
129 | + } |
|
130 | + |
|
131 | + $this->shared[ $alias ] = true; |
|
132 | + |
|
133 | + return $this; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * {@inheritdoc} |
|
138 | + * |
|
139 | + * @param string $alias |
|
140 | + * |
|
141 | + * @throws UndefinedAliasException |
|
142 | + * |
|
143 | + * @return mixed |
|
144 | + */ |
|
145 | + public function fetch( $alias ) { |
|
146 | + if ( isset( $this->classes[ $alias ] ) ) { |
|
147 | + // If the alias is a class name, |
|
148 | + // then retrieve its linked alias. |
|
149 | + // This is only registered when |
|
150 | + // registering using an array. |
|
151 | + $alias = $this->classes[ $alias ]; |
|
152 | + } |
|
153 | + |
|
154 | + if ( ! isset( $this->aliases[ $alias ] ) ) { |
|
155 | + throw new UndefinedAliasException; |
|
156 | + } |
|
157 | + |
|
158 | + $value = $this->definitions[ $alias ]; |
|
159 | + |
|
160 | + // If the shared value is a closure, |
|
161 | + // execute it and assign the result |
|
162 | + // in place of the closure. |
|
163 | + if ( $value instanceof \Closure ) { |
|
164 | + $factory = $value; |
|
165 | + $value = $factory( $this ); |
|
166 | + } |
|
167 | + |
|
168 | + // If the value is shared, save the shared value. |
|
169 | + if ( isset( $this->shared[ $alias ] ) ) { |
|
170 | + $this->definitions[ $alias ] = $value; |
|
171 | + } |
|
172 | + |
|
173 | + // Return the fetched value. |
|
174 | + return $value; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * {@inheritdoc} |
|
179 | + * |
|
180 | + * @param string $alias |
|
181 | + * |
|
182 | + * @return bool |
|
183 | + */ |
|
184 | + public function has( $alias ) { |
|
185 | + return isset( $this->aliases[ $alias ] ); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * {@inheritDoc} |
|
190 | + * |
|
191 | + * @param string $alias |
|
192 | + * |
|
193 | + * @return $this |
|
194 | + */ |
|
195 | + public function remove( $alias ) { |
|
196 | + if ( isset( $this->aliases[ $alias ] ) ) { |
|
197 | + /** |
|
198 | + * If there's no reference in the aliases array, |
|
199 | + * the service won't be found on fetching and |
|
200 | + * can be overwritten on setting. |
|
201 | + * |
|
202 | + * Pros: Quick setting/unsetting, faster |
|
203 | + * performance on those operations when doing |
|
204 | + * a lot of these. |
|
205 | + * |
|
206 | + * Cons: Objects and values set in the container |
|
207 | + * can't get garbage collected. |
|
208 | + * |
|
209 | + * If this is a problem, this may need to be revisited. |
|
210 | + */ |
|
211 | + unset( $this->aliases[ $alias ] ); |
|
212 | + } |
|
213 | + |
|
214 | + return $this; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * {@inheritDoc} |
|
219 | + * |
|
220 | + * @param ServiceProvider $provider |
|
221 | + * |
|
222 | + * @return $this |
|
223 | + */ |
|
224 | + public function register( ServiceProvider $provider ) { |
|
225 | + $provider->register( $this ); |
|
226 | + |
|
227 | + return $this; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Set a value into the container. |
|
232 | + * |
|
233 | + * @param string $id |
|
234 | + * @param mixed $value |
|
235 | + * |
|
236 | + * @see define |
|
237 | + */ |
|
238 | + public function offsetSet( $id, $value ) { |
|
239 | + $this->define( $id, $value ); |
|
240 | + } |
|
241 | + |
|
242 | + /** |
|
243 | + * Get an value from the container. |
|
244 | + * |
|
245 | + * @param string $id |
|
246 | + * |
|
247 | + * @return object |
|
248 | + * @throws UndefinedAliasException |
|
249 | + * |
|
250 | + * @see fetch |
|
251 | + */ |
|
252 | + public function offsetGet( $id ) { |
|
253 | + return $this->fetch( $id ); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Checks if a key is set on the container. |
|
258 | + * |
|
259 | + * @param string $id |
|
260 | + * |
|
261 | + * @return bool |
|
262 | + * |
|
263 | + * @see has |
|
264 | + */ |
|
265 | + public function offsetExists( $id ) { |
|
266 | + return $this->has( $id ); |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * Remove a key from the container. |
|
271 | + * |
|
272 | + * @param string $id |
|
273 | + * |
|
274 | + * @see remove |
|
275 | + */ |
|
276 | + public function offsetUnset( $id ) { |
|
277 | + $this->remove( $id ); |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * Sets the object properties to prepare for the loop. |
|
282 | + */ |
|
283 | + public function rewind() { |
|
284 | + $this->position = 0; |
|
285 | + $this->keys = array_keys( $this->aliases ); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Retrieves the service object for the current step in the loop. |
|
290 | + * |
|
291 | + * @return object |
|
292 | + */ |
|
293 | + public function current() { |
|
294 | + return $this->fetch( $this->keys[ $this->position ] ); |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * Retrieves the key for the current step in the loop. |
|
299 | + * |
|
300 | + * @return string |
|
301 | + */ |
|
302 | + public function key() { |
|
303 | + return $this->keys[ $this->position ]; |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * Increments to the next step in the loop. |
|
308 | + */ |
|
309 | + public function next() { |
|
310 | + $this->position ++; |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * Checks if the next step in the loop in valid. |
|
315 | + * |
|
316 | + * @return bool |
|
317 | + */ |
|
318 | + public function valid() { |
|
319 | + return isset( $this->keys[ $this->position ] ); |
|
320 | + } |
|
321 | 321 | } |
@@ -18,80 +18,80 @@ |
||
18 | 18 | * @subpackage Model |
19 | 19 | */ |
20 | 20 | class Post extends Base { |
21 | - /** |
|
22 | - * Post type this model maps to. |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $type = 'post'; |
|
21 | + /** |
|
22 | + * Post type this model maps to. |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $type = 'post'; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Fillable attributes for the model. |
|
30 | - * |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - protected $fillable = array( 'ID', 'author', 'slug', 'title', 'publish_date', 'content', 'excerpt' ); |
|
28 | + /** |
|
29 | + * Fillable attributes for the model. |
|
30 | + * |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + protected $fillable = array( 'ID', 'author', 'slug', 'title', 'publish_date', 'content', 'excerpt' ); |
|
34 | 34 | |
35 | - /** |
|
36 | - * ID property maps to ID. |
|
37 | - * |
|
38 | - * @return string |
|
39 | - */ |
|
40 | - protected function map_ID() { |
|
41 | - return 'ID'; |
|
42 | - } |
|
35 | + /** |
|
36 | + * ID property maps to ID. |
|
37 | + * |
|
38 | + * @return string |
|
39 | + */ |
|
40 | + protected function map_ID() { |
|
41 | + return 'ID'; |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Author property maps to post_author. |
|
46 | - * |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - protected function map_author() { |
|
50 | - return 'post_author'; |
|
51 | - } |
|
44 | + /** |
|
45 | + * Author property maps to post_author. |
|
46 | + * |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + protected function map_author() { |
|
50 | + return 'post_author'; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Slug property maps to post_name. |
|
55 | - * |
|
56 | - * @return string |
|
57 | - */ |
|
58 | - protected function map_slug() { |
|
59 | - return 'post_name'; |
|
60 | - } |
|
53 | + /** |
|
54 | + * Slug property maps to post_name. |
|
55 | + * |
|
56 | + * @return string |
|
57 | + */ |
|
58 | + protected function map_slug() { |
|
59 | + return 'post_name'; |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Title property maps to post_title. |
|
64 | - * |
|
65 | - * @return string |
|
66 | - */ |
|
67 | - protected function map_title() { |
|
68 | - return 'post_title'; |
|
69 | - } |
|
62 | + /** |
|
63 | + * Title property maps to post_title. |
|
64 | + * |
|
65 | + * @return string |
|
66 | + */ |
|
67 | + protected function map_title() { |
|
68 | + return 'post_title'; |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Publish date property maps to post_date_gmt. |
|
73 | - * |
|
74 | - * @return string |
|
75 | - */ |
|
76 | - protected function map_publish_date() { |
|
77 | - return 'post_date_gmt'; |
|
78 | - } |
|
71 | + /** |
|
72 | + * Publish date property maps to post_date_gmt. |
|
73 | + * |
|
74 | + * @return string |
|
75 | + */ |
|
76 | + protected function map_publish_date() { |
|
77 | + return 'post_date_gmt'; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * Content property maps to post_content. |
|
82 | - * |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - protected function map_content() { |
|
86 | - return 'post_content'; |
|
87 | - } |
|
80 | + /** |
|
81 | + * Content property maps to post_content. |
|
82 | + * |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + protected function map_content() { |
|
86 | + return 'post_content'; |
|
87 | + } |
|
88 | 88 | |
89 | - /**¡ |
|
89 | + /**¡ |
|
90 | 90 | * Excerpt property maps to post_excerpt. |
91 | 91 | * |
92 | 92 | * @return string |
93 | 93 | */ |
94 | - protected function map_excerpt() { |
|
95 | - return 'post_excerpt'; |
|
96 | - } |
|
94 | + protected function map_excerpt() { |
|
95 | + return 'post_excerpt'; |
|
96 | + } |
|
97 | 97 | } |
@@ -17,258 +17,258 @@ |
||
17 | 17 | * @since 0.1.0 |
18 | 18 | */ |
19 | 19 | abstract class Base { |
20 | - /** |
|
21 | - * Model attributes array. |
|
22 | - * |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - private $attributes = array( |
|
26 | - 'table' => array(), |
|
27 | - 'post' => null, |
|
28 | - ); |
|
29 | - |
|
30 | - /** |
|
31 | - * Which custom table does this model uses. |
|
32 | - * |
|
33 | - * If false, model wil fall back to postmeta. |
|
34 | - * |
|
35 | - * @var bool|string |
|
36 | - */ |
|
37 | - protected $table = false; |
|
38 | - |
|
39 | - /** |
|
40 | - * Whether to use WP_Post mappings. |
|
41 | - * |
|
42 | - * @var bool |
|
43 | - */ |
|
44 | - protected $post = true; |
|
45 | - |
|
46 | - /** |
|
47 | - * Custom post type. |
|
48 | - * |
|
49 | - * @var bool|string |
|
50 | - */ |
|
51 | - protected $type = false; |
|
52 | - |
|
53 | - /** |
|
54 | - * Properties which are allowed to be set on the model. |
|
55 | - * |
|
56 | - * If this array is empty, any attributes can be set on the model. |
|
57 | - * |
|
58 | - * @var string[] |
|
59 | - */ |
|
60 | - protected $fillable = array(); |
|
61 | - |
|
62 | - /** |
|
63 | - * Constructs a new model with provided attributes. |
|
64 | - * |
|
65 | - * If 'post' is passed as one of the attributes |
|
66 | - * |
|
67 | - * @param array <string, mixed> $attributes |
|
68 | - */ |
|
69 | - public function __construct( array $attributes = array() ) { |
|
70 | - foreach ( $attributes as $name => $value ) { |
|
71 | - $this->set_attribute( $name, $value ); |
|
72 | - } |
|
73 | - |
|
74 | - if ( $this->post && ! isset( $this->attributes['post'] ) ) { |
|
75 | - $this->create_default_post(); |
|
76 | - } |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Get the model's attributes. |
|
81 | - * |
|
82 | - * Returns the array of for the model that will either need to be |
|
83 | - * saved in postmeta or a separate table. |
|
84 | - * |
|
85 | - * @return array |
|
86 | - */ |
|
87 | - public function get_attributes() { |
|
88 | - return $this->attributes['table']; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Get the model's underlying post. |
|
93 | - * |
|
94 | - * Returns the underlying WP_Post object for the model, representing |
|
95 | - * the data that will be save in the wp_posts table. |
|
96 | - * |
|
97 | - * @return false|WP_Post |
|
98 | - */ |
|
99 | - public function get_underlying_post() { |
|
100 | - if ( isset( $this->attributes['post'] ) ) { |
|
101 | - return $this->attributes['post']; |
|
102 | - } |
|
103 | - |
|
104 | - return false; |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Magic __set method. |
|
109 | - * |
|
110 | - * Passes the name and value to set_attribute, which is where the magic happens. |
|
111 | - * |
|
112 | - * @param string $name |
|
113 | - * @param mixed $value |
|
114 | - */ |
|
115 | - public function __set( $name, $value ) { |
|
116 | - $this->set_attribute( $name, $value ); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Sets the model attributes. |
|
121 | - * |
|
122 | - * Checks whether the model attribute can be set, check if it |
|
123 | - * maps to the WP_Post property, otherwise, assigns it to the |
|
124 | - * table attribute array. |
|
125 | - * |
|
126 | - * @param string $name |
|
127 | - * @param mixed $value |
|
128 | - */ |
|
129 | - private function set_attribute( $name, $value ) { |
|
130 | - if ( 'post' === $name ) { |
|
131 | - $this->override_post( $value ); |
|
132 | - |
|
133 | - return; |
|
134 | - } |
|
135 | - |
|
136 | - if ( ! $this->is_fillable( $name ) ) { |
|
137 | - return; |
|
138 | - } |
|
139 | - |
|
140 | - if ( $method = $this->has_map_method( $name ) ) { |
|
141 | - $this->attributes['post']->{$this->{$method}()} = $value; |
|
142 | - |
|
143 | - return; |
|
144 | - } |
|
145 | - |
|
146 | - $this->attributes['table'][ $name ] = $value; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Checks if a given attribute is mass-fillable. |
|
151 | - * |
|
152 | - * Returns true if the attribute can be filled, false if it can't. |
|
153 | - * |
|
154 | - * @param string $name |
|
155 | - * |
|
156 | - * @return bool |
|
157 | - */ |
|
158 | - private function is_fillable( $name ) { |
|
159 | - // `type` is not fillable at all. |
|
160 | - if ( 'type' === $name ) { |
|
161 | - return false; |
|
162 | - } |
|
163 | - |
|
164 | - // If the `$fillable` array hasn't been defined, pass everything. |
|
165 | - if ( ! $this->fillable ) { |
|
166 | - return true; |
|
167 | - } |
|
168 | - |
|
169 | - return in_array( $name, $this->fillable ); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Overrides the current WP_Post with a provided one. |
|
174 | - * |
|
175 | - * Resets the post's default values and stores it in the attributes. |
|
176 | - * |
|
177 | - * @param WP_Post $value |
|
178 | - */ |
|
179 | - private function override_post( WP_Post $value ) { |
|
180 | - $this->attributes['post'] = $this->enforce_post_defaults( $value ); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Create and set with a new blank post. |
|
185 | - * |
|
186 | - * Creates a new WP_Post object, assigns it the default attributes, |
|
187 | - * and stores it in the attributes. |
|
188 | - */ |
|
189 | - private function create_default_post() { |
|
190 | - $this->attributes['post'] = $this->enforce_post_defaults( new WP_Post( new stdClass ) ); |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Enforces values on the post that can't change. |
|
195 | - * |
|
196 | - * Primarily, this is used to make sure the post_type always maps |
|
197 | - * to the model's "$type" property, but this can all be overridden |
|
198 | - * by the developer to enforce other values in the model. |
|
199 | - * |
|
200 | - * @param WP_Post $post |
|
201 | - * |
|
202 | - * @return WP_Post |
|
203 | - */ |
|
204 | - protected function enforce_post_defaults( WP_Post $post ) { |
|
205 | - if ( is_string( $this->type ) ) { |
|
206 | - $post->post_type = $this->type; |
|
207 | - } |
|
208 | - |
|
209 | - return $post; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Magic __get method. |
|
214 | - * |
|
215 | - * Passes the name and value to get_attribute, which is where the magic happens. |
|
216 | - * |
|
217 | - * @param string $name |
|
218 | - * |
|
219 | - * @return mixed |
|
220 | - */ |
|
221 | - public function __get( $name ) { |
|
222 | - return $this->get_attribute( $name ); |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Retrieves the model attribute. |
|
227 | - * |
|
228 | - * If the attribute maps to the WP_Post, retrieves it from there. |
|
229 | - * Otherwise, checks if it's in the attributes array |
|
230 | - * |
|
231 | - * @param string $name |
|
232 | - * |
|
233 | - * @return mixed |
|
234 | - * |
|
235 | - * @throws PropertyDoesNotExistException If property isn't found. |
|
236 | - */ |
|
237 | - protected function get_attribute( $name ) { |
|
238 | - if ( 'type' === $name ) { |
|
239 | - return $this->type; |
|
240 | - } |
|
241 | - |
|
242 | - if ( $method = $this->has_map_method( $name ) ) { |
|
243 | - $value = $this->attributes['post']->{$this->{$method}()}; |
|
244 | - } else { |
|
245 | - if ( ! isset( $this->attributes['table'][ $name ] ) ) { |
|
246 | - throw new PropertyDoesNotExistException; |
|
247 | - } |
|
248 | - |
|
249 | - $value = $this->attributes['table'][ $name ]; |
|
250 | - } |
|
251 | - |
|
252 | - return $value; |
|
253 | - |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Checks whether the attribute has a map method. |
|
258 | - * |
|
259 | - * This is used to determine whether the attribute maps to a |
|
260 | - * property on the underlying WP_Post object. Returns the |
|
261 | - * method if one exists, returns false if it doesn't. |
|
262 | - * |
|
263 | - * @param string $name |
|
264 | - * |
|
265 | - * @return false|string |
|
266 | - */ |
|
267 | - protected function has_map_method( $name ) { |
|
268 | - if ( method_exists( $this, $method = "map_{$name}" ) ) { |
|
269 | - return $method; |
|
270 | - } |
|
271 | - |
|
272 | - return false; |
|
273 | - } |
|
20 | + /** |
|
21 | + * Model attributes array. |
|
22 | + * |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + private $attributes = array( |
|
26 | + 'table' => array(), |
|
27 | + 'post' => null, |
|
28 | + ); |
|
29 | + |
|
30 | + /** |
|
31 | + * Which custom table does this model uses. |
|
32 | + * |
|
33 | + * If false, model wil fall back to postmeta. |
|
34 | + * |
|
35 | + * @var bool|string |
|
36 | + */ |
|
37 | + protected $table = false; |
|
38 | + |
|
39 | + /** |
|
40 | + * Whether to use WP_Post mappings. |
|
41 | + * |
|
42 | + * @var bool |
|
43 | + */ |
|
44 | + protected $post = true; |
|
45 | + |
|
46 | + /** |
|
47 | + * Custom post type. |
|
48 | + * |
|
49 | + * @var bool|string |
|
50 | + */ |
|
51 | + protected $type = false; |
|
52 | + |
|
53 | + /** |
|
54 | + * Properties which are allowed to be set on the model. |
|
55 | + * |
|
56 | + * If this array is empty, any attributes can be set on the model. |
|
57 | + * |
|
58 | + * @var string[] |
|
59 | + */ |
|
60 | + protected $fillable = array(); |
|
61 | + |
|
62 | + /** |
|
63 | + * Constructs a new model with provided attributes. |
|
64 | + * |
|
65 | + * If 'post' is passed as one of the attributes |
|
66 | + * |
|
67 | + * @param array <string, mixed> $attributes |
|
68 | + */ |
|
69 | + public function __construct( array $attributes = array() ) { |
|
70 | + foreach ( $attributes as $name => $value ) { |
|
71 | + $this->set_attribute( $name, $value ); |
|
72 | + } |
|
73 | + |
|
74 | + if ( $this->post && ! isset( $this->attributes['post'] ) ) { |
|
75 | + $this->create_default_post(); |
|
76 | + } |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Get the model's attributes. |
|
81 | + * |
|
82 | + * Returns the array of for the model that will either need to be |
|
83 | + * saved in postmeta or a separate table. |
|
84 | + * |
|
85 | + * @return array |
|
86 | + */ |
|
87 | + public function get_attributes() { |
|
88 | + return $this->attributes['table']; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Get the model's underlying post. |
|
93 | + * |
|
94 | + * Returns the underlying WP_Post object for the model, representing |
|
95 | + * the data that will be save in the wp_posts table. |
|
96 | + * |
|
97 | + * @return false|WP_Post |
|
98 | + */ |
|
99 | + public function get_underlying_post() { |
|
100 | + if ( isset( $this->attributes['post'] ) ) { |
|
101 | + return $this->attributes['post']; |
|
102 | + } |
|
103 | + |
|
104 | + return false; |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Magic __set method. |
|
109 | + * |
|
110 | + * Passes the name and value to set_attribute, which is where the magic happens. |
|
111 | + * |
|
112 | + * @param string $name |
|
113 | + * @param mixed $value |
|
114 | + */ |
|
115 | + public function __set( $name, $value ) { |
|
116 | + $this->set_attribute( $name, $value ); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Sets the model attributes. |
|
121 | + * |
|
122 | + * Checks whether the model attribute can be set, check if it |
|
123 | + * maps to the WP_Post property, otherwise, assigns it to the |
|
124 | + * table attribute array. |
|
125 | + * |
|
126 | + * @param string $name |
|
127 | + * @param mixed $value |
|
128 | + */ |
|
129 | + private function set_attribute( $name, $value ) { |
|
130 | + if ( 'post' === $name ) { |
|
131 | + $this->override_post( $value ); |
|
132 | + |
|
133 | + return; |
|
134 | + } |
|
135 | + |
|
136 | + if ( ! $this->is_fillable( $name ) ) { |
|
137 | + return; |
|
138 | + } |
|
139 | + |
|
140 | + if ( $method = $this->has_map_method( $name ) ) { |
|
141 | + $this->attributes['post']->{$this->{$method}()} = $value; |
|
142 | + |
|
143 | + return; |
|
144 | + } |
|
145 | + |
|
146 | + $this->attributes['table'][ $name ] = $value; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Checks if a given attribute is mass-fillable. |
|
151 | + * |
|
152 | + * Returns true if the attribute can be filled, false if it can't. |
|
153 | + * |
|
154 | + * @param string $name |
|
155 | + * |
|
156 | + * @return bool |
|
157 | + */ |
|
158 | + private function is_fillable( $name ) { |
|
159 | + // `type` is not fillable at all. |
|
160 | + if ( 'type' === $name ) { |
|
161 | + return false; |
|
162 | + } |
|
163 | + |
|
164 | + // If the `$fillable` array hasn't been defined, pass everything. |
|
165 | + if ( ! $this->fillable ) { |
|
166 | + return true; |
|
167 | + } |
|
168 | + |
|
169 | + return in_array( $name, $this->fillable ); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Overrides the current WP_Post with a provided one. |
|
174 | + * |
|
175 | + * Resets the post's default values and stores it in the attributes. |
|
176 | + * |
|
177 | + * @param WP_Post $value |
|
178 | + */ |
|
179 | + private function override_post( WP_Post $value ) { |
|
180 | + $this->attributes['post'] = $this->enforce_post_defaults( $value ); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Create and set with a new blank post. |
|
185 | + * |
|
186 | + * Creates a new WP_Post object, assigns it the default attributes, |
|
187 | + * and stores it in the attributes. |
|
188 | + */ |
|
189 | + private function create_default_post() { |
|
190 | + $this->attributes['post'] = $this->enforce_post_defaults( new WP_Post( new stdClass ) ); |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Enforces values on the post that can't change. |
|
195 | + * |
|
196 | + * Primarily, this is used to make sure the post_type always maps |
|
197 | + * to the model's "$type" property, but this can all be overridden |
|
198 | + * by the developer to enforce other values in the model. |
|
199 | + * |
|
200 | + * @param WP_Post $post |
|
201 | + * |
|
202 | + * @return WP_Post |
|
203 | + */ |
|
204 | + protected function enforce_post_defaults( WP_Post $post ) { |
|
205 | + if ( is_string( $this->type ) ) { |
|
206 | + $post->post_type = $this->type; |
|
207 | + } |
|
208 | + |
|
209 | + return $post; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Magic __get method. |
|
214 | + * |
|
215 | + * Passes the name and value to get_attribute, which is where the magic happens. |
|
216 | + * |
|
217 | + * @param string $name |
|
218 | + * |
|
219 | + * @return mixed |
|
220 | + */ |
|
221 | + public function __get( $name ) { |
|
222 | + return $this->get_attribute( $name ); |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Retrieves the model attribute. |
|
227 | + * |
|
228 | + * If the attribute maps to the WP_Post, retrieves it from there. |
|
229 | + * Otherwise, checks if it's in the attributes array |
|
230 | + * |
|
231 | + * @param string $name |
|
232 | + * |
|
233 | + * @return mixed |
|
234 | + * |
|
235 | + * @throws PropertyDoesNotExistException If property isn't found. |
|
236 | + */ |
|
237 | + protected function get_attribute( $name ) { |
|
238 | + if ( 'type' === $name ) { |
|
239 | + return $this->type; |
|
240 | + } |
|
241 | + |
|
242 | + if ( $method = $this->has_map_method( $name ) ) { |
|
243 | + $value = $this->attributes['post']->{$this->{$method}()}; |
|
244 | + } else { |
|
245 | + if ( ! isset( $this->attributes['table'][ $name ] ) ) { |
|
246 | + throw new PropertyDoesNotExistException; |
|
247 | + } |
|
248 | + |
|
249 | + $value = $this->attributes['table'][ $name ]; |
|
250 | + } |
|
251 | + |
|
252 | + return $value; |
|
253 | + |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Checks whether the attribute has a map method. |
|
258 | + * |
|
259 | + * This is used to determine whether the attribute maps to a |
|
260 | + * property on the underlying WP_Post object. Returns the |
|
261 | + * method if one exists, returns false if it doesn't. |
|
262 | + * |
|
263 | + * @param string $name |
|
264 | + * |
|
265 | + * @return false|string |
|
266 | + */ |
|
267 | + protected function has_map_method( $name ) { |
|
268 | + if ( method_exists( $this, $method = "map_{$name}" ) ) { |
|
269 | + return $method; |
|
270 | + } |
|
271 | + |
|
272 | + return false; |
|
273 | + } |
|
274 | 274 | } |
@@ -2,13 +2,13 @@ |
||
2 | 2 | namespace Intraxia\Jaxion\Contract\Core; |
3 | 3 | |
4 | 4 | interface HasFilters { |
5 | - /** |
|
6 | - * Provides the array of filters the class wants to register with WordPress. |
|
7 | - * |
|
8 | - * These filters are retrieved by the Loader class and used to register the |
|
9 | - * correct service methods with WordPress. |
|
10 | - * |
|
11 | - * @return array[] |
|
12 | - */ |
|
13 | - public function filter_hooks(); |
|
5 | + /** |
|
6 | + * Provides the array of filters the class wants to register with WordPress. |
|
7 | + * |
|
8 | + * These filters are retrieved by the Loader class and used to register the |
|
9 | + * correct service methods with WordPress. |
|
10 | + * |
|
11 | + * @return array[] |
|
12 | + */ |
|
13 | + public function filter_hooks(); |
|
14 | 14 | } |
15 | 15 | \ No newline at end of file |
@@ -4,64 +4,64 @@ |
||
4 | 4 | use Intraxia\Jaxion\Core\ApplicationNotBootedException; |
5 | 5 | |
6 | 6 | interface Application { |
7 | - /** |
|
8 | - * Instantiates a new Application container. |
|
9 | - * |
|
10 | - * The Application constructor enforces the presence of of a single instance |
|
11 | - * of the Application. If an instance already exists, an Exception will be thrown. |
|
12 | - * |
|
13 | - * @param string $file |
|
14 | - * |
|
15 | - * @throws \Intraxia\Jaxion\Core\ApplicationAlreadyBootedException |
|
16 | - */ |
|
17 | - public function __construct( $file ); |
|
7 | + /** |
|
8 | + * Instantiates a new Application container. |
|
9 | + * |
|
10 | + * The Application constructor enforces the presence of of a single instance |
|
11 | + * of the Application. If an instance already exists, an Exception will be thrown. |
|
12 | + * |
|
13 | + * @param string $file |
|
14 | + * |
|
15 | + * @throws \Intraxia\Jaxion\Core\ApplicationAlreadyBootedException |
|
16 | + */ |
|
17 | + public function __construct( $file ); |
|
18 | 18 | |
19 | - /** |
|
20 | - * Starts up the Application. |
|
21 | - * |
|
22 | - * Retrieves the Application's loader instance, and registers all of the Application's |
|
23 | - * services with the loader. It loops through its own services, and any services that |
|
24 | - * implement the required interface are passed into the loader. It then attaches the |
|
25 | - * loader to the required WordPress hook. |
|
26 | - */ |
|
27 | - public function boot(); |
|
19 | + /** |
|
20 | + * Starts up the Application. |
|
21 | + * |
|
22 | + * Retrieves the Application's loader instance, and registers all of the Application's |
|
23 | + * services with the loader. It loops through its own services, and any services that |
|
24 | + * implement the required interface are passed into the loader. It then attaches the |
|
25 | + * loader to the required WordPress hook. |
|
26 | + */ |
|
27 | + public function boot(); |
|
28 | 28 | |
29 | - /** |
|
30 | - * Fired on plugin activation. |
|
31 | - * |
|
32 | - * This function is attached to `register_activation_hook` and is fired when the plugin is |
|
33 | - * activated by WordPress. This gives the developer a place to set up any options, |
|
34 | - * add any custom tables, or flush rewrite rules, as required. |
|
35 | - */ |
|
36 | - public function activate(); |
|
29 | + /** |
|
30 | + * Fired on plugin activation. |
|
31 | + * |
|
32 | + * This function is attached to `register_activation_hook` and is fired when the plugin is |
|
33 | + * activated by WordPress. This gives the developer a place to set up any options, |
|
34 | + * add any custom tables, or flush rewrite rules, as required. |
|
35 | + */ |
|
36 | + public function activate(); |
|
37 | 37 | |
38 | - /** |
|
39 | - * Fired on plugin deactivation. |
|
40 | - * |
|
41 | - * This function is attached to `register_deactivation_hook` and is fired when the plugin |
|
42 | - * is deactivated by WordPress. This gives the developer a place to clean up anything left |
|
43 | - * behind by the plugin. |
|
44 | - */ |
|
45 | - public function deactivate(); |
|
38 | + /** |
|
39 | + * Fired on plugin deactivation. |
|
40 | + * |
|
41 | + * This function is attached to `register_deactivation_hook` and is fired when the plugin |
|
42 | + * is deactivated by WordPress. This gives the developer a place to clean up anything left |
|
43 | + * behind by the plugin. |
|
44 | + */ |
|
45 | + public function deactivate(); |
|
46 | 46 | |
47 | - /** |
|
48 | - * Retrieves the booted Application instance. |
|
49 | - * |
|
50 | - * If the Application has not yet been booted, an Exception will be thrown. |
|
51 | - * |
|
52 | - * @return Application |
|
53 | - * @throws ApplicationNotBootedException |
|
54 | - */ |
|
55 | - public static function instance(); |
|
47 | + /** |
|
48 | + * Retrieves the booted Application instance. |
|
49 | + * |
|
50 | + * If the Application has not yet been booted, an Exception will be thrown. |
|
51 | + * |
|
52 | + * @return Application |
|
53 | + * @throws ApplicationNotBootedException |
|
54 | + */ |
|
55 | + public static function instance(); |
|
56 | 56 | |
57 | - /** |
|
58 | - * Shuts down the booted Application. |
|
59 | - * |
|
60 | - * If the Application has already been booted, the Application instance |
|
61 | - * will be destroyed by assigning it a null value, freeing it from memory. |
|
62 | - * However, the service objects will likely remain in memory if they've been |
|
63 | - * attached to hooks when this method is called. This function is primarily |
|
64 | - * for uniting testing to make sure you can boot a new instance for each test. |
|
65 | - */ |
|
66 | - public static function shutdown(); |
|
57 | + /** |
|
58 | + * Shuts down the booted Application. |
|
59 | + * |
|
60 | + * If the Application has already been booted, the Application instance |
|
61 | + * will be destroyed by assigning it a null value, freeing it from memory. |
|
62 | + * However, the service objects will likely remain in memory if they've been |
|
63 | + * attached to hooks when this method is called. This function is primarily |
|
64 | + * for uniting testing to make sure you can boot a new instance for each test. |
|
65 | + */ |
|
66 | + public static function shutdown(); |
|
67 | 67 | } |
@@ -2,13 +2,13 @@ |
||
2 | 2 | namespace Intraxia\Jaxion\Contract\Core; |
3 | 3 | |
4 | 4 | interface ServiceProvider { |
5 | - /** |
|
6 | - * Register the provider's services on the container. |
|
7 | - * |
|
8 | - * This method is passed the container to register on, giving the service provider |
|
9 | - * an opportunity to register its services on the container in an encapsulated way. |
|
10 | - * |
|
11 | - * @param Container $container |
|
12 | - */ |
|
13 | - public function register( Container $container ); |
|
5 | + /** |
|
6 | + * Register the provider's services on the container. |
|
7 | + * |
|
8 | + * This method is passed the container to register on, giving the service provider |
|
9 | + * an opportunity to register its services on the container in an encapsulated way. |
|
10 | + * |
|
11 | + * @param Container $container |
|
12 | + */ |
|
13 | + public function register( Container $container ); |
|
14 | 14 | } |
15 | 15 | \ No newline at end of file |
@@ -2,31 +2,31 @@ |
||
2 | 2 | namespace Intraxia\Jaxion\Contract\Core; |
3 | 3 | |
4 | 4 | interface Loader { |
5 | - /** |
|
6 | - * Register all the actions and filters with WordPress. |
|
7 | - * |
|
8 | - * Loops through all the registered actions/filters and fires add_* for each of |
|
9 | - * them respectively. |
|
10 | - */ |
|
11 | - public function run(); |
|
5 | + /** |
|
6 | + * Register all the actions and filters with WordPress. |
|
7 | + * |
|
8 | + * Loops through all the registered actions/filters and fires add_* for each of |
|
9 | + * them respectively. |
|
10 | + */ |
|
11 | + public function run(); |
|
12 | 12 | |
13 | - /** |
|
14 | - * Registers the service's actions with the loader. |
|
15 | - * |
|
16 | - * Actions retrieved from the service are registered with the Loader. |
|
17 | - * When the loader runs, this actions are registered with WordPress. |
|
18 | - * |
|
19 | - * @param HasActions $service |
|
20 | - */ |
|
21 | - public function register_actions( HasActions $service ); |
|
13 | + /** |
|
14 | + * Registers the service's actions with the loader. |
|
15 | + * |
|
16 | + * Actions retrieved from the service are registered with the Loader. |
|
17 | + * When the loader runs, this actions are registered with WordPress. |
|
18 | + * |
|
19 | + * @param HasActions $service |
|
20 | + */ |
|
21 | + public function register_actions( HasActions $service ); |
|
22 | 22 | |
23 | - /** |
|
24 | - * Registers the service's filters with the loader. |
|
25 | - * |
|
26 | - * Filters retrieved from the service are registered with the Loader. |
|
27 | - * When the loader runs, this actions are registered with WordPress. |
|
28 | - * |
|
29 | - * @param HasFilters $service |
|
30 | - */ |
|
31 | - public function register_filters( HasFilters $service ); |
|
23 | + /** |
|
24 | + * Registers the service's filters with the loader. |
|
25 | + * |
|
26 | + * Filters retrieved from the service are registered with the Loader. |
|
27 | + * When the loader runs, this actions are registered with WordPress. |
|
28 | + * |
|
29 | + * @param HasFilters $service |
|
30 | + */ |
|
31 | + public function register_filters( HasFilters $service ); |
|
32 | 32 | } |