diff --git a/composer.json b/composer.json index 642c632..3fc43fd 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ ], "require": { "php": ">=5.3.3", - "rhumsaa/uuid": "^2.7.3" + "rhumsaa/uuid": "^2.7.3", + "jms/serializer": "^1.0" }, "require-dev": { "phpunit/phpunit": "~4.2", diff --git a/composer.lock b/composer.lock index 662456d..2e65f48 100644 --- a/composer.lock +++ b/composer.lock @@ -4,49 +4,39 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "cecf26ca6df972363c1fb76cd9944193", + "hash": "4103fbd3bc95b45da0c8d28073f7db20", "packages": [ { - "name": "rhumsaa/uuid", - "version": "2.8.0", + "name": "doctrine/annotations", + "version": "v1.2.6", "source": { "type": "git", - "url": "https://github.com/ramsey/rhumsaa-uuid.git", - "reference": "cca98c652cac412c9c2f109c69e5532f313435fc" + "url": "https://github.com/doctrine/annotations.git", + "reference": "f4a91702ca3cd2e568c3736aa031ed00c3752af4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/ramsey/rhumsaa-uuid/zipball/cca98c652cac412c9c2f109c69e5532f313435fc", - "reference": "cca98c652cac412c9c2f109c69e5532f313435fc", + "url": "https://api.github.com/repos/doctrine/annotations/zipball/f4a91702ca3cd2e568c3736aa031ed00c3752af4", + "reference": "f4a91702ca3cd2e568c3736aa031ed00c3752af4", "shasum": "" }, "require": { - "php": ">=5.3.3" + "doctrine/lexer": "1.*", + "php": ">=5.3.2" }, "require-dev": { - "doctrine/dbal": ">=2.3", - "moontoast/math": "~1.1", - "phpunit/phpunit": "~4.1", - "satooshi/php-coveralls": "~0.6", - "symfony/console": "~2.3" + "doctrine/cache": "1.*", + "phpunit/phpunit": "4.*" }, - "suggest": { - "doctrine/dbal": "Allow the use of a UUID as doctrine field type.", - "moontoast/math": "Support for converting UUID to 128-bit integer (in string form).", - "symfony/console": "Support for use of the bin/uuid command line tool." - }, - "bin": [ - "bin/uuid" - ], "type": "library", "extra": { "branch-alias": { - "dev-master": "2.8.x-dev" + "dev-master": "1.3.x-dev" } }, "autoload": { - "psr-4": { - "Rhumsaa\\Uuid\\": "src/" + "psr-0": { + "Doctrine\\Common\\Annotations\\": "lib/" } }, "notification-url": "https://packagist.org/downloads/", @@ -55,25 +45,35 @@ ], "authors": [ { - "name": "Marijn Huizendveld", - "email": "marijn.huizendveld@gmail.com" + "name": "Roman Borschel", + "email": "roman@code-factory.org" }, { - "name": "Ben Ramsey", - "homepage": "http://benramsey.com" + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" } ], - "description": "A PHP 5.3+ library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID).", - "homepage": "https://github.com/ramsey/uuid", + "description": "Docblock Annotations Parser", + "homepage": "http://www.doctrine-project.org", "keywords": [ - "guid", - "identifier", - "uuid" + "annotations", + "docblock", + "parser" ], - "time": "2014-11-09 18:42:56" - } - ], - "packages-dev": [ + "time": "2015-06-17 12:21:22" + }, { "name": "doctrine/instantiator", "version": "1.0.5", @@ -128,6 +128,384 @@ ], "time": "2015-06-14 21:17:01" }, + { + "name": "doctrine/lexer", + "version": "v1.0.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/lexer.git", + "reference": "83893c552fd2045dd78aef794c31e694c37c0b8c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/lexer/zipball/83893c552fd2045dd78aef794c31e694c37c0b8c", + "reference": "83893c552fd2045dd78aef794c31e694c37c0b8c", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\Lexer\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "lexer", + "parser" + ], + "time": "2014-09-09 13:34:57" + }, + { + "name": "jms/metadata", + "version": "1.5.1", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/metadata.git", + "reference": "22b72455559a25777cfd28c4ffda81ff7639f353" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/metadata/zipball/22b72455559a25777cfd28c4ffda81ff7639f353", + "reference": "22b72455559a25777cfd28c4ffda81ff7639f353", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "doctrine/cache": "~1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.5.x-dev" + } + }, + "autoload": { + "psr-0": { + "Metadata\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache" + ], + "authors": [ + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "https://github.com/schmittjoh", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "Class/method/property metadata management in PHP", + "keywords": [ + "annotations", + "metadata", + "xml", + "yaml" + ], + "time": "2014-07-12 07:13:19" + }, + { + "name": "jms/parser-lib", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/parser-lib.git", + "reference": "c509473bc1b4866415627af0e1c6cc8ac97fa51d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/parser-lib/zipball/c509473bc1b4866415627af0e1c6cc8ac97fa51d", + "reference": "c509473bc1b4866415627af0e1c6cc8ac97fa51d", + "shasum": "" + }, + "require": { + "phpoption/phpoption": ">=0.9,<2.0-dev" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-0": { + "JMS\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache2" + ], + "description": "A library for easily creating recursive-descent parsers.", + "time": "2012-11-18 18:08:43" + }, + { + "name": "jms/serializer", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/serializer.git", + "reference": "a29d9a204efc3ca3f39a9d182a83fd34462fef3f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/serializer/zipball/a29d9a204efc3ca3f39a9d182a83fd34462fef3f", + "reference": "a29d9a204efc3ca3f39a9d182a83fd34462fef3f", + "shasum": "" + }, + "require": { + "doctrine/annotations": "1.*", + "doctrine/instantiator": "~1.0.3", + "jms/metadata": "~1.1", + "jms/parser-lib": "1.*", + "php": ">=5.4.0", + "phpcollection/phpcollection": "~0.1" + }, + "require-dev": { + "doctrine/orm": "~2.1", + "doctrine/phpcr-odm": "~1.0.1", + "jackalope/jackalope-doctrine-dbal": "1.0.*", + "phpunit/phpunit": "~4.0", + "propel/propel1": "~1.7", + "symfony/filesystem": "2.*", + "symfony/form": "~2.1", + "symfony/translation": "~2.0", + "symfony/validator": "~2.0", + "symfony/yaml": "2.*", + "twig/twig": ">=1.8,<2.0-dev" + }, + "suggest": { + "symfony/yaml": "Required if you'd like to serialize data to YAML format." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.17-dev" + } + }, + "autoload": { + "psr-0": { + "JMS\\Serializer": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache2" + ], + "authors": [ + { + "name": "Johannes M. Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "Library for (de-)serializing data of any complexity; supports XML, JSON, and YAML.", + "homepage": "http://jmsyst.com/libs/serializer", + "keywords": [ + "deserialization", + "jaxb", + "json", + "serialization", + "xml" + ], + "time": "2015-06-16 11:50:24" + }, + { + "name": "phpcollection/phpcollection", + "version": "0.4.0", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/php-collection.git", + "reference": "b8bf55a0a929ca43b01232b36719f176f86c7e83" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/php-collection/zipball/b8bf55a0a929ca43b01232b36719f176f86c7e83", + "reference": "b8bf55a0a929ca43b01232b36719f176f86c7e83", + "shasum": "" + }, + "require": { + "phpoption/phpoption": "1.*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.3-dev" + } + }, + "autoload": { + "psr-0": { + "PhpCollection": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache2" + ], + "authors": [ + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "https://github.com/schmittjoh", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "General-Purpose Collection Library for PHP", + "keywords": [ + "collection", + "list", + "map", + "sequence", + "set" + ], + "time": "2014-03-11 13:46:42" + }, + { + "name": "phpoption/phpoption", + "version": "1.4.0", + "source": { + "type": "git", + "url": "https://github.com/schmittjoh/php-option.git", + "reference": "5d099bcf0393908bf4ad69cc47dafb785d51f7f5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/schmittjoh/php-option/zipball/5d099bcf0393908bf4ad69cc47dafb785d51f7f5", + "reference": "5d099bcf0393908bf4ad69cc47dafb785d51f7f5", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.3-dev" + } + }, + "autoload": { + "psr-0": { + "PhpOption\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache2" + ], + "authors": [ + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "https://github.com/schmittjoh", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "Option Type for PHP", + "keywords": [ + "language", + "option", + "php", + "type" + ], + "time": "2014-01-09 22:37:17" + }, + { + "name": "rhumsaa/uuid", + "version": "2.8.0", + "source": { + "type": "git", + "url": "https://github.com/ramsey/rhumsaa-uuid.git", + "reference": "cca98c652cac412c9c2f109c69e5532f313435fc" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/ramsey/rhumsaa-uuid/zipball/cca98c652cac412c9c2f109c69e5532f313435fc", + "reference": "cca98c652cac412c9c2f109c69e5532f313435fc", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "doctrine/dbal": ">=2.3", + "moontoast/math": "~1.1", + "phpunit/phpunit": "~4.1", + "satooshi/php-coveralls": "~0.6", + "symfony/console": "~2.3" + }, + "suggest": { + "doctrine/dbal": "Allow the use of a UUID as doctrine field type.", + "moontoast/math": "Support for converting UUID to 128-bit integer (in string form).", + "symfony/console": "Support for use of the bin/uuid command line tool." + }, + "bin": [ + "bin/uuid" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.8.x-dev" + } + }, + "autoload": { + "psr-4": { + "Rhumsaa\\Uuid\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marijn Huizendveld", + "email": "marijn.huizendveld@gmail.com" + }, + { + "name": "Ben Ramsey", + "homepage": "http://benramsey.com" + } + ], + "description": "A PHP 5.3+ library for generating RFC 4122 version 1, 3, 4, and 5 universally unique identifiers (UUID).", + "homepage": "https://github.com/ramsey/uuid", + "keywords": [ + "guid", + "identifier", + "uuid" + ], + "time": "2014-11-09 18:42:56" + } + ], + "packages-dev": [ { "name": "hamcrest/hamcrest-php", "version": "v1.2.2", diff --git a/src/Link0/Profiler/PersistenceHandler.php b/src/Link0/Profiler/PersistenceHandler.php index a277eb7..4854f61 100644 --- a/src/Link0/Profiler/PersistenceHandler.php +++ b/src/Link0/Profiler/PersistenceHandler.php @@ -14,11 +14,6 @@ */ abstract class PersistenceHandler { - /** - * @var ProfileFactoryInterface - */ - private $profileFactory; - /** * @var SerializerInterface */ @@ -26,61 +21,36 @@ abstract class PersistenceHandler /** * Constructor + * + * @param null|SerializerInterface $serializer */ - public function __construct(ProfileFactoryInterface $profileFactory = null, SerializerInterface $serializer = null) + public function __construct(SerializerInterface $serializer = null) { - if($profileFactory === null) { + if($serializer === null) { $profileFactory = new ProfileFactory(); + $serializer = new Serializer($profileFactory); } - - if ($serializer === null) { - $serializer = new Serializer(); - } - - $this->profileFactory = $profileFactory; - $this->serializer = $serializer; - } - - /** - * @param ProfileFactoryInterface $profileFactoryInterface - */ - public function setProfileFactory(ProfileFactoryInterface $profileFactoryInterface) - { - $this->profileFactory = $profileFactoryInterface; - } - - /** - * @return ProfileFactoryInterface - */ - public function getProfileFactory() - { - return $this->profileFactory; - } - - /** - * @param SerializerInterface $serializer - */ - public function setSerializer(SerializerInterface $serializer) - { $this->serializer = $serializer; } /** - * @return SerializerInterface + * @param ProfileInterface $profile + * + * @return string */ - public function getSerializer() + protected function serialize(ProfileInterface $profile) { - return $this->serializer; + return $this->serializer->serialize($profile); } /** - * @param string $profileData + * @param string $content * * @return ProfileInterface */ - public function createProfileFromProfileData($profileData) + protected function unserialize($content) { - return $this->getProfileFactory()->fromArray($this->getSerializer()->unserialize($profileData)); + return $this->serializer->unserialize($content); } /** diff --git a/src/Link0/Profiler/PersistenceHandler/FilesystemHandler.php b/src/Link0/Profiler/PersistenceHandler/FilesystemHandler.php index 41c6b15..3534828 100644 --- a/src/Link0/Profiler/PersistenceHandler/FilesystemHandler.php +++ b/src/Link0/Profiler/PersistenceHandler/FilesystemHandler.php @@ -12,6 +12,7 @@ use Link0\Profiler\PersistenceHandler; use Link0\Profiler\PersistenceHandlerInterface; use Link0\Profiler\ProfileInterface; +use Link0\Profiler\SerializerInterface; /** * FilesystemHandler implementation for PersistenceHandler @@ -36,13 +37,18 @@ final class FilesystemHandler extends PersistenceHandler implements PersistenceH private $extension; /** - * @param FilesystemInterface $filesystem - * @param string $path OPTIONAL The path from the root given in the filesystem - * @param string $extension OPTIONAL The extension of the profile files + * @param FilesystemInterface $filesystem + * @param string $path OPTIONAL The path from the root given in the filesystem + * @param string $extension OPTIONAL The extension of the profile files + * @param null|SerializerInterface $serializer OPTIONAL Custom serializer */ - public function __construct(FilesystemInterface $filesystem, $path = '/', $extension = 'profile') - { - parent::__construct(); + public function __construct( + FilesystemInterface $filesystem, + $path = '/', + $extension = 'profile', + SerializerInterface $serializer = null + ) { + parent::__construct($serializer); $this->filesystem = $filesystem; $this->path = $path; @@ -117,7 +123,7 @@ public function retrieve($identifier) return null; } - return $this->createProfileFromProfileData($content); + return $this->unserialize($content); } /** @@ -127,10 +133,9 @@ public function retrieve($identifier) */ public function persist(ProfileInterface $profile) { - $serializer = $this->getSerializer(); $fullPath = $this->getFullPath($profile->getIdentifier()); - if ($this->getFilesystem()->put($fullPath, $serializer->serialize($profile->toArray())) === false) { + if ($this->getFilesystem()->put($fullPath, $this->serialize($profile)) === false) { throw new Exception('Unable to persist Profile[identifier=' . $profile->getIdentifier() . ']'); } diff --git a/src/Link0/Profiler/PersistenceHandler/MemoryHandler.php b/src/Link0/Profiler/PersistenceHandler/MemoryHandler.php index 597f183..dd10141 100644 --- a/src/Link0/Profiler/PersistenceHandler/MemoryHandler.php +++ b/src/Link0/Profiler/PersistenceHandler/MemoryHandler.php @@ -44,7 +44,7 @@ public function retrieve($identifier) return null; } - return $this->createProfileFromProfileData($this->state[$identifier]); + return $this->unserialize($this->state[$identifier]); } /** @@ -53,7 +53,7 @@ public function retrieve($identifier) */ public function persist(ProfileInterface $profile) { - $this->state[$profile->getIdentifier()] = $this->getSerializer()->serialize($profile->toArray()); + $this->state[$profile->getIdentifier()] = $this->serialize($profile); return $this; } diff --git a/src/Link0/Profiler/PersistenceHandler/MongoDbHandler.php b/src/Link0/Profiler/PersistenceHandler/MongoDbHandler.php index 638c24e..1af6c9e 100644 --- a/src/Link0/Profiler/PersistenceHandler/MongoDbHandler.php +++ b/src/Link0/Profiler/PersistenceHandler/MongoDbHandler.php @@ -11,6 +11,7 @@ use Link0\Profiler\PersistenceHandler\MongoDbHandler\MongoClientInterface; use Link0\Profiler\PersistenceHandlerInterface; use Link0\Profiler\ProfileInterface; +use Link0\Profiler\SerializerInterface; use MongoCollection; use MongoDate; use MongoDB; @@ -38,13 +39,18 @@ final class MongoDbHandler extends PersistenceHandler implements PersistenceHand private $collection; /** - * @param MongoClientInterface $client - * @param string $databaseName - * @param string $collection + * @param MongoClientInterface $client + * @param string $databaseName OPTIONAL + * @param string $collection OPTIONAL + * @param null|SerializerInterface $serializer OPTIONAL */ - public function __construct(MongoClientInterface $client, $databaseName = 'xhprof', $collection = 'results') - { - parent::__construct(); + public function __construct( + MongoClientInterface $client, + $databaseName = 'xhprof', + $collection = 'results', + SerializerInterface $serializer = null + ) { + parent::__construct($serializer); $this->client = $client; $this->database = $this->client->$databaseName; @@ -76,7 +82,7 @@ public function retrieve($identifier) ]); if($profileData !== null) { - return $this->createProfileFromProfileData($profileData['profile']); + return $this->unserialize($profileData['profile']); } return null; diff --git a/src/Link0/Profiler/PersistenceHandler/ZendDbHandler.php b/src/Link0/Profiler/PersistenceHandler/ZendDbHandler.php index 63e9e15..d2398b4 100644 --- a/src/Link0/Profiler/PersistenceHandler/ZendDbHandler.php +++ b/src/Link0/Profiler/PersistenceHandler/ZendDbHandler.php @@ -11,6 +11,7 @@ use Link0\Profiler\PersistenceHandler; use Link0\Profiler\PersistenceHandlerInterface; use Link0\Profiler\ProfileInterface; +use Link0\Profiler\SerializerInterface; use Zend\Db\Adapter\Adapter; use Zend\Db\Adapter\AdapterInterface as ZendDbAdapterInterface; use Zend\Db\Sql\Ddl\Column\BigInteger; @@ -50,11 +51,12 @@ final class ZendDbHandler extends PersistenceHandler implements PersistenceHandl private $dataColumn = 'data'; /** - * @param ZendDbAdapterInterface $adapter + * @param ZendDbAdapterInterface $adapter + * @param null|SerializerInterface $serializer OPTIONAL */ - public function __construct(ZendDbAdapterInterface $adapter) + public function __construct(ZendDbAdapterInterface $adapter, SerializerInterface $serializer = null) { - parent::__construct(); + parent::__construct($serializer); $this->sql = new Sql($adapter); } @@ -177,7 +179,7 @@ public function retrieve($identifier) return null; } - return $this->createProfileFromProfileData($data); + return $this->unserialize($data); } /** @@ -211,7 +213,7 @@ public function persist(ProfileInterface $profile) ->into($this->getTableName()) ->values(array( $this->getIdentifierColumn() => $profile->getIdentifier(), - $this->getDataColumn() => $this->getSerializer()->serialize($profile->toArray()), + $this->getDataColumn() => $this->serialize($profile), )); $sql->prepareStatementForSqlObject($insert)->execute(); diff --git a/src/Link0/Profiler/PersistenceService.php b/src/Link0/Profiler/PersistenceService.php index cf5e75f..2e820cc 100644 --- a/src/Link0/Profiler/PersistenceService.php +++ b/src/Link0/Profiler/PersistenceService.php @@ -26,7 +26,7 @@ final class PersistenceService public function __construct(PersistenceHandlerInterface $handler = null) { if ($handler === null) { - $handler = new NullHandler(); + $handler = new NullHandler(new Serializer(new ProfileFactory())); } $this->persistenceHandlers = array($handler); diff --git a/src/Link0/Profiler/ProfileFactory.php b/src/Link0/Profiler/ProfileFactory.php index 2b25c6c..657af04 100644 --- a/src/Link0/Profiler/ProfileFactory.php +++ b/src/Link0/Profiler/ProfileFactory.php @@ -14,6 +14,11 @@ */ final class ProfileFactory implements ProfileFactoryInterface { + public function getClassName() + { + return 'Link0\Profiler\Profile'; + } + /** * @param array $profileData * @param array $applicationData OPTIONAL @@ -30,14 +35,4 @@ public function create($profileData = array(), $applicationData = array(), $serv return $profile; } - - /** - * @param array $array - * - * @return ProfileInterface - */ - public function fromArray($array) - { - return Profile::fromArray($array); - } } diff --git a/src/Link0/Profiler/ProfileFactoryInterface.php b/src/Link0/Profiler/ProfileFactoryInterface.php index 645ca52..177428d 100644 --- a/src/Link0/Profiler/ProfileFactoryInterface.php +++ b/src/Link0/Profiler/ProfileFactoryInterface.php @@ -14,6 +14,11 @@ */ interface ProfileFactoryInterface { + /** + * @return string + */ + public function getClassName(); + /** * @param array $profileData OPTIONAL * @param array $applicationData OPTIONAL @@ -22,11 +27,4 @@ interface ProfileFactoryInterface * @return ProfileInterface */ public function create($profileData = array(), $applicationData = array(), $serverData = array()); - - /** - * @param array $array - * - * @return ProfileInterface - */ - public function fromArray($array); } diff --git a/src/Link0/Profiler/ProfileMetadataDriver.php b/src/Link0/Profiler/ProfileMetadataDriver.php new file mode 100644 index 0000000..7b14af0 --- /dev/null +++ b/src/Link0/Profiler/ProfileMetadataDriver.php @@ -0,0 +1,70 @@ +profileFactory = $profileFactory; + } + + /** + * @param \ReflectionClass $class + * + * @return ClassMetadata + * @throws Exception + */ + public function loadMetadataForClass(\ReflectionClass $class) + { + $className = $class->getName(); + $factoryClassName = $this->profileFactory->getClassName(); + + if($className !== $factoryClassName) { + throw new Exception("Class name '" . $className . "' does not match ProfileFactory for '" . $factoryClassName . "'"); + } + + $classMetadata = new ClassMetadata($class->getName()); + + foreach ($class->getProperties() as $reflectionProperty) { + $this->createMetadataProperty($class, $reflectionProperty, $classMetadata); + } + + return $classMetadata; + } + + /** + * @param \ReflectionClass $class + * @param \ReflectionProperty $reflectionProperty + * @param ClassMetadata $classMetadata + */ + private function createMetadataProperty(\ReflectionClass $class, \ReflectionProperty $reflectionProperty, ClassMetadata $classMetadata) + { + $propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName()); + + $propertyMetadata->setType('array'); + if ($reflectionProperty->getName() == 'identifier') { + $propertyMetadata->setType('string'); + } + + $classMetadata->addPropertyMetadata($propertyMetadata); + } +} diff --git a/src/Link0/Profiler/Serializer.php b/src/Link0/Profiler/Serializer.php index 8b2becb..c1206f0 100644 --- a/src/Link0/Profiler/Serializer.php +++ b/src/Link0/Profiler/Serializer.php @@ -5,6 +5,11 @@ */ namespace Link0\Profiler; +use JMS\Serializer\Builder\CallbackDriverFactory; +use JMS\Serializer\Exception\UnsupportedFormatException; +use JMS\Serializer\Serializer as JMSSerializer; +use JMS\Serializer\SerializerBuilder; + /** * Serializer implementation for PHPs serialize() and unserialize() functions * @@ -13,27 +18,69 @@ final class Serializer implements SerializerInterface { /** - * @param string $data + * @const string TYPE_JSON Type of serialization + */ + const TYPE_JSON = 'json'; + + /** + * @var ProfileFactoryInterface + */ + private $profileFactory; + + /** + * @var string + */ + private $type; + + /** + * @var JMSSerializer + */ + private $serializer; + + /** + * @param ProfileFactoryInterface $profileFactory + * @param string $type + */ + public function __construct(ProfileFactoryInterface $profileFactory, $type = self::TYPE_JSON) + { + $this->profileFactory = $profileFactory; + $this->type = $type; + + $this->serializer = SerializerBuilder::create() + ->setMetadataDriverFactory(new CallbackDriverFactory(function() use ($profileFactory) { + return new ProfileMetadataDriver($profileFactory); + })) + ->build(); + } + + /** + * @param ProfileInterface $profile + * * @return string + * @throws SerializerException */ - public function serialize($data) + public function serialize(ProfileInterface $profile) { - return serialize($data); + try { + return $this->serializer->serialize($profile, $this->type); + } catch(UnsupportedFormatException $ufe) { + throw new SerializerException($ufe->getMessage()); + } } /** * @param string $data * * @throws SerializerException - * @return string + * @return ProfileInterface */ public function unserialize($data) { - $object = @unserialize($data); - if($object === false) { - throw new SerializerException("Unable to unserialize data: " . $data); + try { + return $this->serializer->deserialize($data, $this->profileFactory->getClassName(), $this->type); + } catch(UnsupportedFormatException $ufe) { + throw new SerializerException($ufe->getMessage()); } - return $object; } } diff --git a/src/Link0/Profiler/SerializerInterface.php b/src/Link0/Profiler/SerializerInterface.php index 880c689..ba0cc5c 100644 --- a/src/Link0/Profiler/SerializerInterface.php +++ b/src/Link0/Profiler/SerializerInterface.php @@ -14,14 +14,14 @@ interface SerializerInterface { /** - * @param mixed $data + * @param ProfileInterface $profile * @return string */ - public function serialize($data); + public function serialize(ProfileInterface $profile); /** * @param string $data - * @return mixed + * @return ProfileInterface */ public function unserialize($data); } diff --git a/tests/Link0/Profiler/PersistenceHandler/FilesystemHandlerTest.php b/tests/Link0/Profiler/PersistenceHandler/FilesystemHandlerTest.php index 45000fc..159bcf9 100644 --- a/tests/Link0/Profiler/PersistenceHandler/FilesystemHandlerTest.php +++ b/tests/Link0/Profiler/PersistenceHandler/FilesystemHandlerTest.php @@ -9,6 +9,8 @@ use Exception; use Link0\Profiler\Profile; +use Link0\Profiler\ProfileFactory; +use Link0\Profiler\Serializer; use \Mockery as M; use Mockery; @@ -29,6 +31,11 @@ class FilesystemHandlerTest extends \PHPUnit_Framework_TestCase */ private $persistenceHandler; + /** + * @var Serializer + */ + private $serializer; + /** * Setup objects for all tests */ @@ -36,6 +43,8 @@ public function setUp() { $this->filesystem = Mockery::mock('\League\Flysystem\FilesystemInterface'); + $this->serializer = new Serializer(new ProfileFactory()); + $this->filesystem->shouldReceive('listFiles')->andReturn(array())->byDefault(); $this->filesystem->shouldReceive('put')->andReturn(true)->byDefault(); $this->filesystem->shouldReceive('read')->andReturn(false)->byDefault(); @@ -83,7 +92,8 @@ public function testPersist() ->withArgs(array( '/' . $profile->getIdentifier() . '.profile' )) - ->andReturn(serialize($profile->toArray())); + ->andReturn($this->serializer->serialize($profile)); + $this->filesystem->shouldReceive('listFiles')->andReturn(array( array( diff --git a/tests/Link0/Profiler/PersistenceHandler/MongoDbHandlerTest.php b/tests/Link0/Profiler/PersistenceHandler/MongoDbHandlerTest.php index 47f142c..4621b72 100644 --- a/tests/Link0/Profiler/PersistenceHandler/MongoDbHandlerTest.php +++ b/tests/Link0/Profiler/PersistenceHandler/MongoDbHandlerTest.php @@ -8,7 +8,10 @@ namespace Link0\Profiler\PersistenceHandler; use Link0\Profiler\Profile; +use Link0\Profiler\ProfileFactory; +use Link0\Profiler\Serializer; use Mockery; +use MongoCollection; /** * MongoDbHandlerTest @@ -27,6 +30,11 @@ class MongoDbHandlerTest extends \PHPUnit_Framework_TestCase */ private $mongoCollection; + /** + * @var Serializer + */ + private $serializer; + public function setUp() { $traversable = new \ArrayObject(); @@ -42,7 +50,9 @@ public function setUp() $client = Mockery::mock('\Link0\Profiler\PersistenceHandler\MongoDbHandler\MongoClientInterface'); $client->foo = $mongoDb; - $this->handler = new MongoDbHandler($client, 'foo', 'bar'); + $this->serializer = new Serializer(new ProfileFactory()); + + $this->handler = new MongoDbHandler($client, 'foo', 'bar', $this->serializer); } public function testEmptyList() @@ -61,7 +71,7 @@ public function testRetrieveReturnsProfile() $profile = Profile::create(); $this->mongoCollection->shouldReceive('findOne')->once()->andReturn(array( 'identifier' => $profile->getIdentifier(), - 'profile' => serialize($profile->toArray()), + 'profile' => $this->serializer->serialize($profile), )); $this->assertInstanceOf('\Link0\Profiler\Profile', $this->handler->retrieve('Foo')); } diff --git a/tests/Link0/Profiler/PersistenceHandler/ZendDbHandlerTest.php b/tests/Link0/Profiler/PersistenceHandler/ZendDbHandlerTest.php index 940a8f5..c391c4e 100644 --- a/tests/Link0/Profiler/PersistenceHandler/ZendDbHandlerTest.php +++ b/tests/Link0/Profiler/PersistenceHandler/ZendDbHandlerTest.php @@ -8,6 +8,9 @@ namespace Link0\Profiler\PersistenceHandler; use Link0\Profiler\Profile; +use Link0\Profiler\ProfileFactory; +use Link0\Profiler\Serializer; +use Link0\Profiler\SerializerInterface; use Mockery as M; use Zend\Db\Adapter\Driver\StatementInterface; use Zend\Db\Sql\Sql; @@ -29,6 +32,11 @@ class ZendDbHandlerTest extends \PHPUnit_Framework_TestCase */ private $statement; + /** + * @var SerializerInterface + */ + private $serializer; + /** * Setup mocks */ @@ -73,9 +81,11 @@ public function setUp() ->once() ->andReturn(); + $this->serializer = new Serializer(new ProfileFactory()); + // Initializing objects $sql = new Sql($adapter, null, $adapterAbstractPlatform); - $this->handler = new ZendDbHandler($adapter); + $this->handler = new ZendDbHandler($adapter, $this->serializer); $this->handler->setSql($sql); } @@ -150,7 +160,7 @@ public function testRetrieveObject() { $profile = Profile::create(); $resultInterface = new \ArrayIterator(array( - array('identifier' => $profile->getIdentifier(), 'data' => serialize($profile->toArray())), + array('identifier' => $profile->getIdentifier(), 'data' => $this->serializer->serialize($profile)), )); $this->statement->shouldReceive('execute') diff --git a/tests/Link0/Profiler/PersistenceServiceTest.php b/tests/Link0/Profiler/PersistenceServiceTest.php index 65c506c..0cf936e 100644 --- a/tests/Link0/Profiler/PersistenceServiceTest.php +++ b/tests/Link0/Profiler/PersistenceServiceTest.php @@ -31,7 +31,6 @@ class PersistenceServiceTest extends \PHPUnit_Framework_TestCase public function setUp() { $this->persistenceHandler = new PersistenceHandler\MemoryHandler(); - $this->persistenceHandler->setSerializer(new Serializer()); $this->persistenceService = new PersistenceService($this->persistenceHandler); } @@ -73,9 +72,8 @@ public function testListOperationOnHandler() public function testPersistAndRetrievePrimary() { $persistenceHandler = new PersistenceHandler\MemoryHandler(); - $persistenceHandler->setProfileFactory(new ProfileFactory()); - $profile = $persistenceHandler->getProfileFactory()->create(); + $profile = (new ProfileFactory())->create(); $this->persistenceService->addPersistenceHandler($persistenceHandler); $this->persistenceService->persist($profile); $this->assertEquals($profile, $this->persistenceService->retrieve($profile->getIdentifier())); diff --git a/tests/Link0/Profiler/ProfileTest.php b/tests/Link0/Profiler/ProfileTest.php index 3f76b5d..001b7a4 100644 --- a/tests/Link0/Profiler/ProfileTest.php +++ b/tests/Link0/Profiler/ProfileTest.php @@ -33,4 +33,15 @@ public function testSetIdentifier() $this->assertEquals('foo', $profile->getIdentifier()); } + public function testFactoryMethodFromArray() + { + $profile = Profile::fromArray([ + 'identifier' => '', + 'profileData' => [], + 'applicationData' => [], + 'serverData' => [], + ]); + $this->assertInstanceOf('\Link0\Profiler\Profile', $profile); + } + } \ No newline at end of file diff --git a/tests/Link0/Profiler/SerializerTest.php b/tests/Link0/Profiler/SerializerTest.php index fd91c97..dd5bd6e 100644 --- a/tests/Link0/Profiler/SerializerTest.php +++ b/tests/Link0/Profiler/SerializerTest.php @@ -7,6 +7,8 @@ */ namespace Link0\Profiler; +use Link0\Profiler\Exception; + /** * Class SerializerTest * @@ -14,6 +16,11 @@ */ class SerializerTest extends \PHPUnit_Framework_TestCase { + /** + * @var ProfileFactory + */ + private $profileFactory; + /** * @var Serializer */ @@ -24,26 +31,161 @@ class SerializerTest extends \PHPUnit_Framework_TestCase */ public function setUp() { - $this->serializer = new Serializer(); + $this->profileFactory = new ProfileFactory(); + $this->serializer = new Serializer($this->profileFactory); } public function testSerialization() { - $foo = array( - 'foo' => 'bar', - ); + $profile = $this->profileFactory->create([ + + ]); - $serializedData = $this->serializer->serialize($foo); + $serializedData = $this->serializer->serialize($profile); $unserializedData = $this->serializer->unserialize($serializedData); - $this->assertEquals($foo, $unserializedData); + $this->assertEquals($profile, $unserializedData); } /** * @expectedException \Link0\Profiler\SerializerException - * @expectedExceptionMessage Unable to unserialize data: FDSAfoobar + * @expectedExceptionMessage The format "foobar" is not supported for serialization. + */ + public function testUnsupportedFormatForSerializeThrowsException() + { + $serializer = new Serializer(new ProfileFactory(), 'foobar'); + $serializer->serialize(Profile::create()); + } + + /** + * @expectedException \Link0\Profiler\SerializerException + * @expectedExceptionMessage The format "foobar" is not supported for deserialization. + */ + public function testUnsupportedFormatForUnserializeThrowsException() + { + $serializer = new Serializer(new ProfileFactory(), 'foobar'); + $serializer->unserialize('{}'); + } + + /** + * @expectedException Exception + * @expectedExceptionMessage Class name 'Link0\Profiler\FakeClass' does not match ProfileFactory for 'Link0\Profiler\Profile' + */ + public function testUnserializeClassMismatchWithFactory() + { + $serializer = new Serializer(new ProfileFactory()); + $serializer->serialize(new FakeClass()); + } + + /** + * @expectedException \JMS\Serializer\Exception\RuntimeException + * @expectedExceptionMessage Could not decode JSON, syntax error - malformed JSON. */ public function testUnexpectedDataToThrowException() { $this->serializer->unserialize('FDSAfoobar'); } -} \ No newline at end of file +} + +class FakeClass implements ProfileInterface { + /** + * @param string $identifier + * + * @return ProfileInterface $this + */ + public function setIdentifier($identifier) + { + // TODO: Implement setIdentifier() method. + } + + /** + * @return string $identifier + */ + public function getIdentifier() + { + // TODO: Implement getIdentifier() method. + } + + /** + * @param array $serverData + * + * @return ProfileInterface $this + */ + public function setServerData($serverData) + { + // TODO: Implement setServerData() method. + } + + /** + * @return array $serverData + */ + public function getServerData() + { + // TODO: Implement getServerData() method. + } + + /** + * @param array $applicationData + * + * @return ProfileInterface $this + */ + public function setApplicationData($applicationData) + { + // TODO: Implement setApplicationData() method. + } + + /** + * @return array $applicationData + */ + public function getApplicationData() + { + // TODO: Implement getApplicationData() method. + } + + /** + * @param array $profileData + * + * @return ProfileInterface $this + */ + public function setProfileData($profileData) + { + // TODO: Implement setProfileData() method. + } + + /** + * @return array $profileData + */ + public function getProfileData() + { + // TODO: Implement getProfileData() method. + } + + /** + * Returns an array representation of this object. + * This array representation is also used for persistence. + * + * @return array + */ + public function toArray() + { + // TODO: Implement toArray() method. + } + + /** + * @param array $arrayData + * + * @return ProfileInterface $profile + */ + public static function fromArray($arrayData) + { + // TODO: Implement fromArray() method. + } + + /** + * @param string|null $identifier + * + * @return ProfileInterface + */ + public static function create($identifier = null) + { + // TODO: Implement create() method. + }} \ No newline at end of file