diff --git a/Classes/Command/SequenceCommandController.php b/Classes/Command/SequenceCommandController.php new file mode 100644 index 0000000..ebea1f6 --- /dev/null +++ b/Classes/Command/SequenceCommandController.php @@ -0,0 +1,33 @@ +sequenceGenerator->advanceTo($to, $type); + } + + // TODO: make clean up job to delete all but the biggest number to save resources +} diff --git a/Classes/DigiComp/Sequence/Command/SequenceCommandController.php b/Classes/DigiComp/Sequence/Command/SequenceCommandController.php deleted file mode 100644 index 4f55521..0000000 --- a/Classes/DigiComp/Sequence/Command/SequenceCommandController.php +++ /dev/null @@ -1,38 +0,0 @@ -sequenceGenerator->advanceTo($to, $type); - } - - //TODO: make clean up job to delete all but the biggest number to save resources -} diff --git a/Classes/DigiComp/Sequence/Service/Exception.php b/Classes/DigiComp/Sequence/Service/Exception.php deleted file mode 100644 index 6b21953..0000000 --- a/Classes/DigiComp/Sequence/Service/Exception.php +++ /dev/null @@ -1,17 +0,0 @@ -setType($type); $this->setNumber($number); + $this->setType($type); + } + + /** + * @return int + */ + public function getNumber() + { + return $this->number; } /** @@ -53,11 +55,11 @@ class Insert } /** - * @return int + * @return string */ - public function getNumber() + public function getType() { - return $this->number; + return $this->type; } /** @@ -70,12 +72,4 @@ class Insert } $this->type = $type; } - - /** - * @return string - */ - public function getType() - { - return $this->type; - } } diff --git a/Classes/Service/Exception.php b/Classes/Service/Exception.php new file mode 100644 index 0000000..0a46197 --- /dev/null +++ b/Classes/Service/Exception.php @@ -0,0 +1,12 @@ + 1 we could return new keys immediately for this * request, as we "reserved" the space between. * @@ -20,21 +19,20 @@ use TYPO3\Flow\Annotations as Flow; */ class SequenceGenerator { - /** - * @var \Doctrine\Common\Persistence\ObjectManager + * @var ObjectManager * @Flow\Inject */ protected $entityManager; /** - * @var \TYPO3\Flow\Reflection\ReflectionService + * @var ReflectionService * @Flow\Inject */ protected $reflectionService; /** - * @var \TYPO3\Flow\Log\SystemLoggerInterface + * @var SystemLoggerInterface * @Flow\Inject */ protected $systemLogger; @@ -42,7 +40,8 @@ class SequenceGenerator /** * @param string|object $type * - * @throws \DigiComp\Sequence\Service\Exception + * @throws Exception + * * @return int */ public function getNextNumberFor($type) @@ -50,18 +49,25 @@ class SequenceGenerator $type = $this->inferTypeFromSource($type); $count = $this->getLastNumberFor($type); - //TODO: Check for maximal tries, or similar - //TODO: Let increment be configurable per type + // TODO: Check for maximal tries, or similar + // TODO: Let increment be configurable per type do { - $count = $count + 1; - } while (!$this->validateFreeNumber($count, $type)); + $count++; + } while (! $this->validateFreeNumber($count, $type)); + return $count; } + /** + * @param int $count + * @param string|object $type + * + * @return bool + */ protected function validateFreeNumber($count, $type) { + /** @var $em EntityManager */ $em = $this->entityManager; - /** @var $em \Doctrine\ORM\EntityManager */ try { $em->getConnection()->insert( 'digicomp_sequence_domain_model_insert', @@ -73,18 +79,27 @@ class SequenceGenerator } catch (DBALException $e) { if ($e->getPrevious() && $e->getPrevious() instanceof \PDOException) { // Do nothing, new Doctrine handling hides the above error - } else { + } + else { $this->systemLogger->logException($e); } } catch (\Exception $e) { $this->systemLogger->logException($e); } + return false; } + /** + * @param int $to + * @param string|object $type + * + * @return bool + */ public function advanceTo($to, $type) { $type = $this->inferTypeFromSource($type); + return ($this->validateFreeNumber($to, $type)); } @@ -95,16 +110,16 @@ class SequenceGenerator */ public function getLastNumberFor($type) { - $type = $this->inferTypeFromSource($type); - /** @var $em \Doctrine\ORM\EntityManager */ + /** @var $em EntityManager */ $em = $this->entityManager; $result = $em->getConnection()->executeQuery( 'SELECT MAX(number) AS count FROM digicomp_sequence_domain_model_insert WHERE type=:type', - ['type' => $type] + ['type' => $this->inferTypeFromSource($type)] ); $count = $result->fetchAll(); $count = $count[0]['count']; + return $count; } @@ -112,15 +127,18 @@ class SequenceGenerator * @param string|object $stringOrObject * * @throws Exception + * * @return string */ - protected function inferTypeFromSource($stringOrObject) { + protected function inferTypeFromSource($stringOrObject) + { if (is_object($stringOrObject)) { - $stringOrObject = $this->reflectionService->getClassNameByObject($stringOrObject); + $stringOrObject = TypeHandling::getTypeForValue($stringOrObject); } - if (!$stringOrObject) { + if (! $stringOrObject) { throw new Exception('No Type given'); } + return $stringOrObject; } } diff --git a/Configuration/Testing/Settings.yaml b/Configuration/Testing/Settings.yaml index b72d1a0..54d2680 100644 --- a/Configuration/Testing/Settings.yaml +++ b/Configuration/Testing/Settings.yaml @@ -1,6 +1,6 @@ -TYPO3: +Neos: Flow: persistence: backendOptions: driver: 'pdo_sqlite' - path: %FLOW_PATH_DATA%/Temporary/testing.db \ No newline at end of file + path: '%FLOW_PATH_DATA%/Temporary/testing.db' diff --git a/Migrations/Mysql/Version20140505093853.php b/Migrations/Mysql/Version20140505093853.php index ad24715..d003d3a 100644 --- a/Migrations/Mysql/Version20140505093853.php +++ b/Migrations/Mysql/Version20140505093853.php @@ -1,5 +1,5 @@ assertEquals(0, $number); $this->assertEquals(1, $sequenceGenerator->getNextNumberFor($sequenceGenerator)); - $pids = []; + $pIds = []; for ($i = 0; $i < 10; $i++) { - $pid = pcntl_fork(); - if ($pid) { - $pids[] = $pid; - } else { + $pId = pcntl_fork(); + if ($pId) { + $pIds[] = $pId; + } + else { for ($j = 0; $j < 10; $j++) { $sequenceGenerator->getNextNumberFor($sequenceGenerator); } @@ -33,10 +36,12 @@ class SequenceTest extends FunctionalTestCase exit; } } - foreach ($pids as $pid) { + + foreach ($pIds as $pId) { $status = 0; - pcntl_waitpid($pid, $status); + pcntl_waitpid($pId, $status); } + $this->assertEquals(101, $sequenceGenerator->getLastNumberFor($sequenceGenerator)); } diff --git a/composer.json b/composer.json index d849b12..d0a7d63 100644 --- a/composer.json +++ b/composer.json @@ -1,50 +1,71 @@ { - "name": "digicomp/sequence", - "type": "typo3-flow-package", - "description": "Sequence is a very simple database agnostic but database based sequence generator", - "keywords": ["flow", "neos", "doctrine", "sequence"], - "authors": [ - { - "name": "Ferdinand Kuhl", - "email": "f.kuhl@digital-competence.de", - "homepage": "http://www.digital-competence.de", - "role": "Developer" - } - ], - "license": "MIT", - "homepage": "https://github.com/digicomp/DigiComp.Sequence", - "require": { - "typo3/flow": "~2.0|~3.0" - }, - "require-dev": { - "phpunit/phpunit": "3.7.*", - "ext-pcntl": "*" - }, - "autoload": { - "psr-0": { - "DigiComp\\Sequence": "Classes" - } - }, - "extra": { - "branch-alias": { - "dev-master": "1.1.x-dev" + "name": "digicomp/sequence", + "type": "neos-package", + "description": "Sequence is a very simple database agnostic but database based sequence generator", + "keywords": [ + "flow", + "neos", + "doctrine", + "sequence" + ], + "authors": [ + { + "name": "Ferdinand Kuhl", + "email": "f.kuhl@digital-competence.de", + "homepage": "http://www.digital-competence.de", + "role": "Developer" + } + ], + "license": "MIT", + "homepage": "https://github.com/digicomp/DigiComp.Sequence", + "require": { + "neos/flow": "~4.1" }, - "applied-flow-migrations": [ - "Inwebs.Basket-201409170938", - "TYPO3.FLOW3-201201261636", - "TYPO3.Fluid-201205031303", - "TYPO3.FLOW3-201205292145", - "TYPO3.FLOW3-201206271128", - "TYPO3.FLOW3-201209201112", - "TYPO3.Flow-201209251426", - "TYPO3.Flow-201211151101", - "TYPO3.Flow-201212051340", - "TYPO3.Flow-201310031523", - "TYPO3.Flow-201405111147", - "TYPO3.Fluid-20141113120800", - "TYPO3.Flow-20141113121400", - "TYPO3.Fluid-20141121091700", - "TYPO3.Fluid-20150214130800" - ] - } -} \ No newline at end of file + "require-dev": { + "phpunit/phpunit": "3.7.*", + "ext-pcntl": "*" + }, + "autoload": { + "psr-4": { + "DigiComp\\Sequence\\": "Classes" + } + }, + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev", + "dev-feature/neos-flow4": "2.0.x-dev" + }, + "applied-flow-migrations": [ + "Inwebs.Basket-201409170938", + "TYPO3.FLOW3-201201261636", + "TYPO3.Fluid-201205031303", + "TYPO3.FLOW3-201205292145", + "TYPO3.FLOW3-201206271128", + "TYPO3.FLOW3-201209201112", + "TYPO3.Flow-201209251426", + "TYPO3.Flow-201211151101", + "TYPO3.Flow-201212051340", + "TYPO3.Flow-201310031523", + "TYPO3.Flow-201405111147", + "TYPO3.Fluid-20141113120800", + "TYPO3.Flow-20141113121400", + "TYPO3.Fluid-20141121091700", + "TYPO3.Fluid-20150214130800", + "TYPO3.Flow-20151113161300", + "TYPO3.Flow-20161115140400", + "TYPO3.Flow-20161115140430", + "Neos.Flow-20161124204700", + "Neos.Flow-20161124204701", + "Neos.Flow-20161124224015", + "Neos.Eel-20161124230101", + "Neos.Imagine-20161124231742", + "Neos.Media-20161124233100", + "Neos.Flow-20161125124112", + "Neos.SwiftMailer-20161130105617", + "TYPO3.FluidAdaptor-20161130112935", + "Neos.Media-20161219094126", + "Neos.Flow-20170125103800", + "Neos.Flow-20170127183102" + ] + } +}