commit 7d9ac74a2129e63be9516173dcb3cf905c834112 Author: Ruben Date: Thu Sep 1 11:15:17 2016 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c9a868b --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +vendor +composer.lock +.htaccess +cache/db.sqlite +config/* +!config/_default.php +.phpintel + diff --git a/README.md b/README.md new file mode 100644 index 0000000..c2afb6a --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Emotion Hero Web Services + +1. Website +2. API + +To run development server run `php -S localhost:80 -t www`. \ No newline at end of file diff --git a/bin/generate_db.php b/bin/generate_db.php new file mode 100644 index 0000000..a1d409b --- /dev/null +++ b/bin/generate_db.php @@ -0,0 +1,50 @@ +getConnection()->getDatabasePlatform()->getName()); +// $serializer = JMS\Serializer\SerializerBuilder::create()->build(); + +$metadatas = $em->getMetadataFactory()->getAllMetadata(); +// $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); +$destPath = $em->getConfiguration()->getProxyDir(); +$em->getProxyFactory()->generateProxyClasses($metadatas, $destPath); + + +$tool = new Doctrine\ORM\Tools\SchemaTool($em); + +$classes = $metadatas; + + +// $classes = array( +// $em->getClassMetadata('EmotionHero\Models\Emotion'), +// ); + +// $tool->getCreateDatabaseSQL(); +$queries = $tool->getCreateSchemaSQL($classes); +// $queries = $tool->getUpdateSchemaSql($classes); +foreach($queries as $sql){ echo "$sql;\n"; } +// updateSchema +$tool->updateSchema($classes); + + +$emotions = ['anger','contempt','disgust', 'fear', 'joy','sadness','surprise']; +foreach($emotions as $emo) { + $emotion = new EmotionHero\Models\Emotion(); + $emotion->setName($emo); + $em->persist($emotion); +} + +$user = new EmotionHero\Models\User(); +$em->persist($user); + +$em->flush(); + +var_dump($user->getId()); \ No newline at end of file diff --git a/bootstrap.php b/bootstrap.php new file mode 100644 index 0000000..c9f38c2 --- /dev/null +++ b/bootstrap.php @@ -0,0 +1,10 @@ +__initializer__ = $initializer; + $this->__cloner__ = $cloner; + } + + + + + + + + /** + * + * @return array + */ + public function __sleep() + { + if ($this->__isInitialized__) { + return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Emotion' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Emotion' . "\0" . 'name', '' . "\0" . 'EmotionHero\\Models\\Emotion' . "\0" . 'targets']; + } + + return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Emotion' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Emotion' . "\0" . 'name', '' . "\0" . 'EmotionHero\\Models\\Emotion' . "\0" . 'targets']; + } + + /** + * + */ + public function __wakeup() + { + if ( ! $this->__isInitialized__) { + $this->__initializer__ = function (Emotion $proxy) { + $proxy->__setInitializer(null); + $proxy->__setCloner(null); + + $existingProperties = get_object_vars($proxy); + + foreach ($proxy->__getLazyProperties() as $property => $defaultValue) { + if ( ! array_key_exists($property, $existingProperties)) { + $proxy->$property = $defaultValue; + } + } + }; + + } + } + + /** + * + */ + public function __clone() + { + $this->__cloner__ && $this->__cloner__->__invoke($this, '__clone', []); + } + + /** + * Forces initialization of the proxy + */ + public function __load() + { + $this->__initializer__ && $this->__initializer__->__invoke($this, '__load', []); + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __isInitialized() + { + return $this->__isInitialized__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setInitialized($initialized) + { + $this->__isInitialized__ = $initialized; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setInitializer(\Closure $initializer = null) + { + $this->__initializer__ = $initializer; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __getInitializer() + { + return $this->__initializer__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setCloner(\Closure $cloner = null) + { + $this->__cloner__ = $cloner; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific cloning logic + */ + public function __getCloner() + { + return $this->__cloner__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + * @static + */ + public function __getLazyProperties() + { + return self::$lazyPropertiesDefaults; + } + + + /** + * {@inheritDoc} + */ + public function getId() + { + if ($this->__isInitialized__ === false) { + return (int) parent::getId(); + } + + + $this->__initializer__ && $this->__initializer__->__invoke($this, 'getId', []); + + return parent::getId(); + } + + /** + * {@inheritDoc} + */ + public function getName() + { + + $this->__initializer__ && $this->__initializer__->__invoke($this, 'getName', []); + + return parent::getName(); + } + + /** + * {@inheritDoc} + */ + public function setName($name) + { + + $this->__initializer__ && $this->__initializer__->__invoke($this, 'setName', [$name]); + + return parent::setName($name); + } + + /** + * {@inheritDoc} + */ + public function getTargets() + { + + $this->__initializer__ && $this->__initializer__->__invoke($this, 'getTargets', []); + + return parent::getTargets(); + } + +} diff --git a/cache/proxy/__CG__EmotionHeroModelsGame.php b/cache/proxy/__CG__EmotionHeroModelsGame.php new file mode 100644 index 0000000..ebe1688 --- /dev/null +++ b/cache/proxy/__CG__EmotionHeroModelsGame.php @@ -0,0 +1,176 @@ +__initializer__ = $initializer; + $this->__cloner__ = $cloner; + } + + + + + + + + /** + * + * @return array + */ + public function __sleep() + { + if ($this->__isInitialized__) { + return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'user', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'level', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'hits', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'createdAt']; + } + + return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'user', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'level', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'hits', '' . "\0" . 'EmotionHero\\Models\\Game' . "\0" . 'createdAt']; + } + + /** + * + */ + public function __wakeup() + { + if ( ! $this->__isInitialized__) { + $this->__initializer__ = function (Game $proxy) { + $proxy->__setInitializer(null); + $proxy->__setCloner(null); + + $existingProperties = get_object_vars($proxy); + + foreach ($proxy->__getLazyProperties() as $property => $defaultValue) { + if ( ! array_key_exists($property, $existingProperties)) { + $proxy->$property = $defaultValue; + } + } + }; + + } + } + + /** + * + */ + public function __clone() + { + $this->__cloner__ && $this->__cloner__->__invoke($this, '__clone', []); + } + + /** + * Forces initialization of the proxy + */ + public function __load() + { + $this->__initializer__ && $this->__initializer__->__invoke($this, '__load', []); + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __isInitialized() + { + return $this->__isInitialized__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setInitialized($initialized) + { + $this->__isInitialized__ = $initialized; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setInitializer(\Closure $initializer = null) + { + $this->__initializer__ = $initializer; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __getInitializer() + { + return $this->__initializer__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setCloner(\Closure $cloner = null) + { + $this->__cloner__ = $cloner; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific cloning logic + */ + public function __getCloner() + { + return $this->__cloner__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + * @static + */ + public function __getLazyProperties() + { + return self::$lazyPropertiesDefaults; + } + + +} diff --git a/cache/proxy/__CG__EmotionHeroModelsHit.php b/cache/proxy/__CG__EmotionHeroModelsHit.php new file mode 100644 index 0000000..17fd455 --- /dev/null +++ b/cache/proxy/__CG__EmotionHeroModelsHit.php @@ -0,0 +1,176 @@ +__initializer__ = $initializer; + $this->__cloner__ = $cloner; + } + + + + + + + + /** + * + * @return array + */ + public function __sleep() + { + if ($this->__isInitialized__) { + return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Hit' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Hit' . "\0" . 'target', '' . "\0" . 'EmotionHero\\Models\\Hit' . "\0" . 'game', '' . "\0" . 'EmotionHero\\Models\\Hit' . "\0" . 'score', '' . "\0" . 'EmotionHero\\Models\\Hit' . "\0" . 'emotion', '' . "\0" . 'EmotionHero\\Models\\Hit' . "\0" . 'hits']; + } + + return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Hit' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Hit' . "\0" . 'target', '' . "\0" . 'EmotionHero\\Models\\Hit' . "\0" . 'game', '' . "\0" . 'EmotionHero\\Models\\Hit' . "\0" . 'score', '' . "\0" . 'EmotionHero\\Models\\Hit' . "\0" . 'emotion', '' . "\0" . 'EmotionHero\\Models\\Hit' . "\0" . 'hits']; + } + + /** + * + */ + public function __wakeup() + { + if ( ! $this->__isInitialized__) { + $this->__initializer__ = function (Hit $proxy) { + $proxy->__setInitializer(null); + $proxy->__setCloner(null); + + $existingProperties = get_object_vars($proxy); + + foreach ($proxy->__getLazyProperties() as $property => $defaultValue) { + if ( ! array_key_exists($property, $existingProperties)) { + $proxy->$property = $defaultValue; + } + } + }; + + } + } + + /** + * + */ + public function __clone() + { + $this->__cloner__ && $this->__cloner__->__invoke($this, '__clone', []); + } + + /** + * Forces initialization of the proxy + */ + public function __load() + { + $this->__initializer__ && $this->__initializer__->__invoke($this, '__load', []); + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __isInitialized() + { + return $this->__isInitialized__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setInitialized($initialized) + { + $this->__isInitialized__ = $initialized; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setInitializer(\Closure $initializer = null) + { + $this->__initializer__ = $initializer; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __getInitializer() + { + return $this->__initializer__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setCloner(\Closure $cloner = null) + { + $this->__cloner__ = $cloner; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific cloning logic + */ + public function __getCloner() + { + return $this->__cloner__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + * @static + */ + public function __getLazyProperties() + { + return self::$lazyPropertiesDefaults; + } + + +} diff --git a/cache/proxy/__CG__EmotionHeroModelsLevel.php b/cache/proxy/__CG__EmotionHeroModelsLevel.php new file mode 100644 index 0000000..e6a1ab5 --- /dev/null +++ b/cache/proxy/__CG__EmotionHeroModelsLevel.php @@ -0,0 +1,176 @@ +__initializer__ = $initializer; + $this->__cloner__ = $cloner; + } + + + + + + + + /** + * + * @return array + */ + public function __sleep() + { + if ($this->__isInitialized__) { + return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'targets', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'games']; + } + + return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'targets', '' . "\0" . 'EmotionHero\\Models\\Level' . "\0" . 'games']; + } + + /** + * + */ + public function __wakeup() + { + if ( ! $this->__isInitialized__) { + $this->__initializer__ = function (Level $proxy) { + $proxy->__setInitializer(null); + $proxy->__setCloner(null); + + $existingProperties = get_object_vars($proxy); + + foreach ($proxy->__getLazyProperties() as $property => $defaultValue) { + if ( ! array_key_exists($property, $existingProperties)) { + $proxy->$property = $defaultValue; + } + } + }; + + } + } + + /** + * + */ + public function __clone() + { + $this->__cloner__ && $this->__cloner__->__invoke($this, '__clone', []); + } + + /** + * Forces initialization of the proxy + */ + public function __load() + { + $this->__initializer__ && $this->__initializer__->__invoke($this, '__load', []); + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __isInitialized() + { + return $this->__isInitialized__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setInitialized($initialized) + { + $this->__isInitialized__ = $initialized; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setInitializer(\Closure $initializer = null) + { + $this->__initializer__ = $initializer; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __getInitializer() + { + return $this->__initializer__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setCloner(\Closure $cloner = null) + { + $this->__cloner__ = $cloner; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific cloning logic + */ + public function __getCloner() + { + return $this->__cloner__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + * @static + */ + public function __getLazyProperties() + { + return self::$lazyPropertiesDefaults; + } + + +} diff --git a/cache/proxy/__CG__EmotionHeroModelsTarget.php b/cache/proxy/__CG__EmotionHeroModelsTarget.php new file mode 100644 index 0000000..378d73d --- /dev/null +++ b/cache/proxy/__CG__EmotionHeroModelsTarget.php @@ -0,0 +1,176 @@ +__initializer__ = $initializer; + $this->__cloner__ = $cloner; + } + + + + + + + + /** + * + * @return array + */ + public function __sleep() + { + if ($this->__isInitialized__) { + return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'level', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'time', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'emotion', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'hits']; + } + + return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'level', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'time', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'emotion', '' . "\0" . 'EmotionHero\\Models\\Target' . "\0" . 'hits']; + } + + /** + * + */ + public function __wakeup() + { + if ( ! $this->__isInitialized__) { + $this->__initializer__ = function (Target $proxy) { + $proxy->__setInitializer(null); + $proxy->__setCloner(null); + + $existingProperties = get_object_vars($proxy); + + foreach ($proxy->__getLazyProperties() as $property => $defaultValue) { + if ( ! array_key_exists($property, $existingProperties)) { + $proxy->$property = $defaultValue; + } + } + }; + + } + } + + /** + * + */ + public function __clone() + { + $this->__cloner__ && $this->__cloner__->__invoke($this, '__clone', []); + } + + /** + * Forces initialization of the proxy + */ + public function __load() + { + $this->__initializer__ && $this->__initializer__->__invoke($this, '__load', []); + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __isInitialized() + { + return $this->__isInitialized__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setInitialized($initialized) + { + $this->__isInitialized__ = $initialized; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setInitializer(\Closure $initializer = null) + { + $this->__initializer__ = $initializer; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __getInitializer() + { + return $this->__initializer__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setCloner(\Closure $cloner = null) + { + $this->__cloner__ = $cloner; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific cloning logic + */ + public function __getCloner() + { + return $this->__cloner__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + * @static + */ + public function __getLazyProperties() + { + return self::$lazyPropertiesDefaults; + } + + +} diff --git a/cache/proxy/__CG__EmotionHeroModelsUser.php b/cache/proxy/__CG__EmotionHeroModelsUser.php new file mode 100644 index 0000000..e87e82e --- /dev/null +++ b/cache/proxy/__CG__EmotionHeroModelsUser.php @@ -0,0 +1,268 @@ +__initializer__ = $initializer; + $this->__cloner__ = $cloner; + } + + + + + + + + /** + * + * @return array + */ + public function __sleep() + { + if ($this->__isInitialized__) { + return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\User' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\User' . "\0" . 'games', 'createdAt']; + } + + return ['__isInitialized__', '' . "\0" . 'EmotionHero\\Models\\User' . "\0" . 'id', '' . "\0" . 'EmotionHero\\Models\\User' . "\0" . 'games', 'createdAt']; + } + + /** + * + */ + public function __wakeup() + { + if ( ! $this->__isInitialized__) { + $this->__initializer__ = function (User $proxy) { + $proxy->__setInitializer(null); + $proxy->__setCloner(null); + + $existingProperties = get_object_vars($proxy); + + foreach ($proxy->__getLazyProperties() as $property => $defaultValue) { + if ( ! array_key_exists($property, $existingProperties)) { + $proxy->$property = $defaultValue; + } + } + }; + + } + } + + /** + * + */ + public function __clone() + { + $this->__cloner__ && $this->__cloner__->__invoke($this, '__clone', []); + } + + /** + * Forces initialization of the proxy + */ + public function __load() + { + $this->__initializer__ && $this->__initializer__->__invoke($this, '__load', []); + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __isInitialized() + { + return $this->__isInitialized__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setInitialized($initialized) + { + $this->__isInitialized__ = $initialized; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setInitializer(\Closure $initializer = null) + { + $this->__initializer__ = $initializer; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __getInitializer() + { + return $this->__initializer__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + */ + public function __setCloner(\Closure $cloner = null) + { + $this->__cloner__ = $cloner; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific cloning logic + */ + public function __getCloner() + { + return $this->__cloner__; + } + + /** + * {@inheritDoc} + * @internal generated method: use only when explicitly handling proxy specific loading logic + * @static + */ + public function __getLazyProperties() + { + return self::$lazyPropertiesDefaults; + } + + + /** + * {@inheritDoc} + */ + public function setCreatedAt(\DateTime $createdAt) + { + + $this->__initializer__ && $this->__initializer__->__invoke($this, 'setCreatedAt', [$createdAt]); + + return parent::setCreatedAt($createdAt); + } + + /** + * {@inheritDoc} + */ + public function getCreatedAt() + { + + $this->__initializer__ && $this->__initializer__->__invoke($this, 'getCreatedAt', []); + + return parent::getCreatedAt(); + } + + /** + * {@inheritDoc} + */ + public function getRoles() + { + + $this->__initializer__ && $this->__initializer__->__invoke($this, 'getRoles', []); + + return parent::getRoles(); + } + + /** + * {@inheritDoc} + */ + public function getPassword() + { + + $this->__initializer__ && $this->__initializer__->__invoke($this, 'getPassword', []); + + return parent::getPassword(); + } + + /** + * {@inheritDoc} + */ + public function getSalt() + { + + $this->__initializer__ && $this->__initializer__->__invoke($this, 'getSalt', []); + + return parent::getSalt(); + } + + /** + * {@inheritDoc} + */ + public function getUsername() + { + + $this->__initializer__ && $this->__initializer__->__invoke($this, 'getUsername', []); + + return parent::getUsername(); + } + + /** + * {@inheritDoc} + */ + public function eraseCredentials() + { + + $this->__initializer__ && $this->__initializer__->__invoke($this, 'eraseCredentials', []); + + return parent::eraseCredentials(); + } + + /** + * {@inheritDoc} + */ + public function getId() + { + if ($this->__isInitialized__ === false) { + return parent::getId(); + } + + + $this->__initializer__ && $this->__initializer__->__invoke($this, 'getId', []); + + return parent::getId(); + } + +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..5447175 --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "emotionhero/emotionhero_api", + "description": "The API for the Emotion Hero game.", + "require": { + "silex/silex": "~2.0", + "doctrine/orm": "v2.5.4", + "beberlei/DoctrineExtensions": "^1.0", + "gedmo/doctrine-extensions": "^2.4", + "symfony/serializer": "^3.1", + "cnam/security-jwt-service-provider": "2.*", + "jms/serializer": "^1.3" + }, + "autoload": { + "psr-4": { + "EmotionHero\\": "src/" + } + } +} diff --git a/config/_default.php b/config/_default.php new file mode 100644 index 0000000..7334df2 --- /dev/null +++ b/config/_default.php @@ -0,0 +1,13 @@ + false, + 'db' => [ + 'dsn' => null, + 'username' => null, + 'password' => null + ] +]; + +return $config; \ No newline at end of file diff --git a/src/Api/Manager.php b/src/Api/Manager.php new file mode 100644 index 0000000..a814366 --- /dev/null +++ b/src/Api/Manager.php @@ -0,0 +1 @@ +_eh = EH::getInstance(); + } + + public function connect(Application $app) + { + // creates a new controller based on the default route + $controllers = $app['controllers_factory']; + + $controllers->get('/', function (Application $app) { + return "OK"; + }); + + $controllers->get('/levels', function (Application $app) { + $levels = $this->_eh->getEm()->getRepository(Models\Level::class)->findAll(); + return $app['serializer']->serialize($levels, 'json'); + }); + + $controllers->get('/emotions', function (Application $app) { + $levels = $this->_eh->getEm()->getRepository(Models\Emotion::class)->findAll(); + return $app['serializer']->serialize($levels, 'json'); + }); + + $controllers->get('/me', function (Application $app) { + $token = $app['security.token_storage']->getToken(); + $user = $token->getUser(); + return $app['serializer']->serialize($user, 'json'); + }); + + $controllers->post('/me/games', function (Request $request, Application $app) { + $vars = json_decode($request->getContent(), true); + var_dump($vars); + return $app['serializer']->serialize($levels, 'json'); + }); + + return $controllers; + } +} \ No newline at end of file diff --git a/src/Application.php b/src/Application.php new file mode 100644 index 0000000..fe781af --- /dev/null +++ b/src/Application.php @@ -0,0 +1,90 @@ +config = array_merge($default_config, $env_config); + } + private function __clone() {} + + public static function getInstance() { + if (!Application::$instance instanceof self) { + Application::$instance = new self(); + } + return Application::$instance; + } + + public function getConfig() { + return $this->config; + } + + public function getEm() { + + if(!$this->em) { + + $config = $this->getConfig(); + + $em_config = Setup::createAnnotationMetadataConfiguration([__DIR__.'/../src/'], $config['debug'], __DIR__ . '/../cache/proxy',null,false); + $em_config->addFilter('soft-deleteable', \Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter::class); + // TODO: make sure APCu is enabled (in test AND live!!) + // $em_config->setQueryCacheImpl(new \Doctrine\Common\Cache\ApcuCache()); + // $em_config->setResultCacheImpl(new \Doctrine\Common\Cache\ApcuCache()); + // $em_config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ApcuCache()); + + $connectionConfig = new Configuration(); + $connectionParams = array( + 'pdo' => new \PDO( + $config['db']['dsn'], + isSet($config['username']) ? $config['username'] : null, + isSet($config['password']) ? $config['password'] : null + ) + ); + + + if($config['debug']) + { + $doctrine_debug_stack = new DebugStack(); + $connectionConfig->setSQLLogger($doctrine_debug_stack); + } + + $conn = DriverManager::getConnection($connectionParams, $connectionConfig); + $platform = $conn->getDatabasePlatform(); + $platform->registerDoctrineTypeMapping('enum', 'string'); + Type::addType('utctime', EmotionHero\Tools\UTCDateTimeType::class); + $platform->registerDoctrineTypeMapping('datetime', 'utctime'); + + + // AnnotationRegistry::registerAutoloadNamespace("Hateoas\Configuration", __DIR__."/vendor/willdurand/hateoas/src/"); + AnnotationRegistry::registerAutoloadNamespace("JMS\Serializer", __DIR__."/../vendor/jms/serializer/src/"); + AnnotationRegistry::registerAutoloadNamespace("Gedmo\Mapping", __DIR__."/../vendor/gedmo/doctrine-extensions/lib/"); + + // obtaining the entity manager + $_em = EntityManager::create($conn, $em_config); + // Need to enable explicitly: https://github.com/Atlantic18/DoctrineExtensions/blob/master/doc/softdeleteable.md + $_em->getFilters()->enable('soft-deleteable'); + $_em->getEventManager()->addEventSubscriber(new \Gedmo\SoftDeleteable\SoftDeleteableListener()); + $_em->getEventManager()->addEventSubscriber(new \Gedmo\Timestampable\TimestampableListener()); + + $this->em = $_em; + } + + return $this->em; + } +} diff --git a/src/Models/Emotion.php b/src/Models/Emotion.php new file mode 100644 index 0000000..72fd6bf --- /dev/null +++ b/src/Models/Emotion.php @@ -0,0 +1,89 @@ +targets = new ArrayCollection(); + } + + /** + * Gets the value of id. + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * Gets the value of name. + * + * @return string + */ + public function getName() + { + return $this->name; + } + + /** + * Sets the value of name. + * + * @param string $name the name + * + * @return self + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * Gets the value of targets. + * + * @return \Doctrine\Common\Collections\Collection + */ + public function getTargets() + { + return $this->targets; + } + +} diff --git a/src/Models/Game.php b/src/Models/Game.php new file mode 100644 index 0000000..87e4edc --- /dev/null +++ b/src/Models/Game.php @@ -0,0 +1,54 @@ +_em->createQuery( + // 'SELECT ri FROM ...::class ri WHERE ri.result = :result AND ri.hitAt IS NULL ORDER BY ri.usedPosition ASC' + // ) + // ->setParameters([ + // 'result' => $result, + // ]) + // ->setMaxResults($limit); + // $resultItems = $query->getResult(); +} \ No newline at end of file diff --git a/src/Models/Hit.php b/src/Models/Hit.php new file mode 100644 index 0000000..1643392 --- /dev/null +++ b/src/Models/Hit.php @@ -0,0 +1,311 @@ +hits = new ArrayCollection(); + } +} diff --git a/src/Models/Level.php b/src/Models/Level.php new file mode 100644 index 0000000..0fdb21e --- /dev/null +++ b/src/Models/Level.php @@ -0,0 +1,43 @@ +targets = new ArrayCollection(); + $this->games = new ArrayCollection(); + } + +} diff --git a/src/Models/Target.php b/src/Models/Target.php new file mode 100644 index 0000000..08524c5 --- /dev/null +++ b/src/Models/Target.php @@ -0,0 +1,55 @@ +hits = new ArrayCollection(); + } +} diff --git a/src/Models/User.php b/src/Models/User.php new file mode 100644 index 0000000..f95bb29 --- /dev/null +++ b/src/Models/User.php @@ -0,0 +1,142 @@ +createdAt = $createdAt; + + return $this; + } + + /** + * Returns createdAt. + * + * @return \DateTime + */ + public function getCreatedAt() + { + return $this->createdAt; + } + + public function __construct() + { + $this->games = new ArrayCollection(); + } + + + + /** + * Returns the roles granted to the user. + * + * + * public function getRoles() + * { + * return array('ROLE_USER'); + * } + * + * + * Alternatively, the roles might be stored on a ``roles`` property, + * and populated in any number of different ways when the user object + * is created. + * + * @return (Role|string)[] The user roles + */ + public function getRoles() { + return ['ROLE_USER']; + } + + /** + * Returns the password used to authenticate the user. + * + * This should be the encoded password. On authentication, a plain-text + * password will be salted, encoded, and then compared to this value. + * + * @return string The password + */ + public function getPassword() { + return null; + } + + /** + * Returns the salt that was originally used to encode the password. + * + * This can return null if the password was not encoded using a salt. + * + * @return string|null The salt + */ + public function getSalt() { + return null; + } + + /** + * Returns the username used to authenticate the user. + * + * @return string The username + */ + public function getUsername() { + return $this->getId(); + } + + /** + * Removes sensitive data from the user. + * + * This is important if, at any given point, sensitive information like + * the plain-text password is stored on this object. + */ + public function eraseCredentials() { + + } + + /** + * Gets the value of id. + * + * @return integer + */ + public function getId() + { + return $this->id; + } +} diff --git a/src/Models/UserRepository.php b/src/Models/UserRepository.php new file mode 100644 index 0000000..2913093 --- /dev/null +++ b/src/Models/UserRepository.php @@ -0,0 +1,68 @@ +find($username); // username == $id field + if(!$user) { + throw new \Symfony\Component\Security\Core\Exception\UsernameNotFoundException("Invalid username"); + } + return $user; + } + + /** + * Refreshes the user for the account interface. + * + * It is up to the implementation to decide if the user data should be + * totally reloaded (e.g. from the database), or if the UserInterface + * object can just be merged into some internal array of users / identity + * map. + * + * @param UserInterface $user + * + * @return UserInterface + * + * @throws UnsupportedUserException if the account is not supported + */ + public function refreshUser(UserInterface $user) { + $this->_em->refresh($user); + } + + /** + * Whether this provider supports the given user class. + * + * @param string $class + * @todo Extending User should be possible + * @return bool + */ + public function supportsClass($class) { + return $class == User::class; + } + + // $query = $this->_em->createQuery( + // 'SELECT ri FROM ...::class ri WHERE ri.result = :result AND ri.hitAt IS NULL ORDER BY ri.usedPosition ASC' + // ) + // ->setParameters([ + // 'result' => $result, + // ]) + // ->setMaxResults($limit); + // $resultItems = $query->getResult(); +} \ No newline at end of file diff --git a/src/Tools/UTCDateTimeType.php b/src/Tools/UTCDateTimeType.php new file mode 100644 index 0000000..8fd3e2b --- /dev/null +++ b/src/Tools/UTCDateTimeType.php @@ -0,0 +1,45 @@ +setTimeZone(self::$utc); + + return $value->format($platform->getDateTimeFormatString()); + } + + public function convertToPHPValue($value, AbstractPlatform $platform) + { + if ($value === null) { + return null; + } + + if (is_null(self::$utc)) { + self::$utc = new \DateTimeZone('UTC'); + } + + $val = \DateTime::createFromFormat($platform->getDateTimeFormatString(), $value, self::$utc); + + if (!$val) { + throw ConversionException::conversionFailed($value, $this->getName()); + } + + return $val; + } +} \ No newline at end of file diff --git a/www/index.php b/www/index.php new file mode 100644 index 0000000..9e8d3d1 --- /dev/null +++ b/www/index.php @@ -0,0 +1,147 @@ + $eh->getConfig()['debug'] + ]); + +/* + * JWT setup +*/ + +define('USER_ID_FIELD', 'id'); + +$get_secret_key = function($payload){return 'AqeZFu4MWMZ=P2H_SMgS%%7'.$payload['uid'].'y9aas52%$^eQSQ%HQbYqU(fDo';}; + +$app['security.jwt'] = [ + 'secret_key' => 'Very_secret_key', + 'life_time' => 86400 * 365, + 'options' => [ + 'username_claim' => USER_ID_FIELD, // default name, option specifying claim containing username + 'header_name' => 'X-Access-Token', // default null, option for usage normal oauth2 header + 'token_prefix' => 'Bearer', + ] +]; + +$app['users'] = function () use ($eh) { + return $eh->getEm()->getRepository(EmotionHero\Models\User::class); +}; + +$app['serializer'] = function () use ($eh) { + return JMS\Serializer\SerializerBuilder::create()->build(); +}; + +$app['security.firewalls'] = array( + 'login' => [ + 'pattern' => 'login|register|oauth|token', + 'anonymous' => true, + ], + 'secured' => array( + 'pattern' => '^.*$', + 'logout' => array('logout_path' => '/logout'), + 'users' => $app['users'], + 'jwt' => array( + 'use_forward' => true, + 'require_previous_session' => false, + 'stateless' => true, + ) + ), +); + +$app->register(new Silex\Provider\SecurityServiceProvider()); +$app->register(new Silex\Provider\SecurityJWTServiceProvider()); + + + + +/** + * Get token for user + */ +$app->post('/api/register', function(Request $request) use ($app, $eh){ + // return $app['serializer']->serialize($eh->getEm()->getRepository(EmotionHero\Models\User::class)->findAll(), 'json'); + // validate user with... NOTING!!! + $user = new EmotionHero\Models\User(); + $eh->getEm()->persist($user); + $eh->getEm()->flush(); + return $app['serializer']->serialize($user, 'json'); +}); +/** + * Get token for user with UUID + * As it is already a generated token.. don't use password + */ +$app->post('/api/token', function(Request $request) use ($app){ + $vars = json_decode($request->getContent(), true); + try { + if (empty($vars['userid'])) { + throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $vars['userid'])); + } + + /** + * @var $user EmotionHero\Models\User + */ + $user = $app['users']->loadUserByUsername($vars['userid']); + + if (! $user) { + // if (! $app['security.encoder.digest']->isPasswordValid($user->getPassword(), $vars['password'], '')) { // no password set + throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $vars['userid'])); + } else { + $response = [ + 'success' => true, + 'token' => $app['security.jwt.encoder']->encode([USER_ID_FIELD => $user->getUsername()]), + ]; + } + } catch (UsernameNotFoundException $e) { + $response = [ + 'success' => false, + 'error' => 'Invalid credentials', + ]; + } + + return $app->json($response, ($response['success'] == true ? Response::HTTP_OK : Response::HTTP_BAD_REQUEST)); +}) +; +/* EXAMPLE +$app->get('/api/protected_resource', function() use ($app){ + $token = $app['security.token_storage']->getToken(); + $jwt = 'no'; + $token = $app['security.token_storage']->getToken(); + if ($token instanceof Silex\Component\Security\Http\Token\JWTToken) { + $jwt = 'yes'; + } + $granted = 'no'; + if($app['security.authorization_checker']->isGranted('ROLE_ADMIN')) { + $granted = 'yes'; + } + $granted_user = 'no'; + if($app['security.authorization_checker']->isGranted('ROLE_USER')) { + $granted_user = 'yes'; + } + $granted_super = 'no'; + if($app['security.authorization_checker']->isGranted('ROLE_SUPER_ADMIN')) { + $granted_super = 'yes'; + } + $user = $token->getUser(); + return $app->json([ + 'hello' => $token->getUsername(), + 'username' => $user->getUsername(), + 'auth' => $jwt, + 'granted' => $granted, + 'granted_user' => $granted_user, + 'granted_super' => $granted_super, + ]); +});*/ + + + +$app->mount('/', new EmotionHero\Api\ScoreControllerProvider()); + + + +$app->run(); \ No newline at end of file