Permalink
Browse files

Initial import of EbutikMongoSessionBundle

  • Loading branch information...
0 parents commit f46dcfe794e6e9210ff34fc6f4e463edec89a468 @magnusnordlander magnusnordlander committed Sep 13, 2011
@@ -0,0 +1 @@
+.DS_Store
@@ -0,0 +1,22 @@
+<?php
+
+namespace Ebutik\MongoSessionBundle\DependencyInjection;
+
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
+use Symfony\Component\HttpKernel\DependencyInjection\Extension;
+use Symfony\Component\Config\FileLocator;
+
+class EbutikMongoSessionExtension extends Extension
+{
+ public function load(array $configs, ContainerBuilder $container)
+ {
+ $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
+ $loader->load('config.yml');
+ }
+
+ public function getAlias()
+ {
+ return 'ebutik_mongo_session';
+ }
+}
@@ -0,0 +1,48 @@
+<?php
+
+namespace Ebutik\MongoSessionBundle\Document;
+
+use Ebutik\MongoSessionBundle\Interfaces\SessionEmbeddable;
+
+/**
+ *
+ * @mongodb:EmbeddedDocument
+ * @author Magnus Nordlander
+ */
+class EmbeddableSessionAttributeWrapper
+{
+ /**
+ * @mongodb:String
+ */
+ protected $key;
+
+ /**
+ * @mongodb:EmbedOne
+ */
+ protected $attribute;
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ public function __construct($key, SessionEmbeddable $attribute)
+ {
+ $this->key = $key;
+ $this->attribute = $attribute;
+ }
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ public function getKey()
+ {
+ return $this->key;
+ }
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ public function getAttribute()
+ {
+ return $this->attribute;
+ }
+}
@@ -0,0 +1,251 @@
+<?php
+
+namespace Ebutik\MongoSessionBundle\Document;
+
+use Doctrine\Common\Collections\ArrayCollection;
+
+use Ebutik\MongoSessionBundle\Interfaces\SessionEmbeddable;
+
+/**
+ *
+ * @mongodb:Document(repositoryClass="Ebutik\MongoSessionBundle\Repository\SessionRepository")
+ * @mongodb:HasLifecycleCallbacks
+ * @author Magnus Nordlander
+ */
+class Session
+{
+ /**
+ * @mongodb:Id(strategy="NONE")
+ */
+ protected $id;
+
+ /**
+ * @mongodb:Field(type="date")
+ */
+ protected $created_at;
+
+ /**
+ * @mongodb:Field(type="date")
+ */
+ protected $accessed_at;
+
+ /**
+ * @mongodb:Hash
+ */
+ protected $scalar_attributes = array();
+
+ /**
+ * @mongodb:EmbedMany(targetDocument="Ebutik\MongoSessionBundle\Document\EmbeddableSessionAttributeWrapper")
+ */
+ protected $embeddable_attributes;
+
+ /**
+ * @mongodb:Hash
+ */
+ protected $serialized_attributes = array();
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ public function __construct()
+ {
+ $this->generateId();
+ $this->embeddable_attributes = new ArrayCollection;
+ $this->created_at = new \DateTime();
+ }
+
+ /**
+ * @author Magnus Nordlander
+ * @mongodb:PostLoad
+ **/
+ public function updateAccessTime()
+ {
+ $this->accessed_at = new \DateTime();
+ }
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ private function generateId()
+ {
+ $this->id = mt_rand().uniqid();
+ }
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ protected function findEmbeddableAttributeWrapper($key)
+ {
+ foreach ($this->embeddable_attributes as $wrapper)
+ {
+ if ($wrapper->getKey() == $key)
+ {
+ return $wrapper;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ public function getId()
+ {
+ return $this->id;
+ }
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ public function read($key)
+ {
+ if (isset($this->scalar_attributes[$key]))
+ {
+ return $this->scalar_attributes[$key];
+ }
+ else if (isset($this->serialized_attributes[$key]))
+ {
+ return unserialize($this->scalar_attributes[$key]);
+ }
+ else if ($wrapper = $this->findEmbeddableAttributeWrapper($key))
+ {
+ return $wrapper->getAttribute();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ public function readAll()
+ {
+ return array_merge(
+ $this->scalar_attributes,
+ array_map('unserialize', $this->serialized_attributes),
+ $this->getEmbeddableAttributeArray()
+ );
+ }
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ protected function getEmbeddableAttributeArray()
+ {
+ $out = array();
+ foreach ($this->embeddable_attributes as $wrapper)
+ {
+ $out[$wrapper->getKey()] = $wrapper->getAttribute();
+ }
+
+ return $out;
+ }
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ public function remove($key)
+ {
+ $retval = null;
+
+ if (isset($this->scalar_attributes[$key]))
+ {
+ $retval = $this->scalar_attributes[$key];
+ unset($this->scalar_attributes[$key]);
+ }
+ else if (isset($this->serialized_attributes[$key]))
+ {
+ $retval = unserialize($this->serialized_attributes[$key]);
+ unset($this->serialized_attributes[$key]);
+ }
+ else if ($wrapper = $this->findEmbeddableAttributeWrapper($key))
+ {
+ $retval = $wrapper->getAttribute();
+ $this->embeddable_attributes->removeElement($wrapper);
+ }
+ else
+ {
+ return null;
+ }
+
+ return $retval;
+ }
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ public function write($key, $data)
+ {
+ $this->remove($key);
+
+ if ($data instanceOf SessionEmbeddable)
+ {
+ $this->embeddable_attributes->add(new EmbeddableSessionAttributeWrapper($key, $data));
+ }
+ else if (is_scalar($data))
+ {
+ $this->scalar_attributes[$key] = $data;
+ }
+ else if (is_object($data))
+ {
+ $this->serialized_attributes[$key] = serialize($data);
+ }
+ else if (is_array($data))
+ {
+ if (self::arrayOnlyContainsScalarsRecursive($data))
+ {
+ $this->scalar_attributes[$key] = $data;
+ }
+ else
+ {
+ $this->serialized_attributes[$key] = serialize($data);
+ }
+ }
+ else
+ {
+ throw new \RuntimeError("Data of type ".gettype($data)." cannot be saved in the session");
+ }
+ }
+
+ /**
+ * @author Magnus Nordlander
+ * @see http://www.doctrine-project.org/docs/orm/2.0/en/cookbook/implementing-wakeup-or-clone.html
+ **/
+ public function __clone()
+ {
+ // If the entity has an identity, proceed as normal.
+ if ($this->id)
+ {
+ $this->generateId();
+ }
+ // otherwise do nothing, do NOT throw an exception!
+ }
+
+ /**
+ * @author Magnus Nordlander
+ **/
+ static protected function arrayOnlyContainsScalarsRecursive(array $array)
+ {
+ $callback = function($reduced, $item) use (&$callback)
+ {
+ if ($reduced == false)
+ {
+ return false;
+ }
+ else if (is_array($item))
+ {
+ return array_reduce($item, $callback, true);
+ }
+ else if (is_scalar($item))
+ {
+ return true;
+ }
+
+ return false;
+ };
+
+ return array_reduce($array, $callback, true);
+ }
+}
@@ -0,0 +1,10 @@
+<?php
+
+namespace Ebutik\MongoSessionBundle;
+
+use Symfony\Component\HttpKernel\Bundle\Bundle;
+
+class EbutikMongoSessionBundle extends Bundle
+{
+
+}
@@ -0,0 +1,8 @@
+<?php
+
+namespace Ebutik\MongoSessionBundle\Interfaces;
+
+interface SessionEmbeddable
+{
+
+}
Oops, something went wrong.

0 comments on commit f46dcfe

Please sign in to comment.