Skip to content

Commit

Permalink
Add restore blog method
Browse files Browse the repository at this point in the history
  • Loading branch information
kayue committed Jun 5, 2017
1 parent 2165662 commit 9765505
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Twig/Extension/WordpressExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public function switchBlog($id)
$this->manager = $this->managerRegistry->getManager();
}

public function restoreBlog()
{
$this->managerRegistry->restorePreviousBlog();
$this->manager = $this->managerRegistry->getManager();
}

public function findOneOptionBy($criteria)
{
if (is_string($criteria)) {
Expand Down
18 changes: 17 additions & 1 deletion Wordpress/ManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class ManagerRegistry implements ManagerRegistryInterface
protected $rootDir;
protected $environment;
protected $currentBlogId = 1;
protected $previousBlogId = 1;
protected $managers = [];

public function __construct(
Expand All @@ -60,7 +61,7 @@ public function __construct(
public function getManager($blogId = null)
{
if ($blogId !== null && $blogId !== $this->currentBlogId) {
$this->currentBlogId = $blogId;
$this->setCurrentBlogId($blogId);
}

if (!isset($this->managers[$this->currentBlogId])) {
Expand All @@ -85,13 +86,28 @@ public function getManager($blogId = null)
}

/**
* Switches the active blog until the user calls the restorePreviousBlog() method.
*
* @param $blogId
*/
public function setCurrentBlogId($blogId)
{
if ($this->currentBlogId === $blogId) {
return;
}

$this->previousBlogId = $this->currentBlogId;
$this->currentBlogId = $blogId;
}

/**
* Switches active blog back after user calls the setCurrentBlogId() method.
*/
public function restorePreviousBlog()
{
$this->setCurrentBlogId($this->previousBlogId);
}

/**
* Loads a configured object manager metadata, query or result cache driver.
*
Expand Down

0 comments on commit 9765505

Please sign in to comment.