1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\I18n;
16:
17: use Aura\Intl\Package;
18: use RuntimeException;
19:
20: /**
21: * Wraps multiple message loaders calling them one after another until
22: * one of them returns a non-empty package.
23: */
24: class ChainMessagesLoader
25: {
26: /**
27: * The list of callables to execute one after another for loading messages
28: *
29: * @var callable[]
30: */
31: protected $_loaders = [];
32:
33: /**
34: * Receives a list of callable functions or objects that will be executed
35: * one after another until one of them returns a non-empty translations package
36: *
37: * @param callable[] $loaders List of callables to execute
38: */
39: public function __construct(array $loaders)
40: {
41: $this->_loaders = $loaders;
42: }
43:
44: /**
45: * Executes this object returning the translations package as configured in
46: * the chain.
47: *
48: * @return \Aura\Intl\Package
49: * @throws \RuntimeException if any of the loaders in the chain is not a valid callable
50: */
51: public function __invoke()
52: {
53: foreach ($this->_loaders as $k => $loader) {
54: if (!is_callable($loader)) {
55: throw new RuntimeException(sprintf(
56: 'Loader "%s" in the chain is not a valid callable',
57: $k
58: ));
59: }
60:
61: $package = $loader();
62: if (!$package) {
63: continue;
64: }
65:
66: if (!($package instanceof Package)) {
67: throw new RuntimeException(sprintf(
68: 'Loader "%s" in the chain did not return a valid Package object',
69: $k
70: ));
71: }
72:
73: return $package;
74: }
75:
76: return new Package();
77: }
78: }
79: