Beispiel #1
0
 /**
  * Fetches a frozen object from the object storage and thaws it.
  *
  * @param string $id The ID of the object that is to be fetched.
  *
  * @return object
  */
 public function fetch($id)
 {
     // Bail out if a non-string was passed.
     if (!is_string($id)) {
         throw Object_Freezer_Util::getInvalidArgumentException(1, 'string');
     }
     // Try to retrieve object from the object cache.
     $object = $this->cache->get($id);
     if (!$object) {
         // Retrieve object from the object storage.
         $frozenObject = $this->doFetch($id);
         $this->fetchArray($frozenObject['objects'][$id]['state']);
         $object = $this->freezer->thaw($frozenObject);
         // Put object into the object cache.
         $this->cache->put($id, $object);
     }
     return $object;
 }
Beispiel #2
0
 /**
  * Exports current content of Doozr_Di_Collection ($this->collection) to a JSON-File.
  *
  * This method is intend to write current content of Doozr_Di_Collection ($this->collection) to a JSON-File.
  *
  * @param bool $exportInstances TRUE to export instances as well, otherwise FALSE to do not
  *
  * @author Benjamin Carl <*****@*****.**>
  *
  * @return bool TRUE on success, otherwise FALSE
  *
  * @throws Doozr_Di_Exception
  */
 public function export($exportInstances = true)
 {
     // check for collection
     if (!$this->collection) {
         throw new Doozr_Di_Exception('Could not import map. No collection set. Please set a collection first.');
     }
     // check for input
     if (!$this->output) {
         throw new Doozr_Di_Exception('Could not export map. No output file set. Please set output first.');
     }
     // check if input directory exists and if it is writable
     if (!is_dir(dirname($this->output)) || !is_writable(dirname($this->output))) {
         throw new Doozr_Di_Exception(sprintf('Could not export map. Output directory "%s" does not exist or isn\'t writable.', Dirname($this->output)));
     }
     /*
     "Foo": {
         "arguments": ["I R Baboon!"],
         "dependencies": [
             {
                 "id": "Database1",
                 "className": "Database",
                 "arguments": ["foo", "bar", "baz"],
                 "instance": null,
                 "configuration": {
                     "type": "constructor"
                 }
             },
             {
                 "id": "Logger1",
                 "className": "Logger",
                 "instance": null,
                 "configuration": {
                     "type": "method",
                     "value": "setLogging"
                 }
             }
         ]
     }
     */
     // get instance of the freezer
     $freezer = new Object_Freezer();
     // the collection for export in correct JSON structure
     $collection = [];
     // iterate over collection
     foreach ($this->collection as $className => $dependencies) {
         // collect dependencies for $className in an array
         $collection[$className] = new stdClass();
         // check for arguments
         $this->collection->getArguments($className) ? $collection[$className]->arguments = $this->collection->getArguments($className) : null;
         // check for custom arguments
         $this->collection->getConstructor($className) ? $collection[$className]->constructor = $this->collection->getConstructor($className) : null;
         // iterate over existing dependencies, encrypt to JSON structure and store temporary in $collection[]
         foreach ($dependencies as $count => $dependency) {
             /* @var $dependency Doozr_Di_Dependency */
             // temp object for storage
             $tmp = new stdClass();
             // the target
             $tmp->target = $dependency->getTarget();
             // the className
             $tmp->className = $dependency->getClassName();
             // the arguments
             if ($dependency->getArguments()) {
                 $tmp->arguments = $dependency->getArguments();
             }
             // the instance
             if ($exportInstances === true) {
                 if (is_object($dependency->getInstance())) {
                     $tmp->instance = serialize($freezer->freeze($dependency->getInstance()));
                 } else {
                     $tmp->instance = $dependency->getInstance();
                 }
             } else {
                 $tmp->instance = null;
             }
             // the configuration
             $tmp->config = $dependency->getConfiguration();
             // store created object to $collection
             $collection[$className]->dependencies[] = $tmp;
         }
     }
     // create tmp object for JSON export
     $output = new stdClass();
     // set collection as output for our map
     $output->map = [$collection];
     // write content to file
     $this->writeFile($this->output, json_encode($output));
     // success
     return true;
 }
 /**
  * @covers Object_Freezer::__construct
  * @covers Object_Freezer::setIdGenerator
  * @covers Object_Freezer::getIdGenerator
  * @covers Object_Freezer::setHashGenerator
  * @covers Object_Freezer::getHashGenerator
  * @covers Object_Freezer::setBlacklist
  * @covers Object_Freezer::getBlacklist
  * @covers Object_Freezer::setUseAutoload
  * @covers Object_Freezer::getUseAutoload
  */
 public function testConstructorWithDefaultArguments()
 {
     $freezer = new Object_Freezer();
     $this->assertType('Object_Freezer_IdGenerator_UUID', $freezer->getIdGenerator());
     $this->assertType('Object_Freezer_HashGenerator_NonRecursiveSHA1', $freezer->getHashGenerator());
     $this->assertEquals(array(), $freezer->getBlacklist());
     $this->assertTrue($freezer->getUseAutoload());
 }
Beispiel #4
0
 /**
  * thaws a frozen user
  * @param Array $frozenUser
  * @return User $user returns User Object or null if user could not be thawed
  */
 public static function thaw($frozenUser)
 {
     $thawedUser = null;
     if (is_array($frozenUser)) {
         $freezer = new Object_Freezer();
         $thawedUser = $freezer->thaw($frozenUser);
         if ($thawedUser instanceof User) {
             $thawedUser->getResource()->getById($thawedUser->getId());
             //TODO: do we need to reinitialize parent's resources?
         } else {
             $thawedUser = null;
             Logger::error("could not thaw user");
         }
     }
     return $thawedUser;
 }