samsonasik /
SanSessionToolbar
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /** |
||
| 6 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
| 7 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
| 8 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||
| 9 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||
| 10 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
| 11 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||
| 12 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
| 13 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
| 14 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
| 15 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
| 16 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 17 | * |
||
| 18 | * This software consists of voluntary contributions made by many individuals |
||
| 19 | * and is licensed under the MIT license. |
||
| 20 | */ |
||
| 21 | |||
| 22 | namespace SanSessionToolbar; |
||
| 23 | |||
| 24 | use Laminas\EventManager\EventInterface; |
||
| 25 | use Laminas\EventManager\SharedEventManagerInterface; |
||
| 26 | use Laminas\ModuleManager\Feature\ConfigProviderInterface; |
||
| 27 | use Laminas\Mvc\Controller\AbstractActionController; |
||
| 28 | use Laminas\Mvc\MvcEvent; |
||
| 29 | use Laminas\Session\Container; |
||
| 30 | use Laminas\Stdlib\SplQueue; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @author Abdul Malik Ikhsan <[email protected]> |
||
| 34 | */ |
||
| 35 | class Module implements ConfigProviderInterface |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * Bootstrap Handle FlashMessenger session show. |
||
| 39 | 16 | */ |
|
| 40 | public function onBootstrap(MvcEvent $mvcEvent) |
||
| 41 | 16 | { |
|
| 42 | 16 | $manager = Container::getDefaultManager(); |
|
| 43 | 2 | if (!$manager->sessionExists()) { |
|
| 44 | return; |
||
| 45 | } |
||
| 46 | 14 | ||
| 47 | $application = $mvcEvent->getApplication(); |
||
| 48 | 14 | /** @var SharedEventManagerInterface $sharedEventManager */ |
|
| 49 | $sharedEventManager = $application->getEventManager()->getSharedManager(); |
||
| 50 | 14 | ||
| 51 | 14 | $sharedEventManager->attach( |
|
| 52 | 14 | AbstractActionController::class, |
|
| 53 | 14 | 'dispatch', |
|
| 54 | 14 | [$this, 'flashMessengerHandler'], |
|
| 55 | 2 |
||
| 56 | 14 | ); |
|
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Used to duplicate flashMessenger data as it shown and gone. |
||
| 61 | 13 | */ |
|
| 62 | private function duplicateFlashMessengerSessionData(Container $container): void |
||
| 63 | 13 | { |
|
| 64 | 13 | $flashToolbarContainer = new Container('SanSessionToolbarFlashMessenger'); |
|
| 65 | 1 | foreach ($container->getArrayCopy() as $key => $row) { |
|
| 66 | 1 | foreach ($row->toArray() as $keyArray => $rowArray) { |
|
| 67 | 1 | if ($keyArray === 0) { |
|
| 68 | $flashToolbarContainer->$key = new SplQueue(); |
||
| 69 | 1 | } |
|
| 70 | |||
| 71 | $flashToolbarContainer->$key->push($rowArray); |
||
| 72 | 13 | } |
|
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | 14 | * Handle FlashMessenger data to be able to be seen in both "app" and toolbar parts. |
|
| 78 | */ |
||
| 79 | public function flashMessengerHandler(EventInterface $event): void |
||
| 80 | 14 | { |
|
| 81 | 14 | /** @var AbstractActionController $target */ |
|
| 82 | 1 | $target = $event->getTarget(); |
|
| 83 | if (!$target->getPluginManager()->has('flashMessenger')) { |
||
| 84 | return; |
||
| 85 | 13 | } |
|
| 86 | 13 | ||
| 87 | 13 | $flash = $target->plugin('flashMessenger'); |
|
| 88 | 13 | $container = $flash->getContainer(); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 89 | $this->duplicateFlashMessengerSessionData($container); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | 13 | * {@inheritdoc} |
|
| 94 | */ |
||
| 95 | 13 | public function getConfig(): array |
|
| 96 | { |
||
| 97 | return include __DIR__.'/../config/module.config.php'; |
||
| 98 | } |
||
| 99 | } |
||
| 100 |