15 |
16 |
17 |
18 |
19 |
20 | render( $panel_left_outer ) ?>
21 |
22 | render( $panel_details_outer ) ?>
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/src/Flash/Flash.php:
--------------------------------------------------------------------------------
1 |
5 | * @copyright 2017-2019 Atanas Angelov
6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 | * @link https://wpemerge.com/
8 | */
9 |
10 | namespace WPEmerge\Flash;
11 |
12 | use ArrayAccess;
13 | use WPEmerge\Exceptions\ConfigurationException;
14 | use WPEmerge\Helpers\MixedType;
15 | use WPEmerge\Support\Arr;
16 |
17 | /**
18 | * Provide a way to flash data into the session for the next request.
19 | */
20 | class Flash {
21 | /**
22 | * Keys for different request contexts.
23 | */
24 | const CURRENT_KEY = 'current';
25 | const NEXT_KEY = 'next';
26 |
27 | /**
28 | * Key to store flashed data in store with.
29 | *
30 | * @var string
31 | */
32 | protected $store_key = '';
33 |
34 | /**
35 | * Root store array or object implementing ArrayAccess.
36 | *
37 | * @var array|ArrayAccess
38 | */
39 | protected $store = null;
40 |
41 | /**
42 | * Flash store array.
43 | *
44 | * @var array
45 | */
46 | protected $flashed = [];
47 |
48 | /**
49 | * Constructor.
50 | *
51 | * @codeCoverageIgnore
52 | * @param array|ArrayAccess $store
53 | * @param string $store_key
54 | */
55 | public function __construct( &$store, $store_key = '__wpemergeFlash' ) {
56 | $this->store_key = $store_key;
57 | $this->setStore( $store );
58 | }
59 |
60 | /**
61 | * Get whether a store object is valid.
62 | *
63 | * @param mixed $store
64 | * @return boolean
65 | */
66 | protected function isValidStore( $store ) {
67 | return ( is_array( $store ) || $store instanceof ArrayAccess );
68 | }
69 |
70 | /**
71 | * Throw an exception if store is not valid.
72 | *
73 | * @return void
74 | */
75 | protected function validateStore() {
76 | if ( ! $this->isValidStore( $this->store ) ) {
77 | throw new ConfigurationException(
78 | 'Attempted to use Flash without an active session. ' .
79 | 'Did you miss to call session_start()?'
80 | );
81 | }
82 | }
83 |
84 | /**
85 | * Get the store for flash messages.
86 | *
87 | * @return array|ArrayAccess
88 | */
89 | public function getStore() {
90 | return $this->store;
91 | }
92 |
93 | /**
94 | * Set the store for flash messages.
95 | *
96 | * @param array|ArrayAccess $store
97 | * @return void
98 | */
99 | public function setStore( &$store ) {
100 | if ( ! $this->isValidStore( $store ) ) {
101 | return;
102 | }
103 |
104 | $this->store = &$store;
105 |
106 | if ( ! isset( $this->store[ $this->store_key ] ) ) {
107 | $this->store[ $this->store_key ] = [
108 | static::CURRENT_KEY => [],
109 | static::NEXT_KEY => [],
110 | ];
111 | }
112 |
113 | $this->flashed = $store[ $this->store_key ];
114 | }
115 |
116 | /**
117 | * Get whether the flash service is enabled.
118 | *
119 | * @return boolean
120 | */
121 | public function enabled() {
122 | return $this->isValidStore( $this->store );
123 | }
124 |
125 | /**
126 | * Get the entire store or the values for a key for a request.
127 | *
128 | * @param string $request_key
129 | * @param string|null $key
130 | * @param mixed $default
131 | * @return mixed
132 | */
133 | protected function getFromRequest( $request_key, $key = null, $default = [] ) {
134 | $this->validateStore();
135 |
136 | if ( $key === null ) {
137 | return Arr::get( $this->flashed, $request_key, $default );
138 | }
139 |
140 | return Arr::get( $this->flashed[ $request_key ], $key, $default );
141 | }
142 |
143 | /**
144 | * Add values for a key for a request.
145 | *
146 | * @param string $request_key
147 | * @param string $key
148 | * @param mixed $new_items
149 | * @return void
150 | */
151 | protected function addToRequest( $request_key, $key, $new_items ) {
152 | $this->validateStore();
153 |
154 | $new_items = MixedType::toArray( $new_items );
155 | $items = MixedType::toArray( $this->getFromRequest( $request_key, $key, [] ) );
156 | $this->flashed[ $request_key ][ $key ] = array_merge( $items, $new_items );
157 | }
158 |
159 | /**
160 | * Remove all values or values for a key from a request.
161 | *
162 | * @param string $request_key
163 | * @param string|null $key
164 | * @return void
165 | */
166 | protected function clearFromRequest( $request_key, $key = null ) {
167 | $this->validateStore();
168 |
169 | $keys = $key === null ? array_keys( $this->flashed[ $request_key ] ) : [$key];
170 | foreach ( $keys as $k ) {
171 | unset( $this->flashed[ $request_key ][ $k ] );
172 | }
173 | }
174 |
175 | /**
176 | * Add values for a key for the next request.
177 | *
178 | * @param string $key
179 | * @param mixed $new_items
180 | * @return void
181 | */
182 | public function add( $key, $new_items ) {
183 | $this->addToRequest( static::NEXT_KEY, $key, $new_items );
184 | }
185 |
186 | /**
187 | * Add values for a key for the current request.
188 | *
189 | * @param string $key
190 | * @param mixed $new_items
191 | * @return void
192 | */
193 | public function addNow( $key, $new_items ) {
194 | $this->addToRequest( static::CURRENT_KEY, $key, $new_items );
195 | }
196 |
197 | /**
198 | * Get the entire store or the values for a key for the current request.
199 | *
200 | * @param string|null $key
201 | * @param mixed $default
202 | * @return mixed
203 | */
204 | public function get( $key = null, $default = [] ) {
205 | return $this->getFromRequest( static::CURRENT_KEY, $key, $default );
206 | }
207 |
208 | /**
209 | * Get the entire store or the values for a key for the next request.
210 | *
211 | * @param string|null $key
212 | * @param mixed $default
213 | * @return mixed
214 | */
215 | public function getNext( $key = null, $default = [] ) {
216 | return $this->getFromRequest( static::NEXT_KEY, $key, $default );
217 | }
218 |
219 | /**
220 | * Clear the entire store or the values for a key for the current request.
221 | *
222 | * @param string|null $key
223 | * @return void
224 | */
225 | public function clear( $key = null ) {
226 | $this->clearFromRequest( static::CURRENT_KEY, $key );
227 | }
228 |
229 | /**
230 | * Clear the entire store or the values for a key for the next request.
231 | *
232 | * @param string|null $key
233 | * @return void
234 | */
235 | public function clearNext( $key = null ) {
236 | $this->clearFromRequest( static::NEXT_KEY, $key );
237 | }
238 |
239 | /**
240 | * Shift current store and replace it with next store.
241 | *
242 | * @return void
243 | */
244 | public function shift() {
245 | $this->validateStore();
246 |
247 | $this->flashed[ static::CURRENT_KEY ] = $this->flashed[ static::NEXT_KEY ];
248 | $this->flashed[ static::NEXT_KEY ] = [];
249 | }
250 |
251 | /**
252 | * Save flashed data to store.
253 | *
254 | * @return void
255 | */
256 | public function save() {
257 | $this->validateStore();
258 |
259 | $this->store[ $this->store_key ] = $this->flashed;
260 | }
261 | }
262 |
--------------------------------------------------------------------------------
/src/Flash/FlashMiddleware.php:
--------------------------------------------------------------------------------
1 |
5 | * @copyright 2017-2019 Atanas Angelov
6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 | * @link https://wpemerge.com/
8 | */
9 |
10 | namespace WPEmerge\Flash;
11 |
12 | use Closure;
13 | use WPEmerge\Requests\RequestInterface;
14 |
15 | /**
16 | * Store current request data and clear old request data
17 | */
18 | class FlashMiddleware {
19 | /**
20 | * Flash service.
21 | *
22 | * @var Flash
23 | */
24 | protected $flash = null;
25 |
26 | /**
27 | * Constructor.
28 | *
29 | * @codeCoverageIgnore
30 | * @param Flash $flash
31 | */
32 | public function __construct( Flash $flash ) {
33 | $this->flash = $flash;
34 | }
35 |
36 | /**
37 | * {@inheritDoc}
38 | */
39 | public function handle( RequestInterface $request, Closure $next ) {
40 | $response = $next( $request );
41 |
42 | if ( $this->flash->enabled() ) {
43 | $this->flash->shift();
44 | $this->flash->save();
45 | }
46 |
47 | return $response;
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/src/Flash/FlashServiceProvider.php:
--------------------------------------------------------------------------------
1 |
5 | * @copyright 2017-2019 Atanas Angelov
6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 | * @link https://wpemerge.com/
8 | */
9 |
10 | namespace WPEmerge\Flash;
11 |
12 | use WPEmerge\ServiceProviders\ServiceProviderInterface;
13 |
14 | /**
15 | * Provide flash dependencies.
16 | *
17 | * @codeCoverageIgnore
18 | */
19 | class FlashServiceProvider implements ServiceProviderInterface {
20 | /**
21 | * {@inheritDoc}
22 | */
23 | public function register( $container ) {
24 | $container[ WPEMERGE_FLASH_KEY ] = function ( $c ) {
25 | $session = null;
26 | if ( isset( $c[ WPEMERGE_SESSION_KEY ] ) ) {
27 | $session = &$c[ WPEMERGE_SESSION_KEY ];
28 | } else if ( isset( $_SESSION ) ) {
29 | $session = &$_SESSION;
30 | }
31 | return new Flash( $session );
32 | };
33 |
34 | $container[ FlashMiddleware::class ] = function ( $c ) {
35 | return new FlashMiddleware( $c[ WPEMERGE_FLASH_KEY ] );
36 | };
37 |
38 | $app = $container[ WPEMERGE_APPLICATION_KEY ];
39 | $app->alias( 'flash', WPEMERGE_FLASH_KEY );
40 | }
41 |
42 | /**
43 | * {@inheritDoc}
44 | */
45 | public function bootstrap( $container ) {
46 | // Nothing to bootstrap.
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/Helpers/Arguments.php:
--------------------------------------------------------------------------------
1 |
5 | * @copyright 2017-2019 Atanas Angelov
6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 | * @link https://wpemerge.com/
8 | */
9 |
10 | namespace WPEmerge\Helpers;
11 |
12 | /**
13 | * A collection of tools dealing with urls
14 | */
15 | class Arguments {
16 | /**
17 | * Get a closure which will flip preceding optional arguments around.
18 | * @example list( $argument1, $argument2 ) = Arguments::flip( $argument1, $argument2 );
19 | *
20 | * @return array
21 | */
22 | public static function flip() {
23 | $arguments = func_get_args();
24 | $first_null = array_search( null, $arguments, true );
25 |
26 | if ( $first_null === false ) {
27 | return $arguments;
28 | }
29 |
30 | // Support integer keys only.
31 | $first_null = (int) $first_null;
32 |
33 | $arguments = array_values( array_merge(
34 | array_slice( $arguments, $first_null ),
35 | array_slice( $arguments, 0, $first_null )
36 | ) );
37 |
38 | return $arguments;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/Helpers/Handler.php:
--------------------------------------------------------------------------------
1 |
5 | * @copyright 2017-2019 Atanas Angelov
6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 | * @link https://wpemerge.com/
8 | */
9 |
10 | namespace WPEmerge\Helpers;
11 |
12 | use Closure;
13 | use WPEmerge\Application\GenericFactory;
14 | use WPEmerge\Exceptions\ClassNotFoundException;
15 | use WPEmerge\Exceptions\ConfigurationException;
16 | use WPEmerge\Support\Arr;
17 |
18 | /**
19 | * Represent a generic handler - a Closure or a class method to be resolved from the service container
20 | */
21 | class Handler {
22 | /**
23 | * Injection Factory.
24 | *
25 | * @var GenericFactory
26 | */
27 | protected $factory = null;
28 |
29 | /**
30 | * Parsed handler
31 | *
32 | * @var array|Closure
33 | */
34 | protected $handler = null;
35 |
36 | /**
37 | * Constructor
38 | *
39 | * @param GenericFactory $factory
40 | * @param string|array|Closure $raw_handler
41 | * @param string $default_method
42 | * @param string $namespace
43 | */
44 | public function __construct( GenericFactory $factory, $raw_handler, $default_method = '', $namespace = '' ) {
45 | $this->factory = $factory;
46 |
47 | $handler = $this->parse( $raw_handler, $default_method, $namespace );
48 |
49 | if ( $handler === null ) {
50 | throw new ConfigurationException( 'No or invalid handler provided.' );
51 | }
52 |
53 | $this->handler = $handler;
54 | }
55 |
56 | /**
57 | * Parse a raw handler to a Closure or a [class, method, namespace] array
58 | *
59 | * @param string|array|Closure $raw_handler
60 | * @param string $default_method
61 | * @param string $namespace
62 | * @return array|Closure|null
63 | */
64 | protected function parse( $raw_handler, $default_method, $namespace ) {
65 | if ( $raw_handler instanceof Closure ) {
66 | return $raw_handler;
67 | }
68 |
69 | if ( is_array( $raw_handler ) ) {
70 | return $this->parseFromArray( $raw_handler, $default_method, $namespace );
71 | }
72 |
73 | return $this->parseFromString( $raw_handler, $default_method, $namespace );
74 | }
75 |
76 | /**
77 | * Parse a [Class::class, 'method'] array handler to a [class, method, namespace] array
78 | *
79 | * @param array $raw_handler
80 | * @param string $default_method
81 | * @param string $namespace
82 | * @return array|null
83 | */
84 | protected function parseFromArray( $raw_handler, $default_method, $namespace ) {
85 | $class = Arr::get( $raw_handler, 0, '' );
86 | $class = preg_replace( '/^\\\\+/', '', $class );
87 | $method = Arr::get( $raw_handler, 1, $default_method );
88 |
89 | if ( empty( $class ) ) {
90 | return null;
91 | }
92 |
93 | if ( empty( $method ) ) {
94 | return null;
95 | }
96 |
97 | return [
98 | 'class' => $class,
99 | 'method' => $method,
100 | 'namespace' => $namespace,
101 | ];
102 | }
103 |
104 | /**
105 | * Parse a 'Controller@method' or 'Controller::method' string handler to a [class, method, namespace] array
106 | *
107 | * @param string $raw_handler
108 | * @param string $default_method
109 | * @param string $namespace
110 | * @return array|null
111 | */
112 | protected function parseFromString( $raw_handler, $default_method, $namespace ) {
113 | return $this->parseFromArray( preg_split( '/@|::/', $raw_handler, 2 ), $default_method, $namespace );
114 | }
115 |
116 | /**
117 | * Get the parsed handler
118 | *
119 | * @return array|Closure
120 | */
121 | public function get() {
122 | return $this->handler;
123 | }
124 |
125 | /**
126 | * Make an instance of the handler.
127 | *
128 | * @return object
129 | */
130 | public function make() {
131 | $handler = $this->get();
132 |
133 | if ( $handler instanceof Closure ) {
134 | return $handler;
135 | }
136 |
137 | $namespace = $handler['namespace'];
138 | $class = $handler['class'];
139 |
140 | try {
141 | $instance = $this->factory->make( $class );
142 | } catch ( ClassNotFoundException $e ) {
143 | try {
144 | $instance = $this->factory->make( $namespace . $class );
145 | } catch ( ClassNotFoundException $e ) {
146 | throw new ClassNotFoundException( 'Class not found - tried: ' . $class . ', ' . $namespace . $class );
147 | }
148 | }
149 |
150 | return $instance;
151 | }
152 |
153 | /**
154 | * Execute the parsed handler with any provided arguments and return the result.
155 | *
156 | * @param mixed ,...$arguments
157 | * @return mixed
158 | */
159 | public function execute() {
160 | $arguments = func_get_args();
161 | $instance = $this->make();
162 |
163 | if ( $instance instanceof Closure ) {
164 | return call_user_func_array( $instance, $arguments );
165 | }
166 |
167 | return call_user_func_array( [$instance, $this->get()['method']], $arguments );
168 | }
169 | }
170 |
--------------------------------------------------------------------------------
/src/Helpers/HandlerFactory.php:
--------------------------------------------------------------------------------
1 |
5 | * @copyright 2017-2019 Atanas Angelov
6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 | * @link https://wpemerge.com/
8 | */
9 |
10 | namespace WPEmerge\Helpers;
11 |
12 | use Closure;
13 | use WPEmerge\Application\GenericFactory;
14 |
15 | /**
16 | * Handler factory.
17 | */
18 | class HandlerFactory {
19 | /**
20 | * Injection Factory.
21 | *
22 | * @var GenericFactory
23 | */
24 | protected $factory = null;
25 |
26 | /**
27 | * Constructor.
28 | *
29 | * @codeCoverageIgnore
30 | * @param GenericFactory $factory
31 | */
32 | public function __construct( GenericFactory $factory ) {
33 | $this->factory = $factory;
34 | }
35 |
36 | /**
37 | * Make a Handler.
38 | *
39 | * @codeCoverageIgnore
40 | * @param string|Closure $raw_handler
41 | * @param string $default_method
42 | * @param string $namespace
43 | * @return Handler
44 | */
45 | public function make( $raw_handler, $default_method = '', $namespace = '' ) {
46 | return new Handler( $this->factory, $raw_handler, $default_method, $namespace );
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/Helpers/HasAttributesInterface.php:
--------------------------------------------------------------------------------
1 |
5 | * @copyright 2017-2019 Atanas Angelov
6 | * @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7 | * @link https://wpemerge.com/
8 | */
9 |
10 | namespace WPEmerge\Helpers;
11 |
12 | /**
13 | * Represent an object which has an array of attributes.
14 | */
15 | interface HasAttributesInterface {
16 | /**
17 | * Get attribute.
18 | *
19 | * @param string $attribute
20 | * @param mixed $default
21 | * @return mixed
22 | */
23 | public function getAttribute( $attribute, $default = '' );
24 |
25 | /**
26 | * Get all attributes.
27 | *
28 | * @return array