Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f0dc9e7e9a
Fetching contributors…

Cannot retrieve contributors at this time

61 lines (56 sloc) 1.761 kb
<?php
namespace Aspect;
use Ding\Aspect\MethodInvocation;
use Ding\Logger\ILoggerAware;
/**
* @Aspect
*/
class Transactional implements ILoggerAware
{
/**
* @Resource
* @var \Doctrine\ORM\EntityRepository
*/
protected $entityManager;
protected static $count = 0;
protected $logger;
public function setLogger(\Logger $logger)
{
$this->logger = $logger;
}
/**
* @MethodInterceptor(class-expression=AbstractService,expression=.*)
*/
public function manageTransaction(MethodInvocation $methodInvocation)
{
$method = $methodInvocation->getMethod();
$class = $methodInvocation->getClass();
if ((strncmp($method, '_', 1) === 0) || (strncmp($method, 'set', 3) === 0)) {
return $methodInvocation->proceed();
}
$this->logger->debug("Serving for: $class::$method");
self::$count++;
try
{
if (self::$count === 1) {
$this->logger->debug('Beginning transaction');
$this->entityManager->beginTransaction();
}
$result = $methodInvocation->proceed();
if (self::$count === 1) {
$this->logger->debug('Commiting transaction');
$this->entityManager->flush();
$this->entityManager->commit();
}
self::$count--;
return $result;
} catch(\Exception $exception) {
if (self::$count === 1) {
$this->logger->error('Exception: ' . $exception->getMessage() . ' occurred, rollbacking transaction');
$this->entityManager->rollback();
}
self::$count--;
throw $exception;
}
}
}
Jump to Line
Something went wrong with that request. Please try again.