@@ -11,14 +11,14 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | interface CommandBusInterface extends InterminableInterface |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @return CommandHandlerManagerInterface |
|
| 16 | - */ |
|
| 17 | - public function getCommandHandlerManager(); |
|
| 14 | + /** |
|
| 15 | + * @return CommandHandlerManagerInterface |
|
| 16 | + */ |
|
| 17 | + public function getCommandHandlerManager(); |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @param CommandInterface $command |
|
| 21 | - * @return mixed |
|
| 22 | - */ |
|
| 23 | - public function execute($command); |
|
| 19 | + /** |
|
| 20 | + * @param CommandInterface $command |
|
| 21 | + * @return mixed |
|
| 22 | + */ |
|
| 23 | + public function execute($command); |
|
| 24 | 24 | } |
@@ -9,13 +9,13 @@ |
||
| 9 | 9 | |
| 10 | 10 | interface CommandFactoryInterface extends InterminableInterface |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @param string $command_fqcn |
|
| 14 | - * @param array $arguments |
|
| 15 | - * @return mixed |
|
| 16 | - * @throws InvalidArgumentException |
|
| 17 | - * @throws InvalidDataTypeException |
|
| 18 | - * @throws InvalidInterfaceException |
|
| 19 | - */ |
|
| 20 | - public function getNew($command_fqcn, $arguments = array()); |
|
| 12 | + /** |
|
| 13 | + * @param string $command_fqcn |
|
| 14 | + * @param array $arguments |
|
| 15 | + * @return mixed |
|
| 16 | + * @throws InvalidArgumentException |
|
| 17 | + * @throws InvalidDataTypeException |
|
| 18 | + * @throws InvalidInterfaceException |
|
| 19 | + */ |
|
| 20 | + public function getNew($command_fqcn, $arguments = array()); |
|
| 21 | 21 | } |
@@ -11,26 +11,26 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | interface CommandHandlerManagerInterface extends InterminableInterface |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * !!! IMPORTANT !!! |
|
| 16 | - * If overriding the default CommandHandler for a Command, |
|
| 17 | - * be sure to also override CommandHandler::verify(), |
|
| 18 | - * or else an Exception will be thrown when the CommandBus |
|
| 19 | - * attempts to verify that the incoming Command matches the Handler |
|
| 20 | - * |
|
| 21 | - * @param CommandHandlerInterface $command_handler |
|
| 22 | - * @param string $fqcn_for_command Fully Qualified ClassName for Command |
|
| 23 | - * @return void |
|
| 24 | - * @throws InvalidCommandHandlerException |
|
| 25 | - */ |
|
| 26 | - public function addCommandHandler(CommandHandlerInterface $command_handler, $fqcn_for_command = ''); |
|
| 14 | + /** |
|
| 15 | + * !!! IMPORTANT !!! |
|
| 16 | + * If overriding the default CommandHandler for a Command, |
|
| 17 | + * be sure to also override CommandHandler::verify(), |
|
| 18 | + * or else an Exception will be thrown when the CommandBus |
|
| 19 | + * attempts to verify that the incoming Command matches the Handler |
|
| 20 | + * |
|
| 21 | + * @param CommandHandlerInterface $command_handler |
|
| 22 | + * @param string $fqcn_for_command Fully Qualified ClassName for Command |
|
| 23 | + * @return void |
|
| 24 | + * @throws InvalidCommandHandlerException |
|
| 25 | + */ |
|
| 26 | + public function addCommandHandler(CommandHandlerInterface $command_handler, $fqcn_for_command = ''); |
|
| 27 | 27 | |
| 28 | 28 | |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @param CommandInterface $command |
|
| 32 | - * @param CommandBusInterface $command_bus |
|
| 33 | - * @return mixed |
|
| 34 | - */ |
|
| 35 | - public function getCommandHandler(CommandInterface $command, CommandBusInterface $command_bus = null); |
|
| 30 | + /** |
|
| 31 | + * @param CommandInterface $command |
|
| 32 | + * @param CommandBusInterface $command_bus |
|
| 33 | + * @return mixed |
|
| 34 | + */ |
|
| 35 | + public function getCommandHandler(CommandInterface $command, CommandBusInterface $command_bus = null); |
|
| 36 | 36 | } |
@@ -13,10 +13,10 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | interface CommandBusMiddlewareInterface extends InterminableInterface |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * @param CommandInterface $command |
|
| 18 | - * @param Closure $next |
|
| 19 | - * @return mixed |
|
| 20 | - */ |
|
| 21 | - public function handle(CommandInterface $command, Closure $next); |
|
| 16 | + /** |
|
| 17 | + * @param CommandInterface $command |
|
| 18 | + * @param Closure $next |
|
| 19 | + * @return mixed |
|
| 20 | + */ |
|
| 21 | + public function handle(CommandInterface $command, Closure $next); |
|
| 22 | 22 | } |
@@ -49,16 +49,16 @@ discard block |
||
| 49 | 49 | { |
| 50 | 50 | $fqn = $this->getFqn($fqn); |
| 51 | 51 | // have we already seen this FQCN ? |
| 52 | - if (! array_key_exists($fqn, $this->interfaces)) { |
|
| 53 | - $this->interfaces[ $fqn ] = array(); |
|
| 52 | + if ( ! array_key_exists($fqn, $this->interfaces)) { |
|
| 53 | + $this->interfaces[$fqn] = array(); |
|
| 54 | 54 | if (class_exists($fqn)) { |
| 55 | - $this->interfaces[ $fqn ] = class_implements($fqn, false); |
|
| 56 | - $this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false |
|
| 57 | - ? $this->interfaces[ $fqn ] |
|
| 55 | + $this->interfaces[$fqn] = class_implements($fqn, false); |
|
| 56 | + $this->interfaces[$fqn] = $this->interfaces[$fqn] !== false |
|
| 57 | + ? $this->interfaces[$fqn] |
|
| 58 | 58 | : array(); |
| 59 | 59 | } |
| 60 | 60 | } |
| 61 | - return $this->interfaces[ $fqn ]; |
|
| 61 | + return $this->interfaces[$fqn]; |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | |
@@ -93,13 +93,13 @@ discard block |
||
| 93 | 93 | // are we adding an alias for a specific class? |
| 94 | 94 | if ($for_class !== '') { |
| 95 | 95 | // make sure it's set up as an array |
| 96 | - if (! isset($this->aliases[ $for_class ])) { |
|
| 97 | - $this->aliases[ $for_class ] = array(); |
|
| 96 | + if ( ! isset($this->aliases[$for_class])) { |
|
| 97 | + $this->aliases[$for_class] = array(); |
|
| 98 | 98 | } |
| 99 | - $this->aliases[ $for_class ][ $alias ] = $fqn; |
|
| 99 | + $this->aliases[$for_class][$alias] = $fqn; |
|
| 100 | 100 | return; |
| 101 | 101 | } |
| 102 | - $this->aliases[ $alias ] = $fqn; |
|
| 102 | + $this->aliases[$alias] = $fqn; |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | */ |
| 132 | 132 | protected function isDirectAlias(string $fqn = ''): bool |
| 133 | 133 | { |
| 134 | - return isset($this->aliases[ $fqn ]) && ! is_array($this->aliases[ $fqn ]); |
|
| 134 | + return isset($this->aliases[$fqn]) && ! is_array($this->aliases[$fqn]); |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | */ |
| 145 | 145 | protected function isAliasForClass(string $fqn = '', string $for_class = ''): bool |
| 146 | 146 | { |
| 147 | - return $for_class !== '' && isset($this->aliases[ $for_class ][ $fqn ]); |
|
| 147 | + return $for_class !== '' && isset($this->aliases[$for_class][$fqn]); |
|
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | |
@@ -168,11 +168,11 @@ discard block |
||
| 168 | 168 | { |
| 169 | 169 | $alias = $this->getFqn($alias); |
| 170 | 170 | if ($this->isAliasForClass($alias, $for_class)) { |
| 171 | - return $this->getFqnForAlias($this->aliases[ $for_class ][ $alias ], $for_class); |
|
| 171 | + return $this->getFqnForAlias($this->aliases[$for_class][$alias], $for_class); |
|
| 172 | 172 | } |
| 173 | 173 | if ($this->isDirectAlias($alias)) { |
| 174 | 174 | // note: changed '' to $for_class |
| 175 | - return $this->getFqnForAlias($this->aliases[ $alias ], $for_class); |
|
| 175 | + return $this->getFqnForAlias($this->aliases[$alias], $for_class); |
|
| 176 | 176 | } |
| 177 | 177 | return $alias; |
| 178 | 178 | } |
@@ -17,185 +17,185 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class ClassInterfaceCache implements InterminableInterface |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * array of interfaces indexed by FQCNs where values are arrays of interface FQNs |
|
| 22 | - * |
|
| 23 | - * @var string[][] $interfaces |
|
| 24 | - */ |
|
| 25 | - private $interfaces = array(); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @type string[][] $aliases |
|
| 29 | - */ |
|
| 30 | - protected $aliases = array(); |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @return string[][] |
|
| 35 | - */ |
|
| 36 | - public function getAliases() |
|
| 37 | - { |
|
| 38 | - return $this->aliases; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @param string|FullyQualifiedName $fqn |
|
| 43 | - * @return string |
|
| 44 | - */ |
|
| 45 | - public function getFqn($fqn): string |
|
| 46 | - { |
|
| 47 | - $fqn = $fqn instanceof FullyQualifiedName ? $fqn->string() : $fqn; |
|
| 48 | - return ltrim((string) $fqn, '\\'); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param string|FullyQualifiedName $fqn |
|
| 54 | - * @return array |
|
| 55 | - */ |
|
| 56 | - public function getInterfaces($fqn): array |
|
| 57 | - { |
|
| 58 | - $fqn = $this->getFqn($fqn); |
|
| 59 | - // have we already seen this FQCN ? |
|
| 60 | - if (! array_key_exists($fqn, $this->interfaces)) { |
|
| 61 | - $this->interfaces[ $fqn ] = array(); |
|
| 62 | - if (class_exists($fqn)) { |
|
| 63 | - $this->interfaces[ $fqn ] = class_implements($fqn, false); |
|
| 64 | - $this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false |
|
| 65 | - ? $this->interfaces[ $fqn ] |
|
| 66 | - : array(); |
|
| 67 | - } |
|
| 68 | - } |
|
| 69 | - return $this->interfaces[ $fqn ]; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @param string|FullyQualifiedName $fqn |
|
| 75 | - * @param string|FullyQualifiedName $interface |
|
| 76 | - * @return bool |
|
| 77 | - */ |
|
| 78 | - public function hasInterface($fqn, $interface): bool |
|
| 79 | - { |
|
| 80 | - $fqn = $this->getFqn($fqn); |
|
| 81 | - $interfaces = $this->getInterfaces($fqn); |
|
| 82 | - return in_array($interface, $interfaces, true); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * adds an alias for a classname |
|
| 88 | - * |
|
| 89 | - * @param string|FullyQualifiedName $fqn the class name that should be used (concrete class to replace interface) |
|
| 90 | - * @param string|FullyQualifiedName $alias the class name that would be type hinted for (abstract parent or interface) |
|
| 91 | - * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
| 92 | - * @throws InvalidAliasException |
|
| 93 | - */ |
|
| 94 | - public function addAlias($fqn, $alias, string $for_class = '') |
|
| 95 | - { |
|
| 96 | - $fqn = $this->getFqn($fqn); |
|
| 97 | - $alias = $this->getFqn($alias); |
|
| 98 | - if (strpos($alias, '\\') !== false && ! is_subclass_of($fqn, $alias)) { |
|
| 99 | - throw new InvalidAliasException($fqn, $alias); |
|
| 100 | - } |
|
| 101 | - // are we adding an alias for a specific class? |
|
| 102 | - if ($for_class !== '') { |
|
| 103 | - // make sure it's set up as an array |
|
| 104 | - if (! isset($this->aliases[ $for_class ])) { |
|
| 105 | - $this->aliases[ $for_class ] = array(); |
|
| 106 | - } |
|
| 107 | - $this->aliases[ $for_class ][ $alias ] = $fqn; |
|
| 108 | - return; |
|
| 109 | - } |
|
| 110 | - $this->aliases[ $alias ] = $fqn; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * returns TRUE if the provided FQN is an alias |
|
| 116 | - * |
|
| 117 | - * @param string|FullyQualifiedName $fqn |
|
| 118 | - * @param string $for_class |
|
| 119 | - * @return bool |
|
| 120 | - */ |
|
| 121 | - public function isAlias($fqn = '', string $for_class = ''): bool |
|
| 122 | - { |
|
| 123 | - $fqn = $this->getFqn($fqn); |
|
| 124 | - if ($this->isAliasForClass($fqn, $for_class)) { |
|
| 125 | - return true; |
|
| 126 | - } |
|
| 127 | - if ($this->isDirectAlias($fqn)) { |
|
| 128 | - return true; |
|
| 129 | - } |
|
| 130 | - return false; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * returns TRUE if the provided FQN is an alias |
|
| 136 | - * |
|
| 137 | - * @param string $fqn |
|
| 138 | - * @return bool |
|
| 139 | - */ |
|
| 140 | - protected function isDirectAlias(string $fqn = ''): bool |
|
| 141 | - { |
|
| 142 | - return isset($this->aliases[ $fqn ]) && ! is_array($this->aliases[ $fqn ]); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * returns TRUE if the provided FQN is an alias for the specified class |
|
| 148 | - * |
|
| 149 | - * @param string $fqn |
|
| 150 | - * @param string $for_class |
|
| 151 | - * @return bool |
|
| 152 | - */ |
|
| 153 | - protected function isAliasForClass(string $fqn = '', string $for_class = ''): bool |
|
| 154 | - { |
|
| 155 | - return $for_class !== '' && isset($this->aliases[ $for_class ][ $fqn ]); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * returns FQN for provided alias if one exists, otherwise returns the original FQN |
|
| 161 | - * functions recursively, so that multiple aliases can be used to drill down to a FQN |
|
| 162 | - * for example: |
|
| 163 | - * if the following two entries were added to the aliases array: |
|
| 164 | - * array( |
|
| 165 | - * 'interface_alias' => 'some\namespace\interface' |
|
| 166 | - * 'some\namespace\interface' => 'some\namespace\classname' |
|
| 167 | - * ) |
|
| 168 | - * then one could use Loader::getNew( 'interface_alias' ) |
|
| 169 | - * to load an instance of 'some\namespace\classname' |
|
| 170 | - * |
|
| 171 | - * @param string|FullyQualifiedName $alias |
|
| 172 | - * @param string $for_class |
|
| 173 | - * @return string |
|
| 174 | - */ |
|
| 175 | - public function getFqnForAlias($alias = '', string $for_class = ''): string |
|
| 176 | - { |
|
| 177 | - $alias = $this->getFqn($alias); |
|
| 178 | - if ($this->isAliasForClass($alias, $for_class)) { |
|
| 179 | - return $this->getFqnForAlias($this->aliases[ $for_class ][ $alias ], $for_class); |
|
| 180 | - } |
|
| 181 | - if ($this->isDirectAlias($alias)) { |
|
| 182 | - // note: changed '' to $for_class |
|
| 183 | - return $this->getFqnForAlias($this->aliases[ $alias ], $for_class); |
|
| 184 | - } |
|
| 185 | - return $alias; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - |
|
| 189 | - // public function debug($for_class = '') |
|
| 190 | - // { |
|
| 191 | - // if ($for_class !== '') { |
|
| 192 | - // if ( ! isset($this->aliases[ $for_class ])) { |
|
| 193 | - // \EEH_Debug_Tools::printr('NOT FOUND', "aliases[ $for_class ]", __FILE__, __LINE__); |
|
| 194 | - // return; |
|
| 195 | - // } |
|
| 196 | - // \EEH_Debug_Tools::printr($this->aliases[ $for_class ], "aliases[ $for_class ]", __FILE__, __LINE__); |
|
| 197 | - // return; |
|
| 198 | - // } |
|
| 199 | - // \EEH_Debug_Tools::printr($this->aliases, '$this->aliases', __FILE__, __LINE__); |
|
| 200 | - // } |
|
| 20 | + /** |
|
| 21 | + * array of interfaces indexed by FQCNs where values are arrays of interface FQNs |
|
| 22 | + * |
|
| 23 | + * @var string[][] $interfaces |
|
| 24 | + */ |
|
| 25 | + private $interfaces = array(); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @type string[][] $aliases |
|
| 29 | + */ |
|
| 30 | + protected $aliases = array(); |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @return string[][] |
|
| 35 | + */ |
|
| 36 | + public function getAliases() |
|
| 37 | + { |
|
| 38 | + return $this->aliases; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @param string|FullyQualifiedName $fqn |
|
| 43 | + * @return string |
|
| 44 | + */ |
|
| 45 | + public function getFqn($fqn): string |
|
| 46 | + { |
|
| 47 | + $fqn = $fqn instanceof FullyQualifiedName ? $fqn->string() : $fqn; |
|
| 48 | + return ltrim((string) $fqn, '\\'); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param string|FullyQualifiedName $fqn |
|
| 54 | + * @return array |
|
| 55 | + */ |
|
| 56 | + public function getInterfaces($fqn): array |
|
| 57 | + { |
|
| 58 | + $fqn = $this->getFqn($fqn); |
|
| 59 | + // have we already seen this FQCN ? |
|
| 60 | + if (! array_key_exists($fqn, $this->interfaces)) { |
|
| 61 | + $this->interfaces[ $fqn ] = array(); |
|
| 62 | + if (class_exists($fqn)) { |
|
| 63 | + $this->interfaces[ $fqn ] = class_implements($fqn, false); |
|
| 64 | + $this->interfaces[ $fqn ] = $this->interfaces[ $fqn ] !== false |
|
| 65 | + ? $this->interfaces[ $fqn ] |
|
| 66 | + : array(); |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | + return $this->interfaces[ $fqn ]; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @param string|FullyQualifiedName $fqn |
|
| 75 | + * @param string|FullyQualifiedName $interface |
|
| 76 | + * @return bool |
|
| 77 | + */ |
|
| 78 | + public function hasInterface($fqn, $interface): bool |
|
| 79 | + { |
|
| 80 | + $fqn = $this->getFqn($fqn); |
|
| 81 | + $interfaces = $this->getInterfaces($fqn); |
|
| 82 | + return in_array($interface, $interfaces, true); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * adds an alias for a classname |
|
| 88 | + * |
|
| 89 | + * @param string|FullyQualifiedName $fqn the class name that should be used (concrete class to replace interface) |
|
| 90 | + * @param string|FullyQualifiedName $alias the class name that would be type hinted for (abstract parent or interface) |
|
| 91 | + * @param string $for_class the class that has the dependency (is type hinting for the interface) |
|
| 92 | + * @throws InvalidAliasException |
|
| 93 | + */ |
|
| 94 | + public function addAlias($fqn, $alias, string $for_class = '') |
|
| 95 | + { |
|
| 96 | + $fqn = $this->getFqn($fqn); |
|
| 97 | + $alias = $this->getFqn($alias); |
|
| 98 | + if (strpos($alias, '\\') !== false && ! is_subclass_of($fqn, $alias)) { |
|
| 99 | + throw new InvalidAliasException($fqn, $alias); |
|
| 100 | + } |
|
| 101 | + // are we adding an alias for a specific class? |
|
| 102 | + if ($for_class !== '') { |
|
| 103 | + // make sure it's set up as an array |
|
| 104 | + if (! isset($this->aliases[ $for_class ])) { |
|
| 105 | + $this->aliases[ $for_class ] = array(); |
|
| 106 | + } |
|
| 107 | + $this->aliases[ $for_class ][ $alias ] = $fqn; |
|
| 108 | + return; |
|
| 109 | + } |
|
| 110 | + $this->aliases[ $alias ] = $fqn; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * returns TRUE if the provided FQN is an alias |
|
| 116 | + * |
|
| 117 | + * @param string|FullyQualifiedName $fqn |
|
| 118 | + * @param string $for_class |
|
| 119 | + * @return bool |
|
| 120 | + */ |
|
| 121 | + public function isAlias($fqn = '', string $for_class = ''): bool |
|
| 122 | + { |
|
| 123 | + $fqn = $this->getFqn($fqn); |
|
| 124 | + if ($this->isAliasForClass($fqn, $for_class)) { |
|
| 125 | + return true; |
|
| 126 | + } |
|
| 127 | + if ($this->isDirectAlias($fqn)) { |
|
| 128 | + return true; |
|
| 129 | + } |
|
| 130 | + return false; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * returns TRUE if the provided FQN is an alias |
|
| 136 | + * |
|
| 137 | + * @param string $fqn |
|
| 138 | + * @return bool |
|
| 139 | + */ |
|
| 140 | + protected function isDirectAlias(string $fqn = ''): bool |
|
| 141 | + { |
|
| 142 | + return isset($this->aliases[ $fqn ]) && ! is_array($this->aliases[ $fqn ]); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * returns TRUE if the provided FQN is an alias for the specified class |
|
| 148 | + * |
|
| 149 | + * @param string $fqn |
|
| 150 | + * @param string $for_class |
|
| 151 | + * @return bool |
|
| 152 | + */ |
|
| 153 | + protected function isAliasForClass(string $fqn = '', string $for_class = ''): bool |
|
| 154 | + { |
|
| 155 | + return $for_class !== '' && isset($this->aliases[ $for_class ][ $fqn ]); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * returns FQN for provided alias if one exists, otherwise returns the original FQN |
|
| 161 | + * functions recursively, so that multiple aliases can be used to drill down to a FQN |
|
| 162 | + * for example: |
|
| 163 | + * if the following two entries were added to the aliases array: |
|
| 164 | + * array( |
|
| 165 | + * 'interface_alias' => 'some\namespace\interface' |
|
| 166 | + * 'some\namespace\interface' => 'some\namespace\classname' |
|
| 167 | + * ) |
|
| 168 | + * then one could use Loader::getNew( 'interface_alias' ) |
|
| 169 | + * to load an instance of 'some\namespace\classname' |
|
| 170 | + * |
|
| 171 | + * @param string|FullyQualifiedName $alias |
|
| 172 | + * @param string $for_class |
|
| 173 | + * @return string |
|
| 174 | + */ |
|
| 175 | + public function getFqnForAlias($alias = '', string $for_class = ''): string |
|
| 176 | + { |
|
| 177 | + $alias = $this->getFqn($alias); |
|
| 178 | + if ($this->isAliasForClass($alias, $for_class)) { |
|
| 179 | + return $this->getFqnForAlias($this->aliases[ $for_class ][ $alias ], $for_class); |
|
| 180 | + } |
|
| 181 | + if ($this->isDirectAlias($alias)) { |
|
| 182 | + // note: changed '' to $for_class |
|
| 183 | + return $this->getFqnForAlias($this->aliases[ $alias ], $for_class); |
|
| 184 | + } |
|
| 185 | + return $alias; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + |
|
| 189 | + // public function debug($for_class = '') |
|
| 190 | + // { |
|
| 191 | + // if ($for_class !== '') { |
|
| 192 | + // if ( ! isset($this->aliases[ $for_class ])) { |
|
| 193 | + // \EEH_Debug_Tools::printr('NOT FOUND', "aliases[ $for_class ]", __FILE__, __LINE__); |
|
| 194 | + // return; |
|
| 195 | + // } |
|
| 196 | + // \EEH_Debug_Tools::printr($this->aliases[ $for_class ], "aliases[ $for_class ]", __FILE__, __LINE__); |
|
| 197 | + // return; |
|
| 198 | + // } |
|
| 199 | + // \EEH_Debug_Tools::printr($this->aliases, '$this->aliases', __FILE__, __LINE__); |
|
| 200 | + // } |
|
| 201 | 201 | } |
@@ -42,323 +42,323 @@ |
||
| 42 | 42 | */ |
| 43 | 43 | class CollectionDetails implements CollectionDetailsInterface |
| 44 | 44 | { |
| 45 | - /** |
|
| 46 | - * if $identifier_type is set to this, |
|
| 47 | - * then the collection will use each object's spl_object_hash() as it's identifier |
|
| 48 | - */ |
|
| 49 | - const ID_OBJECT_HASH = 'identifier-uses-spl-object-hash'; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * if $identifier_type is set to this, |
|
| 53 | - * then the collection will use each object's class name as it's identifier |
|
| 54 | - */ |
|
| 55 | - const ID_CLASS_NAME = 'identifier-uses-object-class-name'; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * if $identifier_type is set to this, |
|
| 59 | - * then the collection will use the return value from a specified callback method on each object |
|
| 60 | - */ |
|
| 61 | - const ID_CALLBACK_METHOD = 'identifier-uses-callback-method'; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * The interface used for controlling what gets added to the collection |
|
| 65 | - * |
|
| 66 | - * @var string $collection_interface |
|
| 67 | - */ |
|
| 68 | - protected $collection_interface = ''; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * a unique name used to identify the collection in filter names |
|
| 72 | - * supplied value is run through sanitize_title_with_dashes(), |
|
| 73 | - * but then also converts dashes to underscores |
|
| 74 | - * |
|
| 75 | - * @var string $collection_name |
|
| 76 | - */ |
|
| 77 | - protected $collection_name = ''; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * what the collection uses for the object identifier. |
|
| 81 | - * corresponds to one of the class constants above. |
|
| 82 | - * CollectionDetails::ID_OBJECT_HASH will use spl_object_hash( object ) for the identifier |
|
| 83 | - * CollectionDetails::ID_CLASS_NAME will use get_class( object ) for the identifier |
|
| 84 | - * CollectionDetails::ID_CALLBACK_METHOD will use a callback for the identifier |
|
| 85 | - * defaults to using spl_object_hash() so that multiple objects of the same class can be added |
|
| 86 | - * |
|
| 87 | - * @var string $identifier_type |
|
| 88 | - */ |
|
| 89 | - protected $identifier_type = CollectionDetails::ID_OBJECT_HASH; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * the pattern applied to paths when searching for class files to add to the collection |
|
| 93 | - * ie: "My_Awesome_*.class.php" |
|
| 94 | - * defaults to "*.php" |
|
| 95 | - * |
|
| 96 | - * @var string $file_mask |
|
| 97 | - */ |
|
| 98 | - protected $file_mask = ''; |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * if the $identifier_type above is set to CollectionDetails::ID_CALLBACK_METHOD, |
|
| 102 | - * then this specifies the method to use on each entity. |
|
| 103 | - * If the callback method does not exist, then an exception will be thrown |
|
| 104 | - * |
|
| 105 | - * @var string $identifier_callback |
|
| 106 | - */ |
|
| 107 | - protected $identifier_callback = ''; |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * an array of Fully Qualified Class Names |
|
| 111 | - * for example: |
|
| 112 | - * $FQCNs = array( |
|
| 113 | - * '/Fully/Qualified/ClassNameA' |
|
| 114 | - * '/Fully/Qualified/Other/ClassNameB' |
|
| 115 | - * ); |
|
| 116 | - * |
|
| 117 | - * @var array $collection_FQCNs |
|
| 118 | - */ |
|
| 119 | - protected $collection_FQCNs = array(); |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * an array of full server paths to folders containing files to be loaded into collection |
|
| 123 | - * for example: |
|
| 124 | - * $paths = array( |
|
| 125 | - * '/full/server/path/to/ClassNameA.ext.php' // for class ClassNameA |
|
| 126 | - * '/full/server/path/to/other/ClassNameB.php' // for class ClassNameB |
|
| 127 | - * ); |
|
| 128 | - * |
|
| 129 | - * @var array $collection_paths |
|
| 130 | - */ |
|
| 131 | - protected $collection_paths = array(); |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * @var LocatorInterface $file_locator |
|
| 135 | - */ |
|
| 136 | - protected $file_locator; |
|
| 137 | - |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * CollectionDetails constructor. |
|
| 141 | - * |
|
| 142 | - * @param string $collection_name |
|
| 143 | - * @param string $collection_interface |
|
| 144 | - * @param array $collection_FQCNs |
|
| 145 | - * @param array $collection_paths |
|
| 146 | - * @param string $file_mask |
|
| 147 | - * @param string $identifier_type |
|
| 148 | - * @param string $identifier_callback |
|
| 149 | - * @param LocatorInterface|null $file_locator |
|
| 150 | - * @throws CollectionDetailsException |
|
| 151 | - */ |
|
| 152 | - public function __construct( |
|
| 153 | - string $collection_name, |
|
| 154 | - string $collection_interface, |
|
| 155 | - array $collection_FQCNs = array(), |
|
| 156 | - array $collection_paths = array(), |
|
| 157 | - string $file_mask = '', |
|
| 158 | - string $identifier_type = CollectionDetails::ID_OBJECT_HASH, |
|
| 159 | - string $identifier_callback = '', |
|
| 160 | - LocatorInterface $file_locator = null |
|
| 161 | - ) { |
|
| 162 | - try { |
|
| 163 | - $this->setCollectionName($collection_name); |
|
| 164 | - $this->setCollectionInterface($collection_interface); |
|
| 165 | - $this->setCollectionFQCNs($collection_FQCNs); |
|
| 166 | - $this->setCollectionPaths($collection_paths); |
|
| 167 | - $this->setFileMasks($file_mask); |
|
| 168 | - $this->setIdentifierType($identifier_type); |
|
| 169 | - $this->setIdentifierCallback($identifier_callback); |
|
| 170 | - $this->file_locator = $file_locator; |
|
| 171 | - } catch (Exception $exception) { |
|
| 172 | - throw new CollectionDetailsException($exception); |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * @return string |
|
| 179 | - */ |
|
| 180 | - public function getCollectionInterface(): string |
|
| 181 | - { |
|
| 182 | - return $this->collection_interface; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * @param string $collection_interface |
|
| 188 | - * @throws InvalidInterfaceException |
|
| 189 | - */ |
|
| 190 | - protected function setCollectionInterface(string $collection_interface) |
|
| 191 | - { |
|
| 192 | - if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
| 193 | - throw new InvalidInterfaceException($collection_interface); |
|
| 194 | - } |
|
| 195 | - $this->collection_interface = $collection_interface; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * the collection name will be used for creating dynamic filters |
|
| 201 | - * |
|
| 202 | - * @return string |
|
| 203 | - */ |
|
| 204 | - public function collectionName(): string |
|
| 205 | - { |
|
| 206 | - return $this->collection_name; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * sanitizes collection name and converts spaces and dashes to underscores |
|
| 212 | - * |
|
| 213 | - * @param string $collection_name |
|
| 214 | - */ |
|
| 215 | - protected function setCollectionName(string $collection_name) |
|
| 216 | - { |
|
| 217 | - $this->collection_name = str_replace( |
|
| 218 | - '-', |
|
| 219 | - '_', |
|
| 220 | - sanitize_title_with_dashes($collection_name, '', 'save') |
|
| 221 | - ); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * @return string |
|
| 227 | - */ |
|
| 228 | - public function identifierType(): string |
|
| 229 | - { |
|
| 230 | - return $this->identifier_type; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * @param string $identifier_type |
|
| 236 | - * @throws InvalidIdentifierException |
|
| 237 | - */ |
|
| 238 | - protected function setIdentifierType(string $identifier_type) |
|
| 239 | - { |
|
| 240 | - if ( |
|
| 241 | - ! ( |
|
| 242 | - $identifier_type === CollectionDetails::ID_CLASS_NAME |
|
| 243 | - || $identifier_type === CollectionDetails::ID_OBJECT_HASH |
|
| 244 | - || $identifier_type === CollectionDetails::ID_CALLBACK_METHOD |
|
| 245 | - ) |
|
| 246 | - ) { |
|
| 247 | - throw new InvalidIdentifierException( |
|
| 248 | - $identifier_type, |
|
| 249 | - 'CollectionDetails::ID_CLASS_NAME or CollectionDetails::ID_OBJECT_HASH or CollectionDetails::ID_CALLBACK_METHOD' |
|
| 250 | - ); |
|
| 251 | - } |
|
| 252 | - $this->identifier_type = $identifier_type; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * @return string |
|
| 258 | - */ |
|
| 259 | - public function identifierCallback(): string |
|
| 260 | - { |
|
| 261 | - return $this->identifier_callback; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - |
|
| 265 | - /** |
|
| 266 | - * @param string $identifier_callback |
|
| 267 | - */ |
|
| 268 | - protected function setIdentifierCallback(string $identifier_callback = 'identifier') |
|
| 269 | - { |
|
| 270 | - $this->identifier_callback = $identifier_callback; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * @return string |
|
| 276 | - */ |
|
| 277 | - public function getFileMask(): string |
|
| 278 | - { |
|
| 279 | - return $this->file_mask; |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * sets the file mask which is then used to filter what files get loaded |
|
| 285 | - * when searching for classes to add to the collection. Defaults to '*.php' |
|
| 286 | - * |
|
| 287 | - * @param string $file_mask |
|
| 288 | - */ |
|
| 289 | - protected function setFileMasks(string $file_mask) |
|
| 290 | - { |
|
| 291 | - $this->file_mask = ! empty($file_mask) ? $file_mask : '*.php'; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * @return array |
|
| 297 | - */ |
|
| 298 | - public function getCollectionFQCNs(): array |
|
| 299 | - { |
|
| 300 | - return $this->collection_FQCNs; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * @param string|array $collection_FQCNs |
|
| 306 | - * @throws InvalidClassException |
|
| 307 | - */ |
|
| 308 | - public function setCollectionFQCNs($collection_FQCNs) |
|
| 309 | - { |
|
| 310 | - foreach ((array) $collection_FQCNs as $collection_FQCN) { |
|
| 311 | - if (! empty($collection_FQCN)) { |
|
| 312 | - if (class_exists($collection_FQCN)) { |
|
| 313 | - $this->collection_FQCNs[] = $collection_FQCN; |
|
| 314 | - } else { |
|
| 315 | - $FQCNs = $this->getFQCNsFromPartialNamespace($collection_FQCN); |
|
| 316 | - foreach ($FQCNs as $FQCN) { |
|
| 317 | - $this->collection_FQCNs[] = $FQCN; |
|
| 318 | - } |
|
| 319 | - } |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - |
|
| 325 | - /** |
|
| 326 | - * @param string $partial_FQCN |
|
| 327 | - * @return array |
|
| 328 | - * @throws InvalidClassException |
|
| 329 | - */ |
|
| 330 | - protected function getFQCNsFromPartialNamespace(string $partial_FQCN): array |
|
| 331 | - { |
|
| 332 | - if (! $this->file_locator instanceof FqcnLocator) { |
|
| 333 | - $this->file_locator = new FqcnLocator(); |
|
| 334 | - } |
|
| 335 | - $this->file_locator->locate($partial_FQCN); |
|
| 336 | - return $this->file_locator->getFQCNs(); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * @return array |
|
| 342 | - */ |
|
| 343 | - public function getCollectionPaths(): array |
|
| 344 | - { |
|
| 345 | - return $this->collection_paths; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * @param string|array $collection_paths |
|
| 351 | - * @throws InvalidFilePathException |
|
| 352 | - */ |
|
| 353 | - public function setCollectionPaths($collection_paths) |
|
| 354 | - { |
|
| 355 | - foreach ((array) $collection_paths as $collection_path) { |
|
| 356 | - if (! empty($collection_path)) { |
|
| 357 | - if (! is_readable($collection_path)) { |
|
| 358 | - throw new InvalidFilePathException($collection_path); |
|
| 359 | - } |
|
| 360 | - $this->collection_paths[] = $collection_path; |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - } |
|
| 45 | + /** |
|
| 46 | + * if $identifier_type is set to this, |
|
| 47 | + * then the collection will use each object's spl_object_hash() as it's identifier |
|
| 48 | + */ |
|
| 49 | + const ID_OBJECT_HASH = 'identifier-uses-spl-object-hash'; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * if $identifier_type is set to this, |
|
| 53 | + * then the collection will use each object's class name as it's identifier |
|
| 54 | + */ |
|
| 55 | + const ID_CLASS_NAME = 'identifier-uses-object-class-name'; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * if $identifier_type is set to this, |
|
| 59 | + * then the collection will use the return value from a specified callback method on each object |
|
| 60 | + */ |
|
| 61 | + const ID_CALLBACK_METHOD = 'identifier-uses-callback-method'; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * The interface used for controlling what gets added to the collection |
|
| 65 | + * |
|
| 66 | + * @var string $collection_interface |
|
| 67 | + */ |
|
| 68 | + protected $collection_interface = ''; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * a unique name used to identify the collection in filter names |
|
| 72 | + * supplied value is run through sanitize_title_with_dashes(), |
|
| 73 | + * but then also converts dashes to underscores |
|
| 74 | + * |
|
| 75 | + * @var string $collection_name |
|
| 76 | + */ |
|
| 77 | + protected $collection_name = ''; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * what the collection uses for the object identifier. |
|
| 81 | + * corresponds to one of the class constants above. |
|
| 82 | + * CollectionDetails::ID_OBJECT_HASH will use spl_object_hash( object ) for the identifier |
|
| 83 | + * CollectionDetails::ID_CLASS_NAME will use get_class( object ) for the identifier |
|
| 84 | + * CollectionDetails::ID_CALLBACK_METHOD will use a callback for the identifier |
|
| 85 | + * defaults to using spl_object_hash() so that multiple objects of the same class can be added |
|
| 86 | + * |
|
| 87 | + * @var string $identifier_type |
|
| 88 | + */ |
|
| 89 | + protected $identifier_type = CollectionDetails::ID_OBJECT_HASH; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * the pattern applied to paths when searching for class files to add to the collection |
|
| 93 | + * ie: "My_Awesome_*.class.php" |
|
| 94 | + * defaults to "*.php" |
|
| 95 | + * |
|
| 96 | + * @var string $file_mask |
|
| 97 | + */ |
|
| 98 | + protected $file_mask = ''; |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * if the $identifier_type above is set to CollectionDetails::ID_CALLBACK_METHOD, |
|
| 102 | + * then this specifies the method to use on each entity. |
|
| 103 | + * If the callback method does not exist, then an exception will be thrown |
|
| 104 | + * |
|
| 105 | + * @var string $identifier_callback |
|
| 106 | + */ |
|
| 107 | + protected $identifier_callback = ''; |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * an array of Fully Qualified Class Names |
|
| 111 | + * for example: |
|
| 112 | + * $FQCNs = array( |
|
| 113 | + * '/Fully/Qualified/ClassNameA' |
|
| 114 | + * '/Fully/Qualified/Other/ClassNameB' |
|
| 115 | + * ); |
|
| 116 | + * |
|
| 117 | + * @var array $collection_FQCNs |
|
| 118 | + */ |
|
| 119 | + protected $collection_FQCNs = array(); |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * an array of full server paths to folders containing files to be loaded into collection |
|
| 123 | + * for example: |
|
| 124 | + * $paths = array( |
|
| 125 | + * '/full/server/path/to/ClassNameA.ext.php' // for class ClassNameA |
|
| 126 | + * '/full/server/path/to/other/ClassNameB.php' // for class ClassNameB |
|
| 127 | + * ); |
|
| 128 | + * |
|
| 129 | + * @var array $collection_paths |
|
| 130 | + */ |
|
| 131 | + protected $collection_paths = array(); |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * @var LocatorInterface $file_locator |
|
| 135 | + */ |
|
| 136 | + protected $file_locator; |
|
| 137 | + |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * CollectionDetails constructor. |
|
| 141 | + * |
|
| 142 | + * @param string $collection_name |
|
| 143 | + * @param string $collection_interface |
|
| 144 | + * @param array $collection_FQCNs |
|
| 145 | + * @param array $collection_paths |
|
| 146 | + * @param string $file_mask |
|
| 147 | + * @param string $identifier_type |
|
| 148 | + * @param string $identifier_callback |
|
| 149 | + * @param LocatorInterface|null $file_locator |
|
| 150 | + * @throws CollectionDetailsException |
|
| 151 | + */ |
|
| 152 | + public function __construct( |
|
| 153 | + string $collection_name, |
|
| 154 | + string $collection_interface, |
|
| 155 | + array $collection_FQCNs = array(), |
|
| 156 | + array $collection_paths = array(), |
|
| 157 | + string $file_mask = '', |
|
| 158 | + string $identifier_type = CollectionDetails::ID_OBJECT_HASH, |
|
| 159 | + string $identifier_callback = '', |
|
| 160 | + LocatorInterface $file_locator = null |
|
| 161 | + ) { |
|
| 162 | + try { |
|
| 163 | + $this->setCollectionName($collection_name); |
|
| 164 | + $this->setCollectionInterface($collection_interface); |
|
| 165 | + $this->setCollectionFQCNs($collection_FQCNs); |
|
| 166 | + $this->setCollectionPaths($collection_paths); |
|
| 167 | + $this->setFileMasks($file_mask); |
|
| 168 | + $this->setIdentifierType($identifier_type); |
|
| 169 | + $this->setIdentifierCallback($identifier_callback); |
|
| 170 | + $this->file_locator = $file_locator; |
|
| 171 | + } catch (Exception $exception) { |
|
| 172 | + throw new CollectionDetailsException($exception); |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * @return string |
|
| 179 | + */ |
|
| 180 | + public function getCollectionInterface(): string |
|
| 181 | + { |
|
| 182 | + return $this->collection_interface; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * @param string $collection_interface |
|
| 188 | + * @throws InvalidInterfaceException |
|
| 189 | + */ |
|
| 190 | + protected function setCollectionInterface(string $collection_interface) |
|
| 191 | + { |
|
| 192 | + if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
| 193 | + throw new InvalidInterfaceException($collection_interface); |
|
| 194 | + } |
|
| 195 | + $this->collection_interface = $collection_interface; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * the collection name will be used for creating dynamic filters |
|
| 201 | + * |
|
| 202 | + * @return string |
|
| 203 | + */ |
|
| 204 | + public function collectionName(): string |
|
| 205 | + { |
|
| 206 | + return $this->collection_name; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * sanitizes collection name and converts spaces and dashes to underscores |
|
| 212 | + * |
|
| 213 | + * @param string $collection_name |
|
| 214 | + */ |
|
| 215 | + protected function setCollectionName(string $collection_name) |
|
| 216 | + { |
|
| 217 | + $this->collection_name = str_replace( |
|
| 218 | + '-', |
|
| 219 | + '_', |
|
| 220 | + sanitize_title_with_dashes($collection_name, '', 'save') |
|
| 221 | + ); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @return string |
|
| 227 | + */ |
|
| 228 | + public function identifierType(): string |
|
| 229 | + { |
|
| 230 | + return $this->identifier_type; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * @param string $identifier_type |
|
| 236 | + * @throws InvalidIdentifierException |
|
| 237 | + */ |
|
| 238 | + protected function setIdentifierType(string $identifier_type) |
|
| 239 | + { |
|
| 240 | + if ( |
|
| 241 | + ! ( |
|
| 242 | + $identifier_type === CollectionDetails::ID_CLASS_NAME |
|
| 243 | + || $identifier_type === CollectionDetails::ID_OBJECT_HASH |
|
| 244 | + || $identifier_type === CollectionDetails::ID_CALLBACK_METHOD |
|
| 245 | + ) |
|
| 246 | + ) { |
|
| 247 | + throw new InvalidIdentifierException( |
|
| 248 | + $identifier_type, |
|
| 249 | + 'CollectionDetails::ID_CLASS_NAME or CollectionDetails::ID_OBJECT_HASH or CollectionDetails::ID_CALLBACK_METHOD' |
|
| 250 | + ); |
|
| 251 | + } |
|
| 252 | + $this->identifier_type = $identifier_type; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * @return string |
|
| 258 | + */ |
|
| 259 | + public function identifierCallback(): string |
|
| 260 | + { |
|
| 261 | + return $this->identifier_callback; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + |
|
| 265 | + /** |
|
| 266 | + * @param string $identifier_callback |
|
| 267 | + */ |
|
| 268 | + protected function setIdentifierCallback(string $identifier_callback = 'identifier') |
|
| 269 | + { |
|
| 270 | + $this->identifier_callback = $identifier_callback; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * @return string |
|
| 276 | + */ |
|
| 277 | + public function getFileMask(): string |
|
| 278 | + { |
|
| 279 | + return $this->file_mask; |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * sets the file mask which is then used to filter what files get loaded |
|
| 285 | + * when searching for classes to add to the collection. Defaults to '*.php' |
|
| 286 | + * |
|
| 287 | + * @param string $file_mask |
|
| 288 | + */ |
|
| 289 | + protected function setFileMasks(string $file_mask) |
|
| 290 | + { |
|
| 291 | + $this->file_mask = ! empty($file_mask) ? $file_mask : '*.php'; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * @return array |
|
| 297 | + */ |
|
| 298 | + public function getCollectionFQCNs(): array |
|
| 299 | + { |
|
| 300 | + return $this->collection_FQCNs; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * @param string|array $collection_FQCNs |
|
| 306 | + * @throws InvalidClassException |
|
| 307 | + */ |
|
| 308 | + public function setCollectionFQCNs($collection_FQCNs) |
|
| 309 | + { |
|
| 310 | + foreach ((array) $collection_FQCNs as $collection_FQCN) { |
|
| 311 | + if (! empty($collection_FQCN)) { |
|
| 312 | + if (class_exists($collection_FQCN)) { |
|
| 313 | + $this->collection_FQCNs[] = $collection_FQCN; |
|
| 314 | + } else { |
|
| 315 | + $FQCNs = $this->getFQCNsFromPartialNamespace($collection_FQCN); |
|
| 316 | + foreach ($FQCNs as $FQCN) { |
|
| 317 | + $this->collection_FQCNs[] = $FQCN; |
|
| 318 | + } |
|
| 319 | + } |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + |
|
| 325 | + /** |
|
| 326 | + * @param string $partial_FQCN |
|
| 327 | + * @return array |
|
| 328 | + * @throws InvalidClassException |
|
| 329 | + */ |
|
| 330 | + protected function getFQCNsFromPartialNamespace(string $partial_FQCN): array |
|
| 331 | + { |
|
| 332 | + if (! $this->file_locator instanceof FqcnLocator) { |
|
| 333 | + $this->file_locator = new FqcnLocator(); |
|
| 334 | + } |
|
| 335 | + $this->file_locator->locate($partial_FQCN); |
|
| 336 | + return $this->file_locator->getFQCNs(); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * @return array |
|
| 342 | + */ |
|
| 343 | + public function getCollectionPaths(): array |
|
| 344 | + { |
|
| 345 | + return $this->collection_paths; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * @param string|array $collection_paths |
|
| 351 | + * @throws InvalidFilePathException |
|
| 352 | + */ |
|
| 353 | + public function setCollectionPaths($collection_paths) |
|
| 354 | + { |
|
| 355 | + foreach ((array) $collection_paths as $collection_path) { |
|
| 356 | + if (! empty($collection_path)) { |
|
| 357 | + if (! is_readable($collection_path)) { |
|
| 358 | + throw new InvalidFilePathException($collection_path); |
|
| 359 | + } |
|
| 360 | + $this->collection_paths[] = $collection_path; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + } |
|
| 364 | 364 | } |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | */ |
| 190 | 190 | protected function setCollectionInterface(string $collection_interface) |
| 191 | 191 | { |
| 192 | - if (! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
| 192 | + if ( ! (interface_exists($collection_interface) || class_exists($collection_interface))) { |
|
| 193 | 193 | throw new InvalidInterfaceException($collection_interface); |
| 194 | 194 | } |
| 195 | 195 | $this->collection_interface = $collection_interface; |
@@ -308,7 +308,7 @@ discard block |
||
| 308 | 308 | public function setCollectionFQCNs($collection_FQCNs) |
| 309 | 309 | { |
| 310 | 310 | foreach ((array) $collection_FQCNs as $collection_FQCN) { |
| 311 | - if (! empty($collection_FQCN)) { |
|
| 311 | + if ( ! empty($collection_FQCN)) { |
|
| 312 | 312 | if (class_exists($collection_FQCN)) { |
| 313 | 313 | $this->collection_FQCNs[] = $collection_FQCN; |
| 314 | 314 | } else { |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | */ |
| 330 | 330 | protected function getFQCNsFromPartialNamespace(string $partial_FQCN): array |
| 331 | 331 | { |
| 332 | - if (! $this->file_locator instanceof FqcnLocator) { |
|
| 332 | + if ( ! $this->file_locator instanceof FqcnLocator) { |
|
| 333 | 333 | $this->file_locator = new FqcnLocator(); |
| 334 | 334 | } |
| 335 | 335 | $this->file_locator->locate($partial_FQCN); |
@@ -353,8 +353,8 @@ discard block |
||
| 353 | 353 | public function setCollectionPaths($collection_paths) |
| 354 | 354 | { |
| 355 | 355 | foreach ((array) $collection_paths as $collection_path) { |
| 356 | - if (! empty($collection_path)) { |
|
| 357 | - if (! is_readable($collection_path)) { |
|
| 356 | + if ( ! empty($collection_path)) { |
|
| 357 | + if ( ! is_readable($collection_path)) { |
|
| 358 | 358 | throw new InvalidFilePathException($collection_path); |
| 359 | 359 | } |
| 360 | 360 | $this->collection_paths[] = $collection_path; |
@@ -14,37 +14,37 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class LooseCollection extends Collection |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * setCollectionInterface |
|
| 19 | - * |
|
| 20 | - * @access protected |
|
| 21 | - * @param string $collection_interface |
|
| 22 | - */ |
|
| 23 | - protected function setCollectionInterface(string $collection_interface) |
|
| 24 | - { |
|
| 25 | - $this->collection_interface = ''; |
|
| 26 | - } |
|
| 17 | + /** |
|
| 18 | + * setCollectionInterface |
|
| 19 | + * |
|
| 20 | + * @access protected |
|
| 21 | + * @param string $collection_interface |
|
| 22 | + */ |
|
| 23 | + protected function setCollectionInterface(string $collection_interface) |
|
| 24 | + { |
|
| 25 | + $this->collection_interface = ''; |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * add |
|
| 31 | - * attaches an object to the Collection |
|
| 32 | - * and sets any supplied data associated with the current iterator entry |
|
| 33 | - * by calling EE_Object_Collection::set_identifier() |
|
| 34 | - * |
|
| 35 | - * @access public |
|
| 36 | - * @param mixed $object |
|
| 37 | - * @param mixed $identifier |
|
| 38 | - * @return bool |
|
| 39 | - * @throws InvalidEntityException |
|
| 40 | - */ |
|
| 41 | - public function add($object, $identifier = null): bool |
|
| 42 | - { |
|
| 43 | - if (! is_object($object)) { |
|
| 44 | - throw new InvalidEntityException($object, 'object'); |
|
| 45 | - } |
|
| 46 | - $identifier = $this->getIdentifier($object, $identifier); |
|
| 47 | - parent::attach($object, $identifier); |
|
| 48 | - return $this->contains($object); |
|
| 49 | - } |
|
| 29 | + /** |
|
| 30 | + * add |
|
| 31 | + * attaches an object to the Collection |
|
| 32 | + * and sets any supplied data associated with the current iterator entry |
|
| 33 | + * by calling EE_Object_Collection::set_identifier() |
|
| 34 | + * |
|
| 35 | + * @access public |
|
| 36 | + * @param mixed $object |
|
| 37 | + * @param mixed $identifier |
|
| 38 | + * @return bool |
|
| 39 | + * @throws InvalidEntityException |
|
| 40 | + */ |
|
| 41 | + public function add($object, $identifier = null): bool |
|
| 42 | + { |
|
| 43 | + if (! is_object($object)) { |
|
| 44 | + throw new InvalidEntityException($object, 'object'); |
|
| 45 | + } |
|
| 46 | + $identifier = $this->getIdentifier($object, $identifier); |
|
| 47 | + parent::attach($object, $identifier); |
|
| 48 | + return $this->contains($object); |
|
| 49 | + } |
|
| 50 | 50 | } |
@@ -40,7 +40,7 @@ |
||
| 40 | 40 | */ |
| 41 | 41 | public function add($object, $identifier = null): bool |
| 42 | 42 | { |
| 43 | - if (! is_object($object)) { |
|
| 43 | + if ( ! is_object($object)) { |
|
| 44 | 44 | throw new InvalidEntityException($object, 'object'); |
| 45 | 45 | } |
| 46 | 46 | $identifier = $this->getIdentifier($object, $identifier); |
@@ -28,295 +28,295 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | class CollectionLoader |
| 30 | 30 | { |
| 31 | - /** |
|
| 32 | - * possible return value when adding entities to a collection. |
|
| 33 | - * denotes that the entity was NOT ADDED to the collection |
|
| 34 | - */ |
|
| 35 | - const ENTITY_NOT_ADDED = 'entity-not-added-to-collection'; |
|
| 31 | + /** |
|
| 32 | + * possible return value when adding entities to a collection. |
|
| 33 | + * denotes that the entity was NOT ADDED to the collection |
|
| 34 | + */ |
|
| 35 | + const ENTITY_NOT_ADDED = 'entity-not-added-to-collection'; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * possible return value when adding entities to a collection. |
|
| 39 | - * denotes that the entity was SUCCESSFULLY ADDED to the collection |
|
| 40 | - */ |
|
| 41 | - const ENTITY_ADDED = 'entity-added-to-collection'; |
|
| 37 | + /** |
|
| 38 | + * possible return value when adding entities to a collection. |
|
| 39 | + * denotes that the entity was SUCCESSFULLY ADDED to the collection |
|
| 40 | + */ |
|
| 41 | + const ENTITY_ADDED = 'entity-added-to-collection'; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * possible return value when adding entities to a collection. |
|
| 45 | - * denotes that the entity was ALREADY ADDED to the collection, |
|
| 46 | - * and therefore could not be added again. |
|
| 47 | - */ |
|
| 48 | - const ENTITY_EXISTS = 'entity-already-in-collection'; |
|
| 43 | + /** |
|
| 44 | + * possible return value when adding entities to a collection. |
|
| 45 | + * denotes that the entity was ALREADY ADDED to the collection, |
|
| 46 | + * and therefore could not be added again. |
|
| 47 | + */ |
|
| 48 | + const ENTITY_EXISTS = 'entity-already-in-collection'; |
|
| 49 | 49 | |
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @var CollectionDetailsInterface $collection_details |
|
| 53 | - */ |
|
| 54 | - protected $collection_details; |
|
| 51 | + /** |
|
| 52 | + * @var CollectionDetailsInterface $collection_details |
|
| 53 | + */ |
|
| 54 | + protected $collection_details; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @var CollectionInterface $collection |
|
| 58 | - */ |
|
| 59 | - protected $collection; |
|
| 56 | + /** |
|
| 57 | + * @var CollectionInterface $collection |
|
| 58 | + */ |
|
| 59 | + protected $collection; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @var FactoryInterface $entity_factory |
|
| 63 | - */ |
|
| 64 | - protected $entity_factory; |
|
| 61 | + /** |
|
| 62 | + * @var FactoryInterface $entity_factory |
|
| 63 | + */ |
|
| 64 | + protected $entity_factory; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * @var FileLocator $file_locator |
|
| 68 | - */ |
|
| 69 | - protected $file_locator; |
|
| 66 | + /** |
|
| 67 | + * @var FileLocator $file_locator |
|
| 68 | + */ |
|
| 69 | + protected $file_locator; |
|
| 70 | 70 | |
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * CollectionLoader constructor. |
|
| 74 | - * |
|
| 75 | - * @param CollectionDetailsInterface $collection_details |
|
| 76 | - * @param CollectionInterface|null $collection |
|
| 77 | - * @param LocatorInterface|null $file_locator |
|
| 78 | - * @param FactoryInterface|null $entity_factory |
|
| 79 | - * @throws CollectionLoaderException |
|
| 80 | - */ |
|
| 81 | - public function __construct( |
|
| 82 | - CollectionDetailsInterface $collection_details, |
|
| 83 | - CollectionInterface $collection = null, |
|
| 84 | - LocatorInterface $file_locator = null, |
|
| 85 | - FactoryInterface $entity_factory = null |
|
| 86 | - ) { |
|
| 87 | - try { |
|
| 88 | - $this->collection_details = $collection_details; |
|
| 89 | - if (! $collection instanceof CollectionInterface) { |
|
| 90 | - $collection = new Collection($this->collection_details->getCollectionInterface()); |
|
| 91 | - } |
|
| 92 | - $this->collection = $collection; |
|
| 93 | - $this->file_locator = $file_locator; |
|
| 94 | - $this->entity_factory = $entity_factory; |
|
| 95 | - $this->loadAllFromFilepaths(); |
|
| 96 | - $this->loadFromFQCNs(); |
|
| 97 | - } catch (Exception $exception) { |
|
| 98 | - throw new CollectionLoaderException($exception); |
|
| 99 | - } |
|
| 100 | - } |
|
| 72 | + /** |
|
| 73 | + * CollectionLoader constructor. |
|
| 74 | + * |
|
| 75 | + * @param CollectionDetailsInterface $collection_details |
|
| 76 | + * @param CollectionInterface|null $collection |
|
| 77 | + * @param LocatorInterface|null $file_locator |
|
| 78 | + * @param FactoryInterface|null $entity_factory |
|
| 79 | + * @throws CollectionLoaderException |
|
| 80 | + */ |
|
| 81 | + public function __construct( |
|
| 82 | + CollectionDetailsInterface $collection_details, |
|
| 83 | + CollectionInterface $collection = null, |
|
| 84 | + LocatorInterface $file_locator = null, |
|
| 85 | + FactoryInterface $entity_factory = null |
|
| 86 | + ) { |
|
| 87 | + try { |
|
| 88 | + $this->collection_details = $collection_details; |
|
| 89 | + if (! $collection instanceof CollectionInterface) { |
|
| 90 | + $collection = new Collection($this->collection_details->getCollectionInterface()); |
|
| 91 | + } |
|
| 92 | + $this->collection = $collection; |
|
| 93 | + $this->file_locator = $file_locator; |
|
| 94 | + $this->entity_factory = $entity_factory; |
|
| 95 | + $this->loadAllFromFilepaths(); |
|
| 96 | + $this->loadFromFQCNs(); |
|
| 97 | + } catch (Exception $exception) { |
|
| 98 | + throw new CollectionLoaderException($exception); |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * @return CollectionInterface |
|
| 105 | - */ |
|
| 106 | - public function getCollection() |
|
| 107 | - { |
|
| 108 | - return $this->collection; |
|
| 109 | - } |
|
| 103 | + /** |
|
| 104 | + * @return CollectionInterface |
|
| 105 | + */ |
|
| 106 | + public function getCollection() |
|
| 107 | + { |
|
| 108 | + return $this->collection; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * @throws InvalidClassException |
|
| 114 | - * @throws InvalidFilePathException |
|
| 115 | - * @throws InvalidDataTypeException |
|
| 116 | - * @throws InvalidEntityException |
|
| 117 | - * @throws DuplicateCollectionIdentifierException |
|
| 118 | - */ |
|
| 119 | - protected function loadAllFromFilepaths() |
|
| 120 | - { |
|
| 121 | - if (! $this->file_locator instanceof FileLocator) { |
|
| 122 | - $this->file_locator = new FileLocator(); |
|
| 123 | - } |
|
| 124 | - $this->file_locator->setFileMask($this->collection_details->getFileMask()); |
|
| 125 | - // find all of the files that match the file mask in the specified folder |
|
| 126 | - $this->file_locator->locate($this->collection_details->getCollectionPaths()); |
|
| 127 | - // filter the results |
|
| 128 | - $filepaths = (array) apply_filters( |
|
| 129 | - 'FHEE__CollectionLoader__loadAllFromFilepath__filepaths', |
|
| 130 | - $this->file_locator->getFilePaths(), |
|
| 131 | - $this->collection_details->collectionName(), |
|
| 132 | - $this->collection_details |
|
| 133 | - ); |
|
| 134 | - if (empty($filepaths)) { |
|
| 135 | - return; |
|
| 136 | - } |
|
| 137 | - foreach ($filepaths as $filepath) { |
|
| 138 | - $this->loadClassFromFilepath($filepath); |
|
| 139 | - } |
|
| 140 | - } |
|
| 112 | + /** |
|
| 113 | + * @throws InvalidClassException |
|
| 114 | + * @throws InvalidFilePathException |
|
| 115 | + * @throws InvalidDataTypeException |
|
| 116 | + * @throws InvalidEntityException |
|
| 117 | + * @throws DuplicateCollectionIdentifierException |
|
| 118 | + */ |
|
| 119 | + protected function loadAllFromFilepaths() |
|
| 120 | + { |
|
| 121 | + if (! $this->file_locator instanceof FileLocator) { |
|
| 122 | + $this->file_locator = new FileLocator(); |
|
| 123 | + } |
|
| 124 | + $this->file_locator->setFileMask($this->collection_details->getFileMask()); |
|
| 125 | + // find all of the files that match the file mask in the specified folder |
|
| 126 | + $this->file_locator->locate($this->collection_details->getCollectionPaths()); |
|
| 127 | + // filter the results |
|
| 128 | + $filepaths = (array) apply_filters( |
|
| 129 | + 'FHEE__CollectionLoader__loadAllFromFilepath__filepaths', |
|
| 130 | + $this->file_locator->getFilePaths(), |
|
| 131 | + $this->collection_details->collectionName(), |
|
| 132 | + $this->collection_details |
|
| 133 | + ); |
|
| 134 | + if (empty($filepaths)) { |
|
| 135 | + return; |
|
| 136 | + } |
|
| 137 | + foreach ($filepaths as $filepath) { |
|
| 138 | + $this->loadClassFromFilepath($filepath); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * @param string|null $filepath |
|
| 145 | - * @return string |
|
| 146 | - * @throws InvalidEntityException |
|
| 147 | - * @throws InvalidDataTypeException |
|
| 148 | - * @throws InvalidFilePathException |
|
| 149 | - * @throws InvalidClassException |
|
| 150 | - * @throws DuplicateCollectionIdentifierException |
|
| 151 | - */ |
|
| 152 | - protected function loadClassFromFilepath(?string $filepath): string |
|
| 153 | - { |
|
| 154 | - if (! is_string($filepath)) { |
|
| 155 | - throw new InvalidDataTypeException('$filepath', $filepath, 'string'); |
|
| 156 | - } |
|
| 157 | - if (! is_readable($filepath)) { |
|
| 158 | - throw new InvalidFilePathException($filepath); |
|
| 159 | - } |
|
| 160 | - require_once $filepath; |
|
| 161 | - // extract filename from path |
|
| 162 | - $file_name = basename($filepath); |
|
| 163 | - // now remove any file extensions |
|
| 164 | - $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($file_name); |
|
| 165 | - if (! class_exists($class_name)) { |
|
| 166 | - throw new InvalidClassException($class_name); |
|
| 167 | - } |
|
| 168 | - $entity = $this->entity_factory instanceof FactoryInterface |
|
| 169 | - ? call_user_func(array($this->entity_factory, 'create'), $class_name) |
|
| 170 | - : new $class_name(); |
|
| 171 | - return $this->addEntityToCollection($entity, $file_name); |
|
| 172 | - } |
|
| 143 | + /** |
|
| 144 | + * @param string|null $filepath |
|
| 145 | + * @return string |
|
| 146 | + * @throws InvalidEntityException |
|
| 147 | + * @throws InvalidDataTypeException |
|
| 148 | + * @throws InvalidFilePathException |
|
| 149 | + * @throws InvalidClassException |
|
| 150 | + * @throws DuplicateCollectionIdentifierException |
|
| 151 | + */ |
|
| 152 | + protected function loadClassFromFilepath(?string $filepath): string |
|
| 153 | + { |
|
| 154 | + if (! is_string($filepath)) { |
|
| 155 | + throw new InvalidDataTypeException('$filepath', $filepath, 'string'); |
|
| 156 | + } |
|
| 157 | + if (! is_readable($filepath)) { |
|
| 158 | + throw new InvalidFilePathException($filepath); |
|
| 159 | + } |
|
| 160 | + require_once $filepath; |
|
| 161 | + // extract filename from path |
|
| 162 | + $file_name = basename($filepath); |
|
| 163 | + // now remove any file extensions |
|
| 164 | + $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($file_name); |
|
| 165 | + if (! class_exists($class_name)) { |
|
| 166 | + throw new InvalidClassException($class_name); |
|
| 167 | + } |
|
| 168 | + $entity = $this->entity_factory instanceof FactoryInterface |
|
| 169 | + ? call_user_func(array($this->entity_factory, 'create'), $class_name) |
|
| 170 | + : new $class_name(); |
|
| 171 | + return $this->addEntityToCollection($entity, $file_name); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | 174 | |
| 175 | - /** |
|
| 176 | - * @param $entity |
|
| 177 | - * @param mixed $identifier |
|
| 178 | - * @return string |
|
| 179 | - * @throws InvalidEntityException |
|
| 180 | - * @throws DuplicateCollectionIdentifierException |
|
| 181 | - */ |
|
| 182 | - protected function addEntityToCollection($entity, $identifier): string |
|
| 183 | - { |
|
| 184 | - do_action( |
|
| 185 | - 'FHEE__CollectionLoader__addEntityToCollection__entity', |
|
| 186 | - $entity, |
|
| 187 | - $this->collection_details->collectionName(), |
|
| 188 | - $this->collection_details |
|
| 189 | - ); |
|
| 190 | - $identifier = $this->setIdentifier($entity, $identifier); |
|
| 191 | - if ($this->collection->contains($entity) || $this->collection->has($identifier)) { |
|
| 192 | - do_action( |
|
| 193 | - 'FHEE__CollectionLoader__addEntityToCollection__entity_already_added', |
|
| 194 | - $this, |
|
| 195 | - $this->collection_details->collectionName(), |
|
| 196 | - $this->collection_details |
|
| 197 | - ); |
|
| 198 | - return CollectionLoader::ENTITY_EXISTS; |
|
| 199 | - } |
|
| 200 | - if ($this->collection->add($entity, $identifier)) { |
|
| 201 | - do_action( |
|
| 202 | - 'FHEE__CollectionLoader__addEntityToCollection__entity_added', |
|
| 203 | - $this, |
|
| 204 | - $this->collection_details->collectionName(), |
|
| 205 | - $this->collection_details |
|
| 206 | - ); |
|
| 207 | - return CollectionLoader::ENTITY_ADDED; |
|
| 208 | - } |
|
| 209 | - do_action( |
|
| 210 | - 'FHEE__CollectionLoader__addEntityToCollection__entity_not_added', |
|
| 211 | - $this, |
|
| 212 | - $this->collection_details->collectionName(), |
|
| 213 | - $this->collection_details |
|
| 214 | - ); |
|
| 215 | - return CollectionLoader::ENTITY_NOT_ADDED; |
|
| 216 | - } |
|
| 175 | + /** |
|
| 176 | + * @param $entity |
|
| 177 | + * @param mixed $identifier |
|
| 178 | + * @return string |
|
| 179 | + * @throws InvalidEntityException |
|
| 180 | + * @throws DuplicateCollectionIdentifierException |
|
| 181 | + */ |
|
| 182 | + protected function addEntityToCollection($entity, $identifier): string |
|
| 183 | + { |
|
| 184 | + do_action( |
|
| 185 | + 'FHEE__CollectionLoader__addEntityToCollection__entity', |
|
| 186 | + $entity, |
|
| 187 | + $this->collection_details->collectionName(), |
|
| 188 | + $this->collection_details |
|
| 189 | + ); |
|
| 190 | + $identifier = $this->setIdentifier($entity, $identifier); |
|
| 191 | + if ($this->collection->contains($entity) || $this->collection->has($identifier)) { |
|
| 192 | + do_action( |
|
| 193 | + 'FHEE__CollectionLoader__addEntityToCollection__entity_already_added', |
|
| 194 | + $this, |
|
| 195 | + $this->collection_details->collectionName(), |
|
| 196 | + $this->collection_details |
|
| 197 | + ); |
|
| 198 | + return CollectionLoader::ENTITY_EXISTS; |
|
| 199 | + } |
|
| 200 | + if ($this->collection->add($entity, $identifier)) { |
|
| 201 | + do_action( |
|
| 202 | + 'FHEE__CollectionLoader__addEntityToCollection__entity_added', |
|
| 203 | + $this, |
|
| 204 | + $this->collection_details->collectionName(), |
|
| 205 | + $this->collection_details |
|
| 206 | + ); |
|
| 207 | + return CollectionLoader::ENTITY_ADDED; |
|
| 208 | + } |
|
| 209 | + do_action( |
|
| 210 | + 'FHEE__CollectionLoader__addEntityToCollection__entity_not_added', |
|
| 211 | + $this, |
|
| 212 | + $this->collection_details->collectionName(), |
|
| 213 | + $this->collection_details |
|
| 214 | + ); |
|
| 215 | + return CollectionLoader::ENTITY_NOT_ADDED; |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | 218 | |
| 219 | - /** |
|
| 220 | - * @param $entity |
|
| 221 | - * @param mixed $identifier |
|
| 222 | - * @return string |
|
| 223 | - * @throws InvalidEntityException |
|
| 224 | - */ |
|
| 225 | - protected function setIdentifier($entity, $identifier): string |
|
| 226 | - { |
|
| 227 | - switch ($this->collection_details->identifierType()) { |
|
| 228 | - // every unique object gets added to the collection, but not duplicates of the exact same object |
|
| 229 | - case CollectionDetails::ID_OBJECT_HASH: |
|
| 230 | - $identifier = spl_object_hash($entity); |
|
| 231 | - break; |
|
| 232 | - // only one entity per class can be added to collection, like a singleton |
|
| 233 | - case CollectionDetails::ID_CLASS_NAME: |
|
| 234 | - $identifier = get_class($entity); |
|
| 235 | - break; |
|
| 236 | - // objects added to the collection based on entity callback, so the entity itself decides |
|
| 237 | - case CollectionDetails::ID_CALLBACK_METHOD: |
|
| 238 | - $identifier_callback = $this->collection_details->identifierCallback(); |
|
| 239 | - if (! method_exists($entity, $identifier_callback)) { |
|
| 240 | - throw new InvalidEntityException( |
|
| 241 | - $entity, |
|
| 242 | - $this->collection_details->getCollectionInterface(), |
|
| 243 | - sprintf( |
|
| 244 | - esc_html__( |
|
| 245 | - 'The current collection is configured to use a method named "%1$s" when setting or retrieving objects. The supplied entity is an instance |
|
| 219 | + /** |
|
| 220 | + * @param $entity |
|
| 221 | + * @param mixed $identifier |
|
| 222 | + * @return string |
|
| 223 | + * @throws InvalidEntityException |
|
| 224 | + */ |
|
| 225 | + protected function setIdentifier($entity, $identifier): string |
|
| 226 | + { |
|
| 227 | + switch ($this->collection_details->identifierType()) { |
|
| 228 | + // every unique object gets added to the collection, but not duplicates of the exact same object |
|
| 229 | + case CollectionDetails::ID_OBJECT_HASH: |
|
| 230 | + $identifier = spl_object_hash($entity); |
|
| 231 | + break; |
|
| 232 | + // only one entity per class can be added to collection, like a singleton |
|
| 233 | + case CollectionDetails::ID_CLASS_NAME: |
|
| 234 | + $identifier = get_class($entity); |
|
| 235 | + break; |
|
| 236 | + // objects added to the collection based on entity callback, so the entity itself decides |
|
| 237 | + case CollectionDetails::ID_CALLBACK_METHOD: |
|
| 238 | + $identifier_callback = $this->collection_details->identifierCallback(); |
|
| 239 | + if (! method_exists($entity, $identifier_callback)) { |
|
| 240 | + throw new InvalidEntityException( |
|
| 241 | + $entity, |
|
| 242 | + $this->collection_details->getCollectionInterface(), |
|
| 243 | + sprintf( |
|
| 244 | + esc_html__( |
|
| 245 | + 'The current collection is configured to use a method named "%1$s" when setting or retrieving objects. The supplied entity is an instance |
|
| 246 | 246 | of "%2$s", but does not contain this method.', |
| 247 | - 'event_espresso' |
|
| 248 | - ), |
|
| 249 | - $identifier_callback, |
|
| 250 | - get_class($entity) |
|
| 251 | - ) |
|
| 252 | - ); |
|
| 253 | - } |
|
| 254 | - $identifier = $entity->{$identifier_callback}(); |
|
| 255 | - break; |
|
| 256 | - } |
|
| 257 | - return apply_filters( |
|
| 258 | - 'FHEE__CollectionLoader__addEntityToCollection__identifier', |
|
| 259 | - $identifier, |
|
| 260 | - $this->collection_details->collectionName(), |
|
| 261 | - $this->collection_details |
|
| 262 | - ); |
|
| 263 | - } |
|
| 247 | + 'event_espresso' |
|
| 248 | + ), |
|
| 249 | + $identifier_callback, |
|
| 250 | + get_class($entity) |
|
| 251 | + ) |
|
| 252 | + ); |
|
| 253 | + } |
|
| 254 | + $identifier = $entity->{$identifier_callback}(); |
|
| 255 | + break; |
|
| 256 | + } |
|
| 257 | + return apply_filters( |
|
| 258 | + 'FHEE__CollectionLoader__addEntityToCollection__identifier', |
|
| 259 | + $identifier, |
|
| 260 | + $this->collection_details->collectionName(), |
|
| 261 | + $this->collection_details |
|
| 262 | + ); |
|
| 263 | + } |
|
| 264 | 264 | |
| 265 | 265 | |
| 266 | - /** |
|
| 267 | - * @throws ReflectionException |
|
| 268 | - * @throws InvalidArgumentException |
|
| 269 | - * @throws InvalidInterfaceException |
|
| 270 | - * @throws EE_Error |
|
| 271 | - * @throws InvalidClassException |
|
| 272 | - * @throws InvalidDataTypeException |
|
| 273 | - * @throws InvalidEntityException |
|
| 274 | - * @throws DuplicateCollectionIdentifierException |
|
| 275 | - */ |
|
| 276 | - protected function loadFromFQCNs() |
|
| 277 | - { |
|
| 278 | - $FQCNs = $this->collection_details->getCollectionFQCNs(); |
|
| 279 | - $FQCNs = (array) apply_filters( |
|
| 280 | - 'FHEE__CollectionLoader__loadAllFromFQCNs__FQCNs', |
|
| 281 | - $FQCNs, |
|
| 282 | - $this->collection_details->collectionName(), |
|
| 283 | - $this->collection_details |
|
| 284 | - ); |
|
| 285 | - foreach ($FQCNs as $FQCN) { |
|
| 286 | - $this->loadClassFromFQCN($FQCN); |
|
| 287 | - } |
|
| 288 | - } |
|
| 266 | + /** |
|
| 267 | + * @throws ReflectionException |
|
| 268 | + * @throws InvalidArgumentException |
|
| 269 | + * @throws InvalidInterfaceException |
|
| 270 | + * @throws EE_Error |
|
| 271 | + * @throws InvalidClassException |
|
| 272 | + * @throws InvalidDataTypeException |
|
| 273 | + * @throws InvalidEntityException |
|
| 274 | + * @throws DuplicateCollectionIdentifierException |
|
| 275 | + */ |
|
| 276 | + protected function loadFromFQCNs() |
|
| 277 | + { |
|
| 278 | + $FQCNs = $this->collection_details->getCollectionFQCNs(); |
|
| 279 | + $FQCNs = (array) apply_filters( |
|
| 280 | + 'FHEE__CollectionLoader__loadAllFromFQCNs__FQCNs', |
|
| 281 | + $FQCNs, |
|
| 282 | + $this->collection_details->collectionName(), |
|
| 283 | + $this->collection_details |
|
| 284 | + ); |
|
| 285 | + foreach ($FQCNs as $FQCN) { |
|
| 286 | + $this->loadClassFromFQCN($FQCN); |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | 289 | |
| 290 | 290 | |
| 291 | - /** |
|
| 292 | - * @param string|null $FQCN Fully Qualified Class Name |
|
| 293 | - * @return string |
|
| 294 | - * @throws InvalidArgumentException |
|
| 295 | - * @throws InvalidInterfaceException |
|
| 296 | - * @throws ReflectionException |
|
| 297 | - * @throws EE_Error |
|
| 298 | - * @throws InvalidEntityException |
|
| 299 | - * @throws InvalidDataTypeException |
|
| 300 | - * @throws InvalidClassException |
|
| 301 | - * @throws DuplicateCollectionIdentifierException |
|
| 302 | - */ |
|
| 303 | - protected function loadClassFromFQCN(?string $FQCN): string |
|
| 304 | - { |
|
| 305 | - if (! is_string($FQCN)) { |
|
| 306 | - throw new InvalidDataTypeException('$FQCN', $FQCN, 'string'); |
|
| 307 | - } |
|
| 308 | - if (! class_exists($FQCN)) { |
|
| 309 | - throw new InvalidClassException($FQCN); |
|
| 310 | - } |
|
| 311 | - do_action( |
|
| 312 | - 'FHEE__CollectionLoader__loadClassFromFQCN__beforeLoading', |
|
| 313 | - $FQCN, |
|
| 314 | - $this->collection_details->collectionName(), |
|
| 315 | - $this->collection_details |
|
| 316 | - ); |
|
| 317 | - $entity = $this->entity_factory instanceof FactoryInterface |
|
| 318 | - ? call_user_func(array($this->entity_factory, 'create'), $FQCN) |
|
| 319 | - : EE_Registry::instance()->create($FQCN); |
|
| 320 | - return $this->addEntityToCollection($entity, $FQCN); |
|
| 321 | - } |
|
| 291 | + /** |
|
| 292 | + * @param string|null $FQCN Fully Qualified Class Name |
|
| 293 | + * @return string |
|
| 294 | + * @throws InvalidArgumentException |
|
| 295 | + * @throws InvalidInterfaceException |
|
| 296 | + * @throws ReflectionException |
|
| 297 | + * @throws EE_Error |
|
| 298 | + * @throws InvalidEntityException |
|
| 299 | + * @throws InvalidDataTypeException |
|
| 300 | + * @throws InvalidClassException |
|
| 301 | + * @throws DuplicateCollectionIdentifierException |
|
| 302 | + */ |
|
| 303 | + protected function loadClassFromFQCN(?string $FQCN): string |
|
| 304 | + { |
|
| 305 | + if (! is_string($FQCN)) { |
|
| 306 | + throw new InvalidDataTypeException('$FQCN', $FQCN, 'string'); |
|
| 307 | + } |
|
| 308 | + if (! class_exists($FQCN)) { |
|
| 309 | + throw new InvalidClassException($FQCN); |
|
| 310 | + } |
|
| 311 | + do_action( |
|
| 312 | + 'FHEE__CollectionLoader__loadClassFromFQCN__beforeLoading', |
|
| 313 | + $FQCN, |
|
| 314 | + $this->collection_details->collectionName(), |
|
| 315 | + $this->collection_details |
|
| 316 | + ); |
|
| 317 | + $entity = $this->entity_factory instanceof FactoryInterface |
|
| 318 | + ? call_user_func(array($this->entity_factory, 'create'), $FQCN) |
|
| 319 | + : EE_Registry::instance()->create($FQCN); |
|
| 320 | + return $this->addEntityToCollection($entity, $FQCN); |
|
| 321 | + } |
|
| 322 | 322 | } |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | ) { |
| 87 | 87 | try { |
| 88 | 88 | $this->collection_details = $collection_details; |
| 89 | - if (! $collection instanceof CollectionInterface) { |
|
| 89 | + if ( ! $collection instanceof CollectionInterface) { |
|
| 90 | 90 | $collection = new Collection($this->collection_details->getCollectionInterface()); |
| 91 | 91 | } |
| 92 | 92 | $this->collection = $collection; |
@@ -118,7 +118,7 @@ discard block |
||
| 118 | 118 | */ |
| 119 | 119 | protected function loadAllFromFilepaths() |
| 120 | 120 | { |
| 121 | - if (! $this->file_locator instanceof FileLocator) { |
|
| 121 | + if ( ! $this->file_locator instanceof FileLocator) { |
|
| 122 | 122 | $this->file_locator = new FileLocator(); |
| 123 | 123 | } |
| 124 | 124 | $this->file_locator->setFileMask($this->collection_details->getFileMask()); |
@@ -151,10 +151,10 @@ discard block |
||
| 151 | 151 | */ |
| 152 | 152 | protected function loadClassFromFilepath(?string $filepath): string |
| 153 | 153 | { |
| 154 | - if (! is_string($filepath)) { |
|
| 154 | + if ( ! is_string($filepath)) { |
|
| 155 | 155 | throw new InvalidDataTypeException('$filepath', $filepath, 'string'); |
| 156 | 156 | } |
| 157 | - if (! is_readable($filepath)) { |
|
| 157 | + if ( ! is_readable($filepath)) { |
|
| 158 | 158 | throw new InvalidFilePathException($filepath); |
| 159 | 159 | } |
| 160 | 160 | require_once $filepath; |
@@ -162,7 +162,7 @@ discard block |
||
| 162 | 162 | $file_name = basename($filepath); |
| 163 | 163 | // now remove any file extensions |
| 164 | 164 | $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($file_name); |
| 165 | - if (! class_exists($class_name)) { |
|
| 165 | + if ( ! class_exists($class_name)) { |
|
| 166 | 166 | throw new InvalidClassException($class_name); |
| 167 | 167 | } |
| 168 | 168 | $entity = $this->entity_factory instanceof FactoryInterface |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | // objects added to the collection based on entity callback, so the entity itself decides |
| 237 | 237 | case CollectionDetails::ID_CALLBACK_METHOD: |
| 238 | 238 | $identifier_callback = $this->collection_details->identifierCallback(); |
| 239 | - if (! method_exists($entity, $identifier_callback)) { |
|
| 239 | + if ( ! method_exists($entity, $identifier_callback)) { |
|
| 240 | 240 | throw new InvalidEntityException( |
| 241 | 241 | $entity, |
| 242 | 242 | $this->collection_details->getCollectionInterface(), |
@@ -302,10 +302,10 @@ discard block |
||
| 302 | 302 | */ |
| 303 | 303 | protected function loadClassFromFQCN(?string $FQCN): string |
| 304 | 304 | { |
| 305 | - if (! is_string($FQCN)) { |
|
| 305 | + if ( ! is_string($FQCN)) { |
|
| 306 | 306 | throw new InvalidDataTypeException('$FQCN', $FQCN, 'string'); |
| 307 | 307 | } |
| 308 | - if (! class_exists($FQCN)) { |
|
| 308 | + if ( ! class_exists($FQCN)) { |
|
| 309 | 309 | throw new InvalidClassException($FQCN); |
| 310 | 310 | } |
| 311 | 311 | do_action( |
@@ -18,131 +18,131 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class TableAnalysis extends EE_Base implements InterminableInterface |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * The maximum number of characters that can be indexed on a column using utf8mb4 collation, |
|
| 23 | - * see https://events.codebasehq.com/redirect?https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/ |
|
| 24 | - */ |
|
| 25 | - const INDEX_COLUMN_SIZE = 191; |
|
| 21 | + /** |
|
| 22 | + * The maximum number of characters that can be indexed on a column using utf8mb4 collation, |
|
| 23 | + * see https://events.codebasehq.com/redirect?https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/ |
|
| 24 | + */ |
|
| 25 | + const INDEX_COLUMN_SIZE = 191; |
|
| 26 | 26 | |
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Returns the table name which will definitely have the wpdb prefix on the front, |
|
| 30 | - * except if it currently has the wpdb->base_prefix on the front, in which case |
|
| 31 | - * it will have the wpdb->base_prefix on it |
|
| 32 | - * |
|
| 33 | - * @param string $table_name |
|
| 34 | - * @return string $tableName, having ensured it has the wpdb prefix on the front |
|
| 35 | - * @global wpdb $wpdb |
|
| 36 | - */ |
|
| 37 | - public function ensureTableNameHasPrefix(string $table_name): string |
|
| 38 | - { |
|
| 39 | - global $wpdb; |
|
| 40 | - return strpos($table_name, $wpdb->base_prefix) === 0 ? $table_name : $wpdb->prefix . $table_name; |
|
| 41 | - } |
|
| 28 | + /** |
|
| 29 | + * Returns the table name which will definitely have the wpdb prefix on the front, |
|
| 30 | + * except if it currently has the wpdb->base_prefix on the front, in which case |
|
| 31 | + * it will have the wpdb->base_prefix on it |
|
| 32 | + * |
|
| 33 | + * @param string $table_name |
|
| 34 | + * @return string $tableName, having ensured it has the wpdb prefix on the front |
|
| 35 | + * @global wpdb $wpdb |
|
| 36 | + */ |
|
| 37 | + public function ensureTableNameHasPrefix(string $table_name): string |
|
| 38 | + { |
|
| 39 | + global $wpdb; |
|
| 40 | + return strpos($table_name, $wpdb->base_prefix) === 0 ? $table_name : $wpdb->prefix . $table_name; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Indicates whether or not the table has any entries. $table_name can |
|
| 46 | - * optionally start with $wpdb->prefix or not |
|
| 47 | - * |
|
| 48 | - * @param string $table_name |
|
| 49 | - * @return bool |
|
| 50 | - * @global wpdb $wpdb |
|
| 51 | - */ |
|
| 52 | - public function tableIsEmpty(string $table_name): bool |
|
| 53 | - { |
|
| 54 | - global $wpdb; |
|
| 55 | - $table_name = $this->ensureTableNameHasPrefix($table_name); |
|
| 56 | - if ($this->tableExists($table_name)) { |
|
| 57 | - $count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name"); |
|
| 58 | - return absint($count) === 0; |
|
| 59 | - } |
|
| 60 | - return false; |
|
| 61 | - } |
|
| 44 | + /** |
|
| 45 | + * Indicates whether or not the table has any entries. $table_name can |
|
| 46 | + * optionally start with $wpdb->prefix or not |
|
| 47 | + * |
|
| 48 | + * @param string $table_name |
|
| 49 | + * @return bool |
|
| 50 | + * @global wpdb $wpdb |
|
| 51 | + */ |
|
| 52 | + public function tableIsEmpty(string $table_name): bool |
|
| 53 | + { |
|
| 54 | + global $wpdb; |
|
| 55 | + $table_name = $this->ensureTableNameHasPrefix($table_name); |
|
| 56 | + if ($this->tableExists($table_name)) { |
|
| 57 | + $count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name"); |
|
| 58 | + return absint($count) === 0; |
|
| 59 | + } |
|
| 60 | + return false; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Indicates whether or not the table exists. $table_name can optionally |
|
| 66 | - * have the $wpdb->prefix on the beginning, or not. |
|
| 67 | - * |
|
| 68 | - * @param string $table_name |
|
| 69 | - * @return bool |
|
| 70 | - * @global wpdb $wpdb |
|
| 71 | - */ |
|
| 72 | - public function tableExists(string $table_name): bool |
|
| 73 | - { |
|
| 74 | - global $wpdb, $EZSQL_ERROR; |
|
| 75 | - $table_name = $this->ensureTableNameHasPrefix($table_name); |
|
| 76 | - // ignore if this causes an sql error |
|
| 77 | - $old_error = $wpdb->last_error; |
|
| 78 | - $old_suppress_errors = $wpdb->suppress_errors(); |
|
| 79 | - $old_show_errors_value = $wpdb->show_errors(false); |
|
| 80 | - $ezsql_error_cache = $EZSQL_ERROR; |
|
| 81 | - $wpdb->get_results("SELECT * from $table_name LIMIT 1"); |
|
| 82 | - $wpdb->show_errors($old_show_errors_value); |
|
| 83 | - $wpdb->suppress_errors($old_suppress_errors); |
|
| 84 | - $new_error = $wpdb->last_error; |
|
| 85 | - $wpdb->last_error = $old_error; |
|
| 86 | - $EZSQL_ERROR = $ezsql_error_cache; |
|
| 87 | - // if there was a table doesn't exist error |
|
| 88 | - if (! empty($new_error)) { |
|
| 89 | - if ( |
|
| 90 | - in_array( |
|
| 91 | - EEH_Activation::last_wpdb_error_code(), |
|
| 92 | - [ |
|
| 93 | - 1051, // bad table |
|
| 94 | - 1109, // unknown table |
|
| 95 | - 117, // no such table |
|
| 96 | - ] |
|
| 97 | - ) |
|
| 98 | - || preg_match( |
|
| 99 | - '~^Table .* doesn\'t exist~', |
|
| 100 | - $new_error |
|
| 101 | - ) // in case not using mysql and error codes aren't reliable, just check for this error string |
|
| 102 | - ) { |
|
| 103 | - return false; |
|
| 104 | - } else { |
|
| 105 | - // log this because that's weird. Just use the normal PHP error log |
|
| 106 | - error_log( |
|
| 107 | - sprintf( |
|
| 108 | - esc_html__( |
|
| 109 | - 'Event Espresso error detected when checking if table existed: %1$s (it wasn\'t just that the table didn\'t exist either)', |
|
| 110 | - 'event_espresso' |
|
| 111 | - ), |
|
| 112 | - $new_error |
|
| 113 | - ) |
|
| 114 | - ); |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - return true; |
|
| 118 | - } |
|
| 64 | + /** |
|
| 65 | + * Indicates whether or not the table exists. $table_name can optionally |
|
| 66 | + * have the $wpdb->prefix on the beginning, or not. |
|
| 67 | + * |
|
| 68 | + * @param string $table_name |
|
| 69 | + * @return bool |
|
| 70 | + * @global wpdb $wpdb |
|
| 71 | + */ |
|
| 72 | + public function tableExists(string $table_name): bool |
|
| 73 | + { |
|
| 74 | + global $wpdb, $EZSQL_ERROR; |
|
| 75 | + $table_name = $this->ensureTableNameHasPrefix($table_name); |
|
| 76 | + // ignore if this causes an sql error |
|
| 77 | + $old_error = $wpdb->last_error; |
|
| 78 | + $old_suppress_errors = $wpdb->suppress_errors(); |
|
| 79 | + $old_show_errors_value = $wpdb->show_errors(false); |
|
| 80 | + $ezsql_error_cache = $EZSQL_ERROR; |
|
| 81 | + $wpdb->get_results("SELECT * from $table_name LIMIT 1"); |
|
| 82 | + $wpdb->show_errors($old_show_errors_value); |
|
| 83 | + $wpdb->suppress_errors($old_suppress_errors); |
|
| 84 | + $new_error = $wpdb->last_error; |
|
| 85 | + $wpdb->last_error = $old_error; |
|
| 86 | + $EZSQL_ERROR = $ezsql_error_cache; |
|
| 87 | + // if there was a table doesn't exist error |
|
| 88 | + if (! empty($new_error)) { |
|
| 89 | + if ( |
|
| 90 | + in_array( |
|
| 91 | + EEH_Activation::last_wpdb_error_code(), |
|
| 92 | + [ |
|
| 93 | + 1051, // bad table |
|
| 94 | + 1109, // unknown table |
|
| 95 | + 117, // no such table |
|
| 96 | + ] |
|
| 97 | + ) |
|
| 98 | + || preg_match( |
|
| 99 | + '~^Table .* doesn\'t exist~', |
|
| 100 | + $new_error |
|
| 101 | + ) // in case not using mysql and error codes aren't reliable, just check for this error string |
|
| 102 | + ) { |
|
| 103 | + return false; |
|
| 104 | + } else { |
|
| 105 | + // log this because that's weird. Just use the normal PHP error log |
|
| 106 | + error_log( |
|
| 107 | + sprintf( |
|
| 108 | + esc_html__( |
|
| 109 | + 'Event Espresso error detected when checking if table existed: %1$s (it wasn\'t just that the table didn\'t exist either)', |
|
| 110 | + 'event_espresso' |
|
| 111 | + ), |
|
| 112 | + $new_error |
|
| 113 | + ) |
|
| 114 | + ); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + return true; |
|
| 118 | + } |
|
| 119 | 119 | |
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * @param string $table_name |
|
| 123 | - * @param string $index_name |
|
| 124 | - * @return array of columns used on that index, Each entry is an object with the following properties { |
|
| 125 | - * @type string Table |
|
| 126 | - * @type string Non_unique "0" or "1" |
|
| 127 | - * @type string Key_name |
|
| 128 | - * @type string Seq_in_index |
|
| 129 | - * @type string Column_name |
|
| 130 | - * @type string Collation |
|
| 131 | - * @type string Cardinality |
|
| 132 | - * @type string Sub_part on a column, usually this is just the number of characters from this column to use in |
|
| 133 | - * indexing |
|
| 134 | - * @type string|null Packed |
|
| 135 | - * @type string Null |
|
| 136 | - * @type string Index_type |
|
| 137 | - * @type string Comment |
|
| 138 | - * @type string Index_comment |
|
| 139 | - * } |
|
| 140 | - */ |
|
| 141 | - public function showIndexes(string $table_name, string $index_name): array |
|
| 142 | - { |
|
| 143 | - global $wpdb; |
|
| 144 | - $table_name = $this->ensureTableNameHasPrefix($table_name); |
|
| 145 | - $index_exists_query = "SHOW INDEX FROM {$table_name} WHERE Key_name = '{$index_name}'"; |
|
| 146 | - return $wpdb->get_results($index_exists_query); |
|
| 147 | - } |
|
| 121 | + /** |
|
| 122 | + * @param string $table_name |
|
| 123 | + * @param string $index_name |
|
| 124 | + * @return array of columns used on that index, Each entry is an object with the following properties { |
|
| 125 | + * @type string Table |
|
| 126 | + * @type string Non_unique "0" or "1" |
|
| 127 | + * @type string Key_name |
|
| 128 | + * @type string Seq_in_index |
|
| 129 | + * @type string Column_name |
|
| 130 | + * @type string Collation |
|
| 131 | + * @type string Cardinality |
|
| 132 | + * @type string Sub_part on a column, usually this is just the number of characters from this column to use in |
|
| 133 | + * indexing |
|
| 134 | + * @type string|null Packed |
|
| 135 | + * @type string Null |
|
| 136 | + * @type string Index_type |
|
| 137 | + * @type string Comment |
|
| 138 | + * @type string Index_comment |
|
| 139 | + * } |
|
| 140 | + */ |
|
| 141 | + public function showIndexes(string $table_name, string $index_name): array |
|
| 142 | + { |
|
| 143 | + global $wpdb; |
|
| 144 | + $table_name = $this->ensureTableNameHasPrefix($table_name); |
|
| 145 | + $index_exists_query = "SHOW INDEX FROM {$table_name} WHERE Key_name = '{$index_name}'"; |
|
| 146 | + return $wpdb->get_results($index_exists_query); |
|
| 147 | + } |
|
| 148 | 148 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | public function ensureTableNameHasPrefix(string $table_name): string |
| 38 | 38 | { |
| 39 | 39 | global $wpdb; |
| 40 | - return strpos($table_name, $wpdb->base_prefix) === 0 ? $table_name : $wpdb->prefix . $table_name; |
|
| 40 | + return strpos($table_name, $wpdb->base_prefix) === 0 ? $table_name : $wpdb->prefix.$table_name; |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | $wpdb->last_error = $old_error; |
| 86 | 86 | $EZSQL_ERROR = $ezsql_error_cache; |
| 87 | 87 | // if there was a table doesn't exist error |
| 88 | - if (! empty($new_error)) { |
|
| 88 | + if ( ! empty($new_error)) { |
|
| 89 | 89 | if ( |
| 90 | 90 | in_array( |
| 91 | 91 | EEH_Activation::last_wpdb_error_code(), |