-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ada279
commit 4abe9c9
Showing
4 changed files
with
94 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace DH\DoctrineAuditBundle\Twig\Extension; | ||
|
||
use Symfony\Bridge\Doctrine\RegistryInterface; | ||
|
||
class TwigExtension extends \Twig_Extension | ||
{ | ||
protected $doctrine; | ||
|
||
public function getFunctions() | ||
{ | ||
// Register the function in twig : | ||
// In your template you can use it as : {{findUser(123)}} | ||
return [ | ||
new \Twig_SimpleFunction('findUser', [$this, 'findUser']), | ||
new \Twig_SimpleFunction('class', [$this, 'getClass']), | ||
new \Twig_SimpleFunction('tablename', [$this, 'getTablename']), | ||
]; | ||
} | ||
|
||
public function __construct(RegistryInterface $doctrine) | ||
{ | ||
$this->doctrine = $doctrine; | ||
} | ||
|
||
public function findUser($id, $repository) | ||
{ | ||
$em = $this->doctrine->getManager(); | ||
$repo = $em->getRepository($repository); | ||
|
||
return $repo->find($id); | ||
} | ||
|
||
public function getClass($entity) | ||
{ | ||
return get_class($entity); | ||
} | ||
|
||
public function getTablename($entity) | ||
{ | ||
return $this | ||
->doctrine | ||
->getManager() | ||
->getClassMetadata(get_class($entity)) | ||
->table['name'] | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'twig_extensions'; | ||
} | ||
} |