Skip to content

Commit

Permalink
Fixed FlexObject::triggerEvent() does not emit events [#2816]
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Feb 10, 2020
1 parent 3a722ba commit 234abb3
Show file tree
Hide file tree
Showing 37 changed files with 797 additions and 315 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Regression: Fixed bad method call in FlexDirectory::getAuthorizeRule()
* Regression: Fixed fatal error in admin if the site has custom permissions in `onAdminRegisterPermissions`
* Regression: Fixed flex user index with folder storage
* Fixed `FlexObject::triggerEvent()` does not emit events [#2816](https://github.com/getgrav/grav/issues/2816)
* Grav 1.7: Fixed saving Flex configuration with ignored values becoming null

# v1.7.0-rc.5
Expand Down
8 changes: 4 additions & 4 deletions system/blueprints/flex/pages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ config:

# Data Configuration
data:
object: 'Grav\Common\Flex\Pages\PageObject'
collection: 'Grav\Common\Flex\Pages\PageCollection'
index: 'Grav\Common\Flex\Pages\PageIndex'
object: 'Grav\Common\Flex\Types\Pages\PageObject'
collection: 'Grav\Common\Flex\Types\Pages\PageCollection'
index: 'Grav\Common\Flex\Types\Pages\PageIndex'
storage:
class: 'Grav\Common\Flex\Pages\PageStorage'
class: 'Grav\Common\Flex\Types\Pages\Storage\PageStorage'
options:
formatter:
class: 'Grav\Framework\File\Formatter\MarkdownFormatter'
Expand Down
8 changes: 4 additions & 4 deletions system/blueprints/flex/user-accounts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ config:

# Data Configuration
data:
object: 'Grav\Common\Flex\Users\UserObject'
collection: 'Grav\Common\Flex\Users\UserCollection'
index: 'Grav\Common\Flex\Users\UserIndex'
object: 'Grav\Common\Flex\Types\Users\UserObject'
collection: 'Grav\Common\Flex\Types\Users\UserCollection'
index: 'Grav\Common\Flex\Types\Users\UserIndex'
storage:
class: 'Grav\Common\Flex\Users\Storage\UserFileStorage'
class: 'Grav\Common\Flex\Types\Users\Storage\UserFileStorage'
options:
formatter:
class: 'Grav\Framework\File\Formatter\YamlFormatter'
Expand Down
6 changes: 3 additions & 3 deletions system/blueprints/flex/user-groups.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ config:

# Data Configuration
data:
object: 'Grav\Common\Flex\UserGroups\UserGroupObject'
collection: 'Grav\Common\Flex\UserGroups\UserGroupCollection'
index: 'Grav\Common\Flex\UserGroups\UserGroupIndex'
object: 'Grav\Common\Flex\Types\UserGroups\UserGroupObject'
collection: 'Grav\Common\Flex\Types\UserGroups\UserGroupCollection'
index: 'Grav\Common\Flex\Types\UserGroups\UserGroupIndex'
storage:
class: 'Grav\Framework\Flex\Storage\SimpleStorage'
options:
Expand Down
43 changes: 43 additions & 0 deletions system/src/Grav/Common/Flex/Traits/FlexCollectionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* @package Grav\Common\Flex
*
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Flex\Traits;

use RocketTheme\Toolbox\Event\Event;

/**
* Trait FlexCollectionTrait
* @package Grav\Common\Flex\Traits
*/
trait FlexCollectionTrait
{
use FlexCommonTrait;

/**
* @param string $name
* @param object|null $event
* @return $this
*/
public function triggerEvent(string $name, $event = null)
{
if (null === $event) {
$event = new Event(['collection' => $this]);
}
if (strpos($name, 'onFlexCollection') !== 0 && strpos($name, 'on') === 0) {
$name = 'onFlexCollection' . substr($name, 2);
}

$container = $this->getContainer();
$container->fireEvent($name, $event);

return $this;
}
}
54 changes: 54 additions & 0 deletions system/src/Grav/Common/Flex/Traits/FlexCommonTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/**
* @package Grav\Common\Flex
*
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Flex\Traits;

use Grav\Common\Debugger;
use Grav\Common\Grav;
use Grav\Common\Twig\Twig;
use Twig\Error\LoaderError;
use Twig\Error\SyntaxError;
use Twig\Template;
use Twig\TemplateWrapper;

/**
* Trait FlexCommonTrait
* @package Grav\Common\Flex\Traits
*/
trait FlexCommonTrait
{
/**
* @param string $layout
* @return Template|TemplateWrapper
* @throws LoaderError
* @throws SyntaxError
*/
protected function getTemplate($layout)
{
$container = $this->getContainer();

/** @var Twig $twig */
$twig = $container['twig'];

try {
return $twig->twig()->resolveTemplate($this->getTemplatePaths($layout));
} catch (LoaderError $e) {
/** @var Debugger $debugger */
$debugger = Grav::instance()['debugger'];
$debugger->addException($e);

return $twig->twig()->resolveTemplate(['flex/404.html.twig']);
}
}

abstract protected function getTemplatePaths(string $layout): array;
abstract protected function getContainer(): Grav;
}
74 changes: 74 additions & 0 deletions system/src/Grav/Common/Flex/Traits/FlexGravTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/**
* @package Grav\Common\Flex
*
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Flex\Traits;

use Grav\Common\Grav;
use Grav\Common\User\Interfaces\UserInterface;
use Grav\Framework\Flex\Flex;

/**
* Implements Grav specific logic
*/
trait FlexGravTrait
{
/**
* @return Grav
*/
protected function getContainer(): Grav
{
return Grav::instance();
}

/**
* @return Flex
*/
protected function getFlexContainer(): Flex
{
$container = $this->getContainer();

/** @var Flex $flex */
$flex = $container['flex'];

return $flex;
}

/**
* @return UserInterface|null
*/
protected function getActiveUser(): ?UserInterface
{
$container = $this->getContainer();

/** @var UserInterface|null $user */
$user = $container['user'] ?? null;

return $user;
}

/**
* @return bool
*/
protected function isAdminSite(): bool
{
$container = $this->getContainer();

return isset($container['admin']);
}

/**
* @return string
*/
protected function getAuthorizeScope(): string
{
return $this->isAdminSite() ? 'admin' : 'site';
}
}
20 changes: 20 additions & 0 deletions system/src/Grav/Common/Flex/Traits/FlexIndexTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

/**
* @package Grav\Common\Flex
*
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Flex\Traits;

/**
* Trait FlexIndexTrait
* @package Grav\Common\Flex\Traits
*/
trait FlexIndexTrait
{
}
43 changes: 43 additions & 0 deletions system/src/Grav/Common/Flex/Traits/FlexObjectTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* @package Grav\Common\Flex
*
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Flex\Traits;

use RocketTheme\Toolbox\Event\Event;

/**
* Trait FlexObjectTrait
* @package Grav\Common\Flex\Traits
*/
trait FlexObjectTrait
{
use FlexCommonTrait;

/**
* @param string $name
* @param object|null $event
* @return $this
*/
public function triggerEvent(string $name, $event = null)
{
if (null === $event) {
$event = new Event(['object' => $this]);
}
if (strpos($name, 'onFlexObject') !== 0 && strpos($name, 'on') === 0) {
$name = 'onFlexObject' . substr($name, 2);
}

$container = $this->getContainer();
$container->fireEvent($name, $event);

return $this;
}
}
26 changes: 26 additions & 0 deletions system/src/Grav/Common/Flex/Types/Generic/GenericCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/**
* @package Grav\Common\Flex
*
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Flex\Types\Generic;

use Grav\Common\Flex\Traits\FlexCollectionTrait;
use Grav\Common\Flex\Traits\FlexGravTrait;
use Grav\Framework\Flex\FlexCollection;

/**
* Class GenericCollection
* @package Grav\Common\Flex\Generic
*/
class GenericCollection extends FlexCollection
{
use FlexGravTrait;
use FlexCollectionTrait;
}
26 changes: 26 additions & 0 deletions system/src/Grav/Common/Flex/Types/Generic/GenericIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/**
* @package Grav\Common\Flex
*
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Flex\Types\Generic;

use Grav\Common\Flex\Traits\FlexGravTrait;
use Grav\Common\Flex\Traits\FlexIndexTrait;
use Grav\Framework\Flex\FlexIndex;

/**
* Class GenericIndex
* @package Grav\Common\Flex\Generic
*/
class GenericIndex extends FlexIndex
{
use FlexGravTrait;
use FlexIndexTrait;
}
26 changes: 26 additions & 0 deletions system/src/Grav/Common/Flex/Types/Generic/GenericObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/**
* @package Grav\Common\Flex
*
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Flex\Types\Generic;

use Grav\Common\Flex\Traits\FlexGravTrait;
use Grav\Common\Flex\Traits\FlexObjectTrait;
use Grav\Framework\Flex\FlexObject;

/**
* Class GenericObject
* @package Grav\Common\Flex\Generic
*/
class GenericObject extends FlexObject
{
use FlexGravTrait;
use FlexObjectTrait;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
* @license MIT License; see LICENSE file for details.
*/

namespace Grav\Common\Flex\Pages;
namespace Grav\Common\Flex\Types\Pages;

use Grav\Common\Flex\Traits\FlexCollectionTrait;
use Grav\Common\Flex\Traits\FlexGravTrait;
use Grav\Common\Page\Interfaces\PageCollectionInterface;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Common\Utils;
Expand All @@ -34,6 +36,9 @@
*/
class PageCollection extends FlexPageCollection implements PageCollectionInterface
{
use FlexGravTrait;
use FlexCollectionTrait;

/** @var array|null */
protected $_params;

Expand Down
Loading

0 comments on commit 234abb3

Please sign in to comment.