/** * Freezes an object and storages it in the object storage. * * @param object $object The object that is to be stored. * * @return string */ public function store($object) { // Bail out if a non-object was passed. if (!is_object($object)) { throw Object_Freezer_Util::getInvalidArgumentException(1, 'object'); } $this->doStore($this->freezer->freeze($object)); return $object->__php_object_freezer_uuid; }
/** * 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; }
/** * freezes the user and returns it frozen * @return Array $frozenUser */ public function getAsFrozen() { $freezer = new Object_Freezer(); return $freezer->freeze($this->getFreezable()); }