Initial commit
This commit is contained in:
commit
6b2c36c65d
6 changed files with 240 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
.svn
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
namespace DigiComp\Sequence\Command;
|
||||
|
||||
/* *
|
||||
* This script belongs to the FLOW3 package "DigiComp.Sequence". *
|
||||
* *
|
||||
* */
|
||||
|
||||
use Doctrine\DBAL\DBALException;
|
||||
use TYPO3\Flow\Annotations as Flow;
|
||||
|
||||
/**
|
||||
* A SequenceNumber generator (should be DB-agnostic)
|
||||
*
|
||||
* @Flow\Scope("singleton")
|
||||
*/
|
||||
class SequenceCommandController extends \TYPO3\Flow\Cli\CommandController {
|
||||
|
||||
/**
|
||||
* @var \DigiComp\Sequence\Service\SequenceGenerator
|
||||
* @Flow\Inject
|
||||
*/
|
||||
protected $sequenceGenerator;
|
||||
|
||||
/**
|
||||
* Sets minimum number for sequence generator
|
||||
*
|
||||
* @param int $to
|
||||
* @param string $type
|
||||
*/
|
||||
public function advanceCommand($to, $type) {
|
||||
$this->sequenceGenerator->advanceTo($to, $type);
|
||||
}
|
||||
}
|
73
Classes/DigiComp/Sequence/Domain/Model/Insert.php
Normal file
73
Classes/DigiComp/Sequence/Domain/Model/Insert.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
namespace DigiComp\Sequence\Domain\Model;
|
||||
|
||||
/* *
|
||||
* This script belongs to the FLOW3 package "DigiComp.Sequence". *
|
||||
* *
|
||||
* */
|
||||
|
||||
use TYPO3\Flow\Annotations as Flow;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
/**
|
||||
* SequenceInsert
|
||||
*
|
||||
* @author fcool
|
||||
* @Flow\Scope("prototype")
|
||||
* @Flow\Entity
|
||||
*/
|
||||
class Insert {
|
||||
|
||||
/**
|
||||
* @var int
|
||||
* @ORM\Id
|
||||
* @Flow\Identity
|
||||
*/
|
||||
protected $number;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
* @ORM\Id
|
||||
* @Flow\Identity
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* @param int $number
|
||||
* @param string $type
|
||||
*/
|
||||
public function __construct($number, $type) {
|
||||
$this->setType($type);
|
||||
$this->setNumber($number);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $number
|
||||
*/
|
||||
public function setNumber($number) {
|
||||
$this->number = $number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getNumber() {
|
||||
return $this->number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|object $type
|
||||
*/
|
||||
public function setType($type) {
|
||||
if (is_object($type)) {
|
||||
$type = get_class($type);
|
||||
}
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType() {
|
||||
return $this->type;
|
||||
}
|
||||
}
|
15
Classes/DigiComp/Sequence/Service/Exception.php
Normal file
15
Classes/DigiComp/Sequence/Service/Exception.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
namespace DigiComp\Sequence\Service;
|
||||
|
||||
/* *
|
||||
* This script belongs to the FLOW3 package "DigiComp.Sequence". *
|
||||
* *
|
||||
* */
|
||||
|
||||
use TYPO3\Flow\Annotations as Flow;
|
||||
/**
|
||||
* SequenceException
|
||||
*/
|
||||
class Exception extends \Exception {
|
||||
|
||||
}
|
96
Classes/DigiComp/Sequence/Service/SequenceGenerator.php
Normal file
96
Classes/DigiComp/Sequence/Service/SequenceGenerator.php
Normal file
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
namespace DigiComp\Sequence\Service;
|
||||
|
||||
/* *
|
||||
* This script belongs to the FLOW3 package "DigiComp.Sequence". *
|
||||
* *
|
||||
* */
|
||||
|
||||
use Doctrine\DBAL\DBALException;
|
||||
use TYPO3\Flow\Annotations as Flow;
|
||||
|
||||
/**
|
||||
* A SequenceNumber generator (should be DB-agnostic)
|
||||
*
|
||||
* @Flow\Scope("singleton")
|
||||
*/
|
||||
class SequenceGenerator {
|
||||
|
||||
/**
|
||||
* @var \Doctrine\Common\Persistence\ObjectManager
|
||||
* @Flow\Inject
|
||||
*/
|
||||
protected $_em;
|
||||
|
||||
/**
|
||||
* @var \TYPO3\Flow\Reflection\ReflectionService
|
||||
* @Flow\Inject
|
||||
*/
|
||||
protected $reflectionService;
|
||||
|
||||
/**
|
||||
* @var \TYPO3\Flow\Log\SystemLoggerInterface
|
||||
* @Flow\Inject
|
||||
*/
|
||||
protected $systemLogger;
|
||||
|
||||
/**
|
||||
* @param string|object $type
|
||||
* @throws \DigiComp\Sequence\Service\Exception
|
||||
* @return int
|
||||
*/
|
||||
public function getNextNumberFor($type) {
|
||||
if (is_object($type)) {
|
||||
$type = $this->reflectionService->getClassNameByObject($type);
|
||||
}
|
||||
if (!$type) {
|
||||
throw new Exception('No Type given');
|
||||
}
|
||||
$count = $this->getLastNumberFor($type);
|
||||
|
||||
//TODO: Check for maximal tries, or similar
|
||||
do {
|
||||
$count = $count+1;
|
||||
} while (! $this->validateFreeNumber($count, $type));
|
||||
return $count;
|
||||
}
|
||||
|
||||
protected function validateFreeNumber($count, $type) {
|
||||
$em = $this->_em;
|
||||
/** @var $em \Doctrine\ORM\EntityManager */
|
||||
try {
|
||||
$em->getConnection()->insert('digicomp_sequence_domain_model_insert', array('number' => $count, 'type' => $type));
|
||||
return true;
|
||||
} catch (\PDOException $e) {
|
||||
return false;
|
||||
} catch (DBALException $e) {
|
||||
if ($e->getPrevious() && $e->getPrevious() instanceof \PDOException) {
|
||||
// Do nothing, new Doctrine handling hides the above error
|
||||
} else {
|
||||
$this->systemLogger->logException($e);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->systemLogger->logException($e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function advanceTo($to, $type) {
|
||||
return ($this->validateFreeNumber($to, $type));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $type
|
||||
* @return int
|
||||
*/
|
||||
public function getLastNumberFor($type) {
|
||||
/** @var $em \Doctrine\ORM\EntityManager */
|
||||
$em = $this->_em;
|
||||
|
||||
$result = $em->getConnection()->executeQuery('SELECT MAX(number) AS count FROM digicomp_sequence_domain_model_insert WHERE type=:type', array('type' => $type));
|
||||
$count = $result->fetchAll();
|
||||
$count = $count[0]['count'];
|
||||
return $count;
|
||||
}
|
||||
|
||||
}
|
21
composer.json
Normal file
21
composer.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"name":"digicomp/sequence",
|
||||
"type":"typo3-flow-package",
|
||||
"description":"",
|
||||
"require":{
|
||||
"typo3/flow":"~2.0"
|
||||
},
|
||||
"require-dev":{
|
||||
"phpunit/phpunit": "3.7.*"
|
||||
},
|
||||
"autoload":{
|
||||
"psr-0":{
|
||||
"DigiComp\\Sequence":"Classes"
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.0.x-dev"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue