Skip to content

Commit

Permalink
In Symfony 3, constructor of WordpressExtension run earlier than kern…
Browse files Browse the repository at this point in the history
…el listener causes wrong manager retrieved. Now it will get manager dynamically.

minor update
  • Loading branch information
tom10271 authored and kayue committed Jun 15, 2017
1 parent ff3b763 commit 05c62da
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions Twig/Extension/WordpressExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ class WordpressExtension extends \Twig_Extension
*/
protected $managerRegistry;

/**
* @var WordpressEntityManager
*/
protected $manager;

/**
* @var ShortcodeChain
*/
Expand All @@ -46,7 +41,6 @@ public function __construct(
)
{
$this->managerRegistry = $managerRegistry;
$this->manager = $managerRegistry->getManager();
$this->shortcodeChain = $shortcodeChain;
$this->attachmentHelper = $attachmentHelper;
}
Expand All @@ -56,6 +50,15 @@ public function getName()
return "wordpress";
}


/**
* @return WordpressEntityManager
*/
protected function getManager()
{
return $this->managerRegistry->getManager();
}

public function getFilters()
{
return array(
Expand Down Expand Up @@ -113,7 +116,7 @@ public function findOneOptionBy($criteria)
];
}

return $this->manager->getRepository('KayueWordpressBundle:Option')->findOneBy($criteria);
return $this->getManager()->getRepository('KayueWordpressBundle:Option')->findOneBy($criteria);
}

/**
Expand All @@ -137,12 +140,12 @@ public function findOnePostBy($criteria)
'status' => 'publish',
]);

return $this->manager->getRepository('KayueWordpressBundle:Post')->findOneBy($criteria);
return $this->getManager()->getRepository('KayueWordpressBundle:Post')->findOneBy($criteria);
}

public function findPostMetasBy($criteria)
{
$repository = $this->manager->getRepository('KayueWordpressBundle:PostMeta');
$repository = $this->getManager()->getRepository('KayueWordpressBundle:PostMeta');

if (func_get_arg(0) instanceof Post && is_string(func_get_arg(1))) {
return $repository->getMetasByPost(func_get_arg(0), func_get_arg(1));
Expand All @@ -153,17 +156,17 @@ public function findPostMetasBy($criteria)

public function findCommentsByPost(Post $post)
{
return $this->manager->getRepository('KayueWordpressBundle:Comment')->findApproved($post);
return $this->getManager()->getRepository('KayueWordpressBundle:Comment')->findApproved($post);
}

public function findAttachmentsByPost(Post $post)
{
return $this->manager->getRepository('KayueWordpressBundle:Post')->findAttachmentsByPost($post);
return $this->getManager()->getRepository('KayueWordpressBundle:Post')->findAttachmentsByPost($post);
}

public function findOneAttachmentById($id)
{
return $this->manager->getRepository('KayueWordpressBundle:Post')->findAttachmentById($id);
return $this->getManager()->getRepository('KayueWordpressBundle:Post')->findAttachmentById($id);
}

public function findThumbnail(Post $post)
Expand Down Expand Up @@ -194,7 +197,7 @@ public function getPostFormatByPost(Post $post)

public function findTermsByPost(Post $post, $taxonomy = null)
{
return $this->manager->getRepository('KayueWordpressBundle:Term')->findByPost($post, $taxonomy);
return $this->getManager()->getRepository('KayueWordpressBundle:Term')->findByPost($post, $taxonomy);
}

public function findCategoriesByPost(Post $post)
Expand All @@ -220,7 +223,7 @@ public function findOneUserMetaBy($criteria)
];
}

return $this->manager->getRepository('KayueWordpressBundle:UserMeta')->findOneBy($criteria);
return $this->getManager()->getRepository('KayueWordpressBundle:UserMeta')->findOneBy($criteria);
}

public function findUserMetasBy($criteria)
Expand All @@ -231,7 +234,7 @@ public function findUserMetasBy($criteria)
];
}

return $this->manager->getRepository('KayueWordpressBundle:UserMeta')->findBy($criteria);
return $this->getManager()->getRepository('KayueWordpressBundle:UserMeta')->findBy($criteria);
}

/**
Expand Down

0 comments on commit 05c62da

Please sign in to comment.