Exemple #1
0
 public function _render(array $vars = array(), $return = false)
 {
     $this->context = $vars;
     extract($vars);
     if ($return) {
         ob_start();
     }
     echo "<?php\n\nnamespace ";
     echo trim($namespace, '\\') . ";\n\nuse MongoClient;\nuse ActiveMongo2\\Connection;\nuse Notoj\\Annotation\\Annotation;\nuse Notoj\\Annotation\\Annotations;\nuse Notoj;\n\n";
     foreach ($collections->byName() as $name => $class) {
         $this->context['name'] = $name;
         $this->context['class'] = $class;
         echo "    define(";
         var_export('C' . $name);
         echo ", ";
         var_export($class['class']);
         echo ");\n";
     }
     echo "\n";
     $instance = '_' . uniqid(true);
     $this->context['instance'] = $instance;
     echo "\nclass Mapper\n{\n    protected \$mapper = ";
     echo crodas\FileUtil\dump_array($collections->byName(), true) . ";\n    protected \$class_mapper = ";
     echo crodas\FileUtil\dump_array($collections->byClass(), true) . ";\n    protected \$class_files =  ";
     var_export($collections->autoload());
     echo ";\n    protected static \$loaded = array();\n    protected \$connection;\n    protected \$connections;\n    protected \$class_connections = ";
     var_export($collections->byConnection());
     echo ";\n    protected \$ns = array();\n    protected \$ns_by_name = array();\n\n    public function __construct(Connection \$conn)\n    {\n        \$this->connection = \$conn;\n        spl_autoload_register(array(\$this, '__autoloader'));\n    }\n\n    public function setDatabases(Array \$conns, Array \$ns)\n    {\n        \$this->connections = \$conns;\n\n        \$this->ns = \$ns;\n        foreach (\$this->class_connections as \$class => \$conn) {\n            \$ns =\"\";\n            if (!empty(\$this->ns[\$conn])) {\n                \$ns = \$this->ns[\$conn];\n            }\n            if (!empty(\$this->class_mapper[\$class])) {\n                \$this->ns_by_name[ \$this->class_mapper[\$class]['name'] ] = \$ns;\n            }\n        }\n\n        return \$this;\n    }\n\n    public function getRelativePath(\$object, \$dir)\n    {\n        if (\$dir[0] == '/') {\n            return \$dir;\n        }\n        \$info = \$this->mapClass(\$object);\n        return \$info['dir'] . \"/\" . \$dir;\n    }\n\n    protected function array_diff(Array \$arr1, Array \$arr2)\n    {\n        \$diff = array();\n        foreach (\$arr1 as \$key => \$value) {\n            if (empty(\$arr2[\$key]) || \$arr2[\$key] !== \$arr1[\$key]) {\n                \$diff[\$key] = \$value;\n            }\n        }\n        return \$diff;\n    }\n\n    public function getCollections()\n    {\n        return array(\n";
     foreach ($collections as $collection) {
         $this->context['collection'] = $collection;
         if ($collection->getName()) {
             echo "                " . $collection->getClassCode() . " => \$this->ns_by_name[";
             var_export($collection->getName());
             echo "]. ";
             var_export($collection->getName());
             echo ",\n";
         }
     }
     echo "        );\n    }\n\n    public function __autoloader(\$class)\n    {\n        \$class = strtolower(\$class);\n        if (!empty(\$this->class_files[\$class])) {\n            self::\$loaded[\$this->class_files[\$class]] = true;\n            require \$this->class_files[\$class];\n\n            return true;\n        }\n        return false;\n    }\n\n    public function getCollectionObject(\$col)\n    {\n        if (!is_scalar(\$col) || empty(\$this->mapper[\$col])) {\n            \$data = \$this->mapClass(\$col);     \n        } else {\n            \$data = \$this->mapper[\$col];\n        }\n\n        if (empty(self::\$loaded[\$data['file']])) {\n            if (!\$data['verify'](\$data['class'], false)) {\n                require \$data['file'];\n            }\n            self::\$loaded[\$data['file']] = true;\n        }\n\n        \$data['name'] = \$this->ns_by_name[\$data['name']] . \$data['name'];\n\n        \$conn = \$this->class_connections[\$data['class']];\n\n        if (empty(\$this->connections[\$conn])) {\n            throw new \\RuntimeException(\"Cannot find connection \$conn. We have \" . print_r(array_keys(\$this->connections), true));\n        }\n\n        \$db = \$this->connections[\$conn];\n        if (!empty(\$data['is_gridfs'])) {\n            \$col = \$db->getGridFs(\$data['name']);\n        } else {\n            \$col = \$db->selectCollection(\$data['name']);\n        }\n\n        return [\$col, \$data['class']];\n    }\n\n    public function mapCollection(\$col)\n    {\n        \$col = strtolower(\$col);\n        if (empty(\$this->mapper[\$col])) {\n            throw new \\RuntimeException(\"Cannot map {\$col} collection to its class\");\n        }\n\n        \$data = \$this->mapper[\$col];\n\n        if (empty(self::\$loaded[\$data['file']])) {\n            if (!\$data['verify'](\$data['class'], false)) {\n                require \$data['file'];\n            }\n            self::\$loaded[\$data['file']] = true;\n        }\n\n        return \$data;\n    }\n\n    public function onQuery(\$table, &\$query)\n    {\n        if (!is_array(\$query)) {\n            if (\$query instanceof \\MongoId) {\n                \$query = ['_id' => \$query];\n            } else if (is_scalar(\$query)) {\n                if (is_numeric(\$query)) {\n                    \$query = ['_id' => [\n                        '\$in' => [\$query . '', 0+\$query],\n                    ]];\n                } else if (preg_match('/^[0-9a-f]{24}\$/i', \$query)) {\n                    \$query = ['_id' => [\n                        '\$in' => [\$query, new \\MongoId(\$query)],\n                    ]];\n                } else {\n                    \$query = ['_id' => \$query];\n                }\n            }\n        }\n\n        switch (\$table) {\n";
     foreach ($collections as $collection) {
         $this->context['collection'] = $collection;
         echo "            case " . $collection->getClassCode() . ":\n";
         if ($collection->is('SingleCollection') && $collection->getParent()) {
             echo "                    \$query[";
             var_export($collection->getDiscriminator());
             echo "] = " . $collection->getClassCode() . ";\n";
         }
         foreach ($collection->getMethodsByAnnotation('onQuery') as $method) {
             $this->context['method'] = $method;
             echo "                    " . $method->toCode($collection, '$query') . "\n";
         }
         foreach ($collection->getPlugins('onQuery') as $plugin) {
             $this->context['plugin'] = $plugin;
             echo "                    " . $plugin->toCode($collection, '$query') . "\n";
         }
         echo "            break;\n";
     }
     echo "        }\n    }\n\n    public function mapClass(\$class)\n    {\n        if (is_object(\$class)) {\n            \$class = \$this->get_class(\$class);\n        }\n\n        \$class = strtolower(\$class);\n        if (empty(\$this->class_mapper[\$class])) {\n";
     foreach ($collections as $collection) {
         $this->context['collection'] = $collection;
         if ($collection->is('SingleCollection')) {
             echo "                if (\$class == " . $collection->getClassCode() . " ||  \$class == ";
             var_export($collection->getName());
             echo "){\n                    return ";
             var_export(['name' => $collection->getName(), 'dynamic' => true, 'prop' => $collection->getDiscriminator(), 'class' => NULL]);
             echo ";\n                }\n";
         }
     }
     echo "            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        \$data = \$this->class_mapper[\$class];\n\n        if (empty(self::\$loaded[\$data['file']])) {\n            if (!\$data['verify'](\$data['class'], false)) {\n                require \$data['file'];\n            }\n            self::\$loaded[\$data['file']] = true;\n        }\n\n        return \$data;\n    }\n\n    protected function is_array(\$array)\n    {\n        if (is_array(\$array)) {\n            \$keys = array_keys(\$array);\n            \$expected = range(0, count(\$array)-1);\n            return count(array_diff(\$keys, \$expected)) == 0;\n        }\n        return false;\n    }\n\n    protected function array_unique(\$array, \$toRemove)\n    {\n        \$return = array();\n        \$count  = array();\n        foreach (\$array as \$key => \$value) {\n            \$val = serialize(\$value);\n            if (empty(\$count[\$val])) {\n                \$count[\$val] = 0;\n            }\n            \$count[\$val]++; \n        }\n        foreach (\$toRemove as \$value) {\n            \$val = serialize(\$value);\n            if (!empty(\$count[\$val]) && \$count[\$val] != 1) {\n                return true;\n            }\n        }\n        return false;\n    }\n\n    public function mapObject(\$object)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->class_mapper[\$class];\n    }\n\n    public function getReflection(\$name)\n    {\n        \$class = strtolower(\$name);\n        if (empty(\$this->class_mapper[\$class])) {\n            if (empty(\$this->mapper[\$name])) {\n                throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n            }\n            \$class = \$this->mapper[\$name]['class'];\n        }\n\n        return new \\ActiveMongo2\\Reflection\\Collection(\$this->{\"reflect_\" . sha1(\$class)}(), \$this);\n    }\n\n    public function getReference(\$object, Array \$extra = array())\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n        return \$this->{\"get_reference_\" . sha1(\$class)}(\$object, \$extra);\n    }\n\n    public function populateFromArray(\$object, Array \$data)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"populate_from_array_\" . sha1(\$class)}(\$object, \$data);\n    }\n\n\n    public function getDocument(\$object)\n    {\n        if (\$object instanceof \\ActiveMongo2\\Reference) {\n            \$object = \$object->getObject();\n        }\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"get_array_\" . sha1(\$class)}(\$object);\n    }\n\n    public function validate(\$object)\n    {\n        \$old   = \$this->getRawDocument(\$object);\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"validate_\" . sha1(\$class)}(\$object, \$old);\n    }\n\n    public function set_property(\$object, \$name, \$value)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"set_property_\" . sha1(\$class)}(\$object, \$name, \$value);\n    }\n\n    public function get_property(\$object, \$name)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"get_property_\" . sha1(\$class)}(\$object, \$name);\n    }\n\n    public function update(\$object, Array &\$doc, Array \$old)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"update_\" . sha1(\$class)}(\$doc, \$old);\n    }\n\n    public function getRawDocument(\$object)\n    {\n        if (!empty(\$object->";
     echo $instance . ") && \$object->" . $instance . " instanceof ActiveMongo2Mapped) {\n            return \$object->";
     echo $instance . "->getOriginal();\n        }\n\n        return array();\n    }\n\n    public function populate(&\$object, \$data)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"populate_\" . sha1(\$class)}(\$object, \$data);\n    }\n\n    public function trigger(\$w, \$event, \$object, Array \$args = array())\n    {\n        if (!\$w) return;\n        if (\$object instanceof \\ActiveMongo2\\Reference) {\n            \$class = strtolower(\$object->getClass());\n        } else {\n            \$class = strtolower(\$this->get_class(\$object));\n        }\n        \$method = \"event_{\$event}_\" . sha1(\$class);\n        if (!is_callable(array(\$this, \$method))) {\n            return;\n        }\n\n        return \$this->\$method(\$object, \$args);\n    }\n\n    public function getMapping(\$class)\n    {\n        if (is_object(\$class)) {\n            \$class = \$this->get_class(\$class);\n        }\n        \$func  = \"get_mapping_\" . sha1(\$class);\n        if (!is_callable(array(\$this, \$func))) {\n            throw new \\Exception(\"Cannot map \$class\");\n        }\n        return \$this->\$func();\n    }\n\n    public function getObjectClass(\$col, \$doc)\n    {\n        if (\$doc instanceof \\MongoGridFsFile) {\n            \$doc = \$doc->file;\n        }\n        if (\$col instanceof \\MongoCollection) {\n            \$col = \$col->getName();\n        }\n\n        \$class = NULL;\n        switch (\$col) {\n";
     foreach ($collections as $collection) {
         $this->context['collection'] = $collection;
         if ($collection->is('GridFs')) {
             echo "            case \$this->ns_by_name[";
             var_export($collection->getname());
             echo "] . ";
             var_export($collection->getName() . '.files');
             echo ":\n            case \$this->ns_by_name[";
             var_export($collection->getname());
             echo "] . ";
             var_export($collection->getName() . '.chunks');
             echo ":\n            case ";
             var_export($collection->getName() . '.files');
             echo ":\n            case ";
             var_export($collection->getName() . '.chunks');
             echo ":\n";
         } else {
             if ($collection->getName()) {
                 echo "                case \$this->ns_by_name[";
                 var_export($collection->getname());
                 echo "] . ";
                 var_export($collection->getName());
                 echo ":\n";
             }
             echo "            case ";
             var_export($collection->getName());
             echo ":\n";
         }
         if (!$collection->is('SingleCollection')) {
             echo "                    \$class = " . $collection->getClassCode() . ";\n";
         } else {
             echo "                    if (!empty(" . $collection->getDiscriminator(true)->getPHPVariable() . ")) {\n                        \$class = ";
             echo $collection->getDiscriminator(true)->getPHPVariable() . ";\n                    }\n";
         }
         echo "                break;\n";
     }
     echo "        }\n\n        if (empty(\$class)) {\n            throw new \\RuntimeException(\"Cannot get class for collection {\$col}\");\n        }\n\n        return \$class;\n    }\n\n    public function get_class(\$object)\n    { \n        if (\$object instanceof \\ActiveMongo2\\Reference) {\n            \$class = \$object->getClass();\n        } else {\n            \$class = strtolower(get_class(\$object));\n        }\n\n        return \$class;\n    }\n\n    public function updateProperty(\$document, \$key, \$value)\n    {\n        \$class  = strtolower(\$this->get_class(\$document));\n        \$method = \"update_property_\" . sha1(\$class);\n        if (!is_callable(array(\$this, \$method))) {\n            throw new \\RuntimeException(\"Cannot trigger {\$event} event on '\$class' objects\");\n        }\n\n        return \$this->\$method(\$document, \$key, \$value);\n    }\n\n    public function ensureIndex(\$background)\n    {\n\n";
     $is_new = version_compare(MongoClient::VERSION, '1.5.0', '>');
     $this->context['is_new'] = $is_new;
     echo "\n";
     foreach ($collections->getIndexes() as $id => $index) {
         $this->context['id'] = $id;
         $this->context['index'] = $index;
         $next = uniqid(true);
         $this->context['next'] = $next;
         if (!empty($index['col'])) {
             $col = $index['col'];
             $this->context['col'] = $col;
         } else {
             $col = $index['prop']->getParent();
             $this->context['col'] = $col;
         }
         echo "\n            \$conn = \$this->class_connections[";
         var_export($col->getClass());
         echo "];\n            if (empty(\$this->connections[\$conn])) {\n                goto skip_";
         echo $next . ";\n            }\n            \$db = \$this->connections[\$conn];\n\n        try {\n            \$col = \$db->createCollection(\$this->ns_by_name[";
         var_export($col->getName());
         echo "] . ";
         var_export($col->getName());
         echo "); \n\n";
         if ($is_new) {
             echo "            \$return = \$col->createIndex(\n                ";
             echo crodas\FileUtil\dump_array($index['field']);
             echo ",\n                ";
             echo crodas\FileUtil\dump_array($index['extra']);
             echo "\n            );\n";
         } else {
             echo "            \$return = \$col->ensureIndex(\n                ";
             echo crodas\FileUtil\dump_array($index['field']);
             echo ",\n                ";
             echo crodas\FileUtil\dump_array($index['extra']);
             echo "\n            );\n";
         }
         echo "        } catch (\\MongoException \$e) {\n            // delete index and try to rebuild it\n            \$col->deleteIndex(";
         echo crodas\FileUtil\dump_array($index['field']);
         echo ");\n\n";
         if ($is_new) {
             echo "            \$col->createIndex(\n                ";
             echo crodas\FileUtil\dump_array($index['field']);
             echo ",\n                ";
             echo crodas\FileUtil\dump_array($index['extra']);
             echo "\n            );\n";
         } else {
             echo "            \$col->ensureIndex(\n                ";
             echo crodas\FileUtil\dump_array($index['field']);
             echo ",\n                ";
             echo crodas\FileUtil\dump_array($index['extra']);
             echo "\n            );\n";
         }
         echo "        }\n        skip_";
         echo $next . ":\n";
     }
     echo "    }\n\n    protected function compareObjects(\$a, \$b)\n    {\n        if (\$a === \$b) {\n            return true;\n        }\n\n        if (is_array(\$a) && is_array(\$b)) {\n            \$keysA = array_keys(\$a);\n            \$keysB = array_keys(\$b);\n            if (\$keysA !== \$keysB) {\n                return false;\n            }\n            foreach (\$keysA as \$key) {\n                if (!\$this->compareObjects(\$a[\$key], \$b[\$key])) {\n                    return false;\n                }\n            }\n            return true;\n        }\n\n        if (!is_object(\$a) || !is_object(\$b)) {\n            return false;\n        }\n\n        \$class = get_class(\$b);\n        if (!(\$a instanceof \$class)) {\n            return false;\n        }\n\n        if (\$a instanceof \\MongoBinData || \$a instanceof \\MongoId) {\n            return \$a->__toString() === \$a->__toString();\n        }\n\n        if (\$a instanceof \\MongoDate) {\n            return \$a->sec === \$b->sec && \$b->usec === \$b->usec;\n        }\n\n\n        var_dump(\$a, \$b);exit;\n    }\n\n\n";
     foreach ($collections as $collection) {
         $this->context['collection'] = $collection;
         echo "\n    /**\n     *  ";
         echo $collection->getClass() . " => " . $collection->GetName() . "\n     *  ";
         echo count($collection->getAnnotation()->getAnnotations()) . "\n     */\n    protected function set_property_";
         echo sha1($collection->getClass()) . "(\$object, \$name, \$value)\n    {\n        switch (\$name) {\n";
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             echo "        case ";
             var_export($prop->getPHPName());
             echo ":\n        case ";
             var_export($prop->getName());
             echo ":\n";
             if ($prop->isPublic()) {
                 echo "                \$object->" . $prop->getPHPName() . " = \$value;\n";
             } else {
                 echo "                \$property = new \\ReflectionProperty(\$object, ";
                 var_export($prop->getPHPName());
                 echo ");\n                \$property->setAccessible(true);\n                \$property->setValue(\$object, \$value);\n";
             }
             echo "            break;\n";
         }
         echo "        default:\n            throw new \\RuntimeException(\"Missing property {\$name}\");\n        }\n\n        return true;\n    }\n\n    /**\n     *  Populate from \$_POST for collection ";
         echo $collection->GetClass() . "\n     */\n    protected function populate_from_array_";
         echo sha1($collection->getClass()) . "(\$object, Array \$data)\n    {\n";
         if ($collection->GetName()) {
             echo "        if (array_key_exists(";
             var_export($collection->GetName());
             echo ", \$data)) {\n            \$data = \$data[";
             var_export($collection->getName());
             echo "];\n        }\n";
         }
         echo "        \n";
         if ($collection->GetParent()) {
             echo "        // populate parent data first\n        \$this->populate_from_array_";
             echo sha1($collection->GetParent()->getClass()) . "(\$object, \$data);\n";
         }
         echo "\n";
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             if ($prop->isId() || $prop->getAnnotation()->has('ReferenceMany,EmbedMany')) {
                 echo "                // we cannot handle " . $prop->GetName() . " at the moment\n";
                 continue;
             }
             echo "\n";
             foreach (array_unique([$prop->getName(), $prop->getPHPName()]) as $var) {
                 $this->context['var'] = $var;
                 echo "\n            if (array_key_exists(";
                 var_export($var);
                 echo ", \$data)) {\n                \$value = \$data[";
                 var_export($var);
                 echo "];\n";
                 if ($xcol = $prop->getReferenceCollection()) {
                     $xclass = $collections->ByName()[$xcol]['class'];
                     $this->context['xclass'] = $xclass;
                     if ($xclass) {
                         echo "                        if (!is_array(\$value)) {\n                            throw new \\RuntimeException(\"";
                         var_export($prop->getName());
                         echo " must be an array\");\n                        }\n                        if (";
                         var_export($prop->getType() == 'Reference');
                         echo " && !empty(\$value['_id'])) {\n                            \$value = \$this->connection->getCollection(";
                         var_export($xcol);
                         echo ")\n                                ->findOne(\$value['_id']);\n                        } else {\n";
                         if ($prop->isPublic()) {
                             echo "                                \$oldValue = \$object->" . $prop->getPHPName() . ";\n";
                         } else {
                             echo "                                \$property = new \\ReflectionProperty(\$object, ";
                             var_export($var);
                             echo ");\n                                \$property->setAccessible(true);\n                                \$oldValue = \$property->getValue(\$object);\n";
                         }
                         echo "                            \$docValue =  \$oldValue ?: new \\" . $xclass . ";\n                            \$this->populate_from_array_";
                         echo sha1($xclass) . "(\$docValue, \$value);\n                            \$value = \$docValue;\n                        }\n";
                     }
                 }
                 echo "\n";
                 if ($prop->isPublic()) {
                     echo "                    \$object->" . $prop->getPHPName() . " = \$value; \n";
                 } else {
                     echo "                    \$property = new \\ReflectionProperty(\$object, ";
                     var_export($prop->getPHPName());
                     echo ");\n                    \$property->setAccessible(true);\n                    \$property->setValue(\$object, \$value);\n";
                 }
                 echo "    \n            }\n";
             }
         }
         echo "    }\n\n\n    protected function get_property_";
         echo sha1($collection->getClass()) . "(\$object, \$name)\n    {\n        switch (\$name) {\n";
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             echo "        case ";
             var_export($prop->getPHPName());
             echo ":\n        case ";
             var_export($prop->getName());
             echo ":\n";
             if ($prop->isPublic()) {
                 echo "                \$return = \$object->" . $prop->getPHPName() . ";\n";
             } else {
                 echo "                \$property = new \\ReflectionProperty(\$object, ";
                 var_export($prop->getPHPName());
                 echo ");\n                \$property->setAccessible(true);\n                \$return = \$property->getValue(\$object);\n";
             }
             echo "            break;\n";
         }
         echo "        case '_id': \n            //fallback to get the object ID when it is not part of the object (rare case)\n            if (!empty(\$object->";
         echo $instance . ") && \$object->" . $instance . " instanceof ActiveMongo2Mapped) {\n                return \$object->";
         echo $instance . "->getOriginal()['_id'];\n            }\n        default:\n";
         if ($collection->getParent()) {
             echo "                return \$this->get_property_" . sha1($collection->getParent()) . "(\$object, \$name);\n";
         } else {
             echo "                throw new \\RuntimeException(\"Missing property {\$name}\");\n";
         }
         echo "        }\n\n        return \$return;\n    }\n\n    /**\n     *  Get update object ";
         echo $collection->getClass() . " \n     */\n    protected function update_";
         echo sha1($collection->getClass()) . "(Array &\$current, Array \$old, \$embed = false)\n    {\n        if (!\$embed && !empty(\$current['_id']) && \$current['_id'] != \$old['_id']) {\n            throw new \\RuntimeException(\"document ids cannot be updated\");\n        }\n\n";
         if (!$collection->getParent()) {
             echo "            \$change = array();\n";
         } else {
             echo "            \$change = \$this->update_" . sha1($collection->getParent()) . "(\$current, \$old, \$embed);\n";
         }
         echo "\n";
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             echo "            \$has_changed = false;\n            if (array_key_exists(";
             var_export($prop . '');
             echo ", " . $prop->getPHPBaseVariable('$current') . ")\n                || array_key_exists(";
             var_export($prop . '');
             echo ", \$old)) {\n                if (!array_key_exists(";
             var_export($prop . '');
             echo ", " . $prop->getPHPBaseVariable('$current') . ")) {\n                    \$change['\$unset'][";
             var_export($prop . '');
             echo "] = 1;\n                } else if (!array_key_exists(";
             var_export($prop . '');
             echo ", \$old)) {\n                    \$change['\$set'][";
             var_export($prop . '');
             echo "] = " . $prop->getPHPVariable('$current') . ";\n                    \$has_changed = true;\n                } else if (!\$this->compareObjects(";
             echo $prop->getPHPVariable('$current') . ", \$old[";
             var_export($prop . '');
             echo "])) {\n                    \$has_changed = true;\n\n";
             if ($prop->getAnnotation()->has('Inc')) {
                 echo "                        if (empty(\$old[";
                 var_export($prop . '');
                 echo "])) {\n                            \$prev = 0;\n                        } else {\n                            \$prev = \$old[";
                 var_export($prop . '');
                 echo "];\n                        }\n                        \$change['\$inc'][";
                 var_export($prop . '');
                 echo "] = " . $prop->GetPHPVariable('$current') . " - \$prev;\n";
             } else {
                 if ($prop->getAnnotation()->has('Embed')) {
                     echo "                        if (" . $prop->getPHPVariable('$current') . "['__embed_class'] != \$old[";
                     var_export($prop . '');
                     echo "]['__embed_class']) {\n                            \$change['\$set'][";
                     var_export($prop . '.');
                     echo " . \$index] = " . $prop->GetPHPVariable('$current') . ";\n                        } else {\n                            \$update = 'update_' . sha1(";
                     echo $prop->getPHPVariable('$current') . "['__embed_class']);\n                            \$diff = \$this->\$update(";
                     echo $prop->getPHPVariable('$current') . ", \$old[";
                     var_export($prop . '');
                     echo "], true);\n                            foreach (\$diff as \$op => \$value) {\n                                foreach (\$value as \$p => \$val) {\n                                    \$change[\$op][";
                     var_export($prop . '.');
                     echo " . \$p] = \$val;\n                                }\n                            }\n                        }\n";
                 } else {
                     if ($prop->getAnnotation()->has('EmbedMany')) {
                         echo "                        // add things to the array\n                        \$toRemove = array_diff_key(\$old[";
                         var_export($prop . '');
                         echo "], " . $prop->getPHPVariable('$current') . ");\n\n                        if (count(\$toRemove) > 0 && \$this->array_unique(\$old[";
                         var_export($prop . '');
                         echo "], \$toRemove)) {\n                            \$change['\$set'][";
                         var_export($prop . '');
                         echo "] = array_values(" . $prop->getPHPVariable('$current') . ");\n                        } else {\n                            foreach (";
                         echo $prop->getPHPVariable('$current') . " as \$index => \$value) {\n                                if (!array_key_exists(\$index, \$old[";
                         var_export($prop . '');
                         echo "])) {\n                                    \$change['\$push'][";
                         var_export($prop . '');
                         echo "]['\$each'][] = \$value;\n                                    continue;\n                                }\n                                if (\$value['__embed_class'] != \$old[";
                         var_export($prop . '');
                         echo "][\$index]['__embed_class']) {\n                                    \$change['\$set'][";
                         var_export($prop . '.');
                         echo " . \$index] = \$value;\n                                } else {\n                                    \$update = 'update_' . sha1(\$value['__embed_class']);\n                                    \$diff = \$this->\$update(\$value, \$old[";
                         var_export($prop . '');
                         echo "][\$index], true);\n                                    foreach (\$diff as \$op => \$value) {\n                                        foreach (\$value as \$p => \$val) {\n                                            \$change[\$op][";
                         var_export($prop . '.');
                         echo " . \$index . '.' . \$p] = \$val;\n                                        }\n                                    }\n                                }\n                            }\n\n                            foreach (\$toRemove as \$value) {\n                                if (!empty(\$value['__instance'])) {\n                                    \$change['\$pull'][";
                         var_export($prop . '');
                         echo "]['__instance']['\$in'][] = \$value['__instance'];\n                                } else {\n                                    \$change['\$pull'][";
                         var_export($prop . '');
                         echo "][] = \$value;\n                                }\n                            }\n                        }\n";
                     } else {
                         if ($prop->getAnnotation()->has('ReferenceMany,Array')) {
                             echo "                        // add things to the array\n                        \$toRemove = array_diff_key(\$old[";
                             var_export($prop . '');
                             echo "], " . $prop->getPHPVariable('$current') . ");\n\n                        if ((count(\$toRemove) > 0 && \$this->array_unique(\$old[";
                             var_export($prop . '');
                             echo "], \$toRemove)) || !\$this->is_array(\$old[";
                             var_export($prop . '');
                             echo "])) {\n                            \$change['\$set'][";
                             var_export($prop . '');
                             echo "] = array_values(" . $prop->getPHPVariable('$current') . ");\n                        } else {\n                            foreach (";
                             echo $prop->getPHPVariable('$current') . " as \$index => \$value) {\n                                if (!array_key_exists(\$index, \$old[";
                             var_export($prop . '');
                             echo "])) {\n";
                             if ($prop->getAnnotation()->has('ReferenceMany')) {
                                 echo "                                        \$change['\$addToSet'][";
                                 var_export($prop . '');
                                 echo "]['\$each'][] = \$value;\n";
                             } else {
                                 echo "                                        \$change['\$push'][";
                                 var_export($prop . '');
                                 echo "]['\$each'][] = \$value;\n";
                             }
                             echo "                                    continue;\n                                }\n\n                                if (!empty(\$old[";
                             var_export($prop . '');
                             echo "][\$index]['__instance']) && is_array(\$value)) {\n                                    // __instance is an internal variable that helps\n                                    // activemongo2 to remove sub objects from arrays easily.\n                                    // Its value is private to the library and it shouldn't change\n                                    // unless the value of the object changes\n                                    \$diff = \$this->array_diff(\n                                        \$value,\n                                        \$old[";
                             var_export($prop . '');
                             echo "][\$index]\n                                    );\n                                    if (count(\$diff) == 1 && !empty(\$diff['__instance'])) {\n                                        \$value['__instance'] = \$old[";
                             var_export($prop . '');
                             echo "][\$index]['__instance'];\n                                        ";
                             echo $prop->getPHPVariable('$current') . "[\$index] = \$value;\n                                    }\n                                }\n\n                                if (\$old[";
                             var_export($prop . '');
                             echo "][\$index] != \$value) {\n                                    \$change['\$set'][";
                             var_export($prop . '.');
                             echo " . \$index] = \$value;\n                                }\n                            }\n\n                            foreach (\$toRemove as \$value) {\n                                if (!empty(\$value['__instance'])) {\n                                    \$change['\$pull'][";
                             var_export($prop . '');
                             echo "]['__instance']['\$in'][] = \$value['__instance'];\n                                } else {\n                                    \$change['\$pull'][";
                             var_export($prop . '');
                             echo "] = \$value;\n                                }\n                            }\n                        }\n";
                         } else {
                             echo "                        \$change['\$set'][";
                             var_export($prop . '');
                             echo "] = " . $prop->getPHPVariable('$current') . ";\n";
                         }
                     }
                 }
             }
             echo "                }\n            }\n\n";
             $ann = $prop->getAnnotation();
             $this->context['ann'] = $ann;
             if ($ann->has('Array,ReferenceMany,EmbedMany')) {
                 if ($ann->has('Limit')) {
                     echo "                if (\$has_changed && !empty(\$change['\$push'][";
                     var_export($prop . '');
                     echo "])) {\n                    \$change['\$push'][";
                     var_export($prop . '');
                     echo "]['\$slice'] = ";
                     var_export(0 + current($prop->getAnnotation()->getOne('Limit')->getArgs()));
                     echo ";\n                }\n";
                 }
                 if ($ann->has('Sort')) {
                     echo "                if (\$has_changed && !empty(\$change['\$push'][";
                     var_export($prop . '');
                     echo "])) {\n                    \$change['\$sort'][";
                     var_export($prop . '');
                     echo "]['\$sort'] = ";
                     var_export(0 + current($prop->getAnnotation()->getOne('Limit')->getArgs()));
                     echo ";\n                }\n";
                 }
             }
         }
         echo "\n        return \$change;\n    }\n\n    protected function get_mapping_";
         echo sha1($collection->getClass()) . "() \n    {\n        return array(\n";
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             echo "                ";
             var_export($prop->getName(true));
             echo " => ";
             var_export($prop->getProperty());
             echo ",\n";
         }
         echo "        );\n    }\n\n    /**\n     *  Populate objects ";
         echo $collection->getClass() . " \n     */\n    protected function populate_";
         echo sha1($collection->getClass()) . "(\\" . $collection->getClass() . " &\$object, \$data)\n    {\n";
         if ($p = $collection->getParent()) {
             echo "            \$this->populate_" . sha1($p->getClass()) . "(\$object, \$data);\n";
         }
         echo "\n";
         if ($collection->is('GridFs')) {
             echo "            if (!\$data instanceof \\MongoGridFsFile) {\n                throw new \\RuntimeException(\"Internal error, trying to populate a GridFSFile with an array\");\n            }\n            \$data_file = \$data;\n            \$data      = \$data->file;\n            if (empty(\$data['metadata'])) {\n                \$data['metadata'] = [];\n            }\n";
             foreach (array("length", "chunkSize", "md5", "uploadDate") as $key) {
                 $this->context['key'] = $key;
                 echo "                \$data['metadata'][";
                 var_export($key);
                 echo "] = \$data[";
                 var_export($key);
                 echo "];\n";
             }
         } else {
             echo "\n            if (!is_array(\$data)) {\n                throw new \\RuntimeException(\"Internal error, trying to populate a document with a wrong data\");\n            }\n";
         }
         echo "\n        \$doc = \$data;\n\n";
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             if ($prop->getAnnotation()->has('ReferenceMany')) {
                 echo "                if (!empty(" . $prop->getPHPVariable() . ")) {\n                    foreach(";
                 echo $prop->getPHPVariable() . " as \$id => \$sub) {\n                        if (empty(\$sub['__instance']) || !strpos(\$sub['__instance'], \$sub['\$ref'])) {\n                            \$sub['__instance'] = \$sub['\$ref'] . ':' . serialize(\$sub['\$id']) ;\n                        }\n                        ";
                 echo $prop->getPHPVariable() . "[\$id] = \$sub;\n                    }\n                }\n";
             } else {
                 if ($prop->getAnnotation()->has('Stream')) {
                     if ($prop->isPublic()) {
                         echo "                    \$object->" . $prop->getPHPName() . " = \$data_file->getResource();\n";
                     } else {
                         echo "                    \$property = new \\ReflectionProperty(\$object, ";
                         var_export($prop->getPHPName());
                         echo ");\n                    \$property->setAccessible(true);\n                    \$property->setValue(\$object, \$data_file->getResource());\n";
                     }
                     continue;
                 }
             }
             echo "\n            if (array_key_exists(";
             var_export($prop . '');
             echo ", " . $prop->getPHPBaseVariable() . ")) {\n";
             foreach ($prop->getCallback('Hydratate') as $h) {
                 $this->context['h'] = $h;
                 echo "                    " . $h->toCode($prop, $prop->getPHPVariable()) . "\n";
             }
             echo "\n";
             if ($prop->isPublic()) {
                 echo "                    \$object->" . $prop->getPHPName() . " = " . $prop->getPHPVariable() . ";\n";
             } else {
                 echo "                    \$property = new \\ReflectionProperty(\$object, ";
                 var_export($prop->getPHPName());
                 echo ");\n                    \$property->setAccessible(true);\n                    \$property->setValue(\$object, ";
                 echo $prop->getPHPVariable() . ");\n";
             }
             echo "                \n            }\n";
         }
         echo "\n        if (empty(\$object->";
         echo $instance . ")) {\n            \$object->";
         echo $instance . " = new ActiveMongo2Mapped(" . $collection->getClassCode() . ", \$data);\n        } else {\n            \$object->";
         echo $instance . "->" . $instance . "_setOriginal(\$data);\n        }\n    }\n\n    /**\n     *  Get reference of  ";
         echo $collection->getClass() . " object\n     */\n    protected function get_reference_";
         echo sha1($collection->getClass()) . "(\\" . $collection->getClass() . " \$object, \$include = Array())\n    {\n        \$document = \$this->get_array_";
         echo sha1($collection->getClass()) . "(\$object);\n        \$extra    = array();\n        if (\$include) {\n            \$extra  = array_intersect_key(\$document, \$include);\n        }\n\n";
         if ($cache = $collection->getRefCache()) {
             echo "            \$extra = array_merge(\$extra,  array_intersect_key(\n                \$document, \n                ";
             var_export($cache);
             echo "\n            ));\n";
         }
         echo "        \n        foreach (\$extra as \$key => \$value) {\n            if (is_object(\$value)) {\n                if (\$value instanceof \\ActiveMongo2\\Reference) {\n                    \$extra[\$key] = \$value->getReference();\n                } else {\n                    \$extra[\$key] = \$this->getReference(\$value);\n                }\n            }\n        }\n\n        return array_merge(array(\n                '\$ref'  => ";
         var_export($collection->getName());
         echo ", \n                '\$id'   => \$document['_id'],\n                '__class' => ";
         echo $collection->getClassCode() . ",\n                '__instance' => ";
         var_export($collection->getName());
         echo " . ':' . serialize(\$document['_id']),\n            )\n            , \$extra\n        );\n\n    }\n\n    /**\n     *  Validate ";
         echo $collection->getClass() . " object\n     */\n    protected function get_array_";
         echo sha1($collection) . "(\\" . $collection . " \$object, \$recursive = true)\n    {\n";
         if (!$collection->getParent()) {
             echo "            \$doc = array();\n";
         } else {
             echo "            \$doc = \$recursive ? \$this->get_array_" . sha1($collection->getParent()) . "(\$object) : array();\n";
         }
         echo "\n";
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             if ($prop->isPublic() && !$prop->isCustom()) {
                 echo "                /* Public property " . $prop->getPHPName() . " -> " . $prop->getName() . " */\n                if (\$object->";
                 echo $prop->getPHPName() . " !== NULL) {\n                    ";
                 echo $prop->getPHPVariable() . " = \$object->" . $prop->getPHPName() . ";\n                }\n";
             } else {
                 if ($prop->isCustom()) {
                     echo "                /* public and custom property " . $prop->getPHPName() . " -> " . $prop->getName() . " */\n                if (!empty(\$object->";
                     echo $prop->getPHPName() . ")) {\n                    ";
                     echo $prop->getPHPVariable() . " = \$object->" . $prop->getPHPName() . ";\n                }\n";
                 } else {
                     echo "                \$property = new \\ReflectionProperty(\$object, ";
                     var_export($prop->getPHPName());
                     echo ");\n                \$property->setAccessible(true);\n                \$value = \$property->getValue(\$object);\n                if (\$value !== NULL) {\n                    ";
                     echo $prop->getPHPVariable() . " = \$value;\n                }\n";
                 }
             }
         }
         echo "\n";
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             foreach ($prop->getCallback('DefaultValue') as $default) {
                 $this->context['default'] = $default;
                 echo "                if (empty(" . $prop->getPHPVariable() . ")) {\n                    ";
                 echo $default->toCode($prop) . "\n                    ";
                 echo $prop->getPHPVariable() . " = \$return;\n                }\n";
             }
         }
         echo "\n";
         if ($collection->is('SingleCollection')) {
             echo "            // SINGLE COLLECTION\n            ";
             echo $collection->getDiscriminator(true)->getPHPVariable() . " = " . $collection->getClassCode() . ";\n";
         }
         echo "\n        if (empty(\$doc['_id'])) {\n            \$oldDoc = \$this->getRawDocument(\$object, false);\n            if (!empty(\$oldDoc['_id'])) {\n                \$doc['_id'] = \$oldDoc['_id'];\n            }\n        }\n\n        return \$doc;\n    }\n\n    protected function reflect_";
         echo sha1($collection->getClass()) . "() \n    {\n        \$reflection = array(\n            'class'    => ";
         echo $collection->getClassCode() . ",\n            'name'     => ";
         var_export($collection->getName());
         echo ",\n            'collection'     => ";
         var_export($collection->getName());
         echo ",\n            'annotation' => array(\n";
         foreach ($collection->getAnnotation()->getAnnotations() as $ann) {
             $this->context['ann'] = $ann;
             echo "            new Annotation(";
             var_export($ann->getName());
             echo ", ";
             var_export($collection->serializeAnnArgs($ann));
             echo "),\n";
         }
         echo "            ),\n            'properties'  => array(\n";
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             echo "            ";
             var_export($prop->getPHPName());
             echo " => new \\ActiveMongo2\\Reflection\\Property(array(\n                'property' => ";
             var_export($prop . '');
             echo ",\n                'type'     => ";
             var_export($prop->getType());
             echo ",\n";
             if ($prop->getReferenceCollection()) {
                 echo "                'collection' => ";
                 var_export($prop->getReferenceCollection());
                 echo ",\n";
             }
             echo "                'annotation' => new Annotations(array(\n";
             foreach ($prop->getAnnotation()->getAnnotations() as $ann) {
                 $this->context['ann'] = $ann;
                 echo "                    new Annotation(";
                 var_export($ann->getName());
                 echo ", ";
                 var_export($collection->serializeAnnArgs($ann));
                 echo "),\n";
             }
             echo "                )),\n            ), \$this),\n";
         }
         echo "        ));\n\n";
         if ($collection->getParent()) {
             echo "            \$reflection['properties'] = array_merge(\n                \$this->reflect_";
             echo sha1($collection->GetParent()) . "()['properties'], \n                \$reflection['properties']\n            );\n";
         }
         echo "        return \$reflection;\n    }\n\n    /**\n     *  Validate ";
         echo $collection->getClass() . " object\n     */\n    protected function validate_";
         echo sha1($collection->getClass()) . "(\\" . $collection->getClass() . " \$object, Array \$old)\n    {\n";
         if ($collection->getParent()) {
             echo "            \$doc = array_merge(\n                \$this->validate_";
             echo sha1($collection->getParent()) . "(\$object, \$old),\n                \$this->get_array_";
             echo sha1($collection->getClass()) . "(\$object, false)\n            );\n";
         } else {
             echo "            \$doc = \$this->get_array_" . sha1($collection->getClass()) . "(\$object);\n";
         }
         echo "\n";
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             if ($prop->getAnnotation()->has('Required')) {
                 echo "            if (empty(" . $prop->getPHPVariable() . ")) {\n                throw new \\RuntimeException(\"";
                 echo $prop . '' . " cannot be empty\");\n            }\n";
             }
             echo "            if (!empty(" . $prop->getPHPVariable() . ") &&\n                   (empty(";
             echo $prop->getPHPVariable('$old') . ") ||\n                    ";
             echo $prop->getPHPVariable() . " !== " . $prop->getPHPVariable('$old') . " )) {\n                                                                                            \n";
             foreach ($prop->getCallback('Validate') as $val) {
                 $this->context['val'] = $val;
                 if (!$val->isLast()) {
                     echo "                    " . $val->toCode($prop, $prop->getPHPVariable()) . "\n                    if (\$return === FALSE) {\n                        throw new \\RuntimeException(\"Validation failed for ";
                     echo $prop . '' . "\");\n                    }\n";
                 }
             }
             echo "\n";
             if ($prop->getAnnotation()->has('Date')) {
                 echo "                    \$_date = \\date_create('" . "@" . "' . " . $prop->getPHPVariable() . "->sec);\n                    if (";
                 echo $validator->functionName($collection->getClass(), $prop->getPHPName()) . "(\$_date) === false) {\n                        throw new \\RuntimeException(\"Validation failed for ";
                 echo $prop . '' . "\");\n                    }\n";
             } else {
                 if (!$prop->isCustom() && $validator->hasRules($collection->getClass(), $prop->getPHPName())) {
                     echo "                    if (" . $validator->functionName($collection->getClass(), $prop->getPHPName()) . "(" . $prop->getPHPVariable() . ") === false) {\n                        throw new \\RuntimeException(\"Validation failed for ";
                     echo $prop . '' . "\");\n                    }\n";
                 }
             }
             echo "\n";
             foreach ($prop->getCallback('Validate') as $val) {
                 $this->context['val'] = $val;
                 if ($val->isLast()) {
                     echo "                    " . $val->toCode($prop, $prop->getPHPVariable()) . "\n                    if (\$return === FALSE) {\n                        throw new \\RuntimeException(\"Validation failed for ";
                     echo $prop . '' . "\");\n                    }\n";
                 }
             }
             echo "            } else if (!empty(" . $prop->getPHPVariable() . ")) {\n                // always check\n";
             foreach ($prop->getCallback('Validate') as $val) {
                 $this->context['val'] = $val;
                 if ($val->isAlwaysCheck()) {
                     echo "                    " . $val->toCode($prop, $prop->getPHPVariable()) . "\n                    if (\$return === FALSE) {\n                        throw new \\RuntimeException(\"Validation failed for ";
                     echo $prop . '' . "\");\n                    }\n";
                 }
             }
             echo "            }\n";
         }
         echo "\n        return \$doc;\n    }\n\n    protected function update_property_";
         echo sha1($collection->getClass()) . "(\\" . $collection->getClass() . " \$document, \$property, \$value)\n    {\n";
         if ($collection->getParent()) {
             echo "            \$this->update_property_" . sha1($collection->getParent()) . "(\$document, \$property, \$value);\n";
         }
         echo "        \$iproperty = strtolower(\$property);\n";
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             echo "            if (\$property ==  ";
             var_export($prop . '');
             echo "\n";
             foreach ($prop->getAnnotation()->getAnnotations() as $annotation) {
                 $this->context['annotation'] = $annotation;
                 echo "                 || \$iproperty == ";
                 var_export('@' . $annotation->getName());
                 echo "\n";
             }
             echo "            ) {\n";
             if ($prop->isPublic()) {
                 echo "                    \$document->" . $prop->getPHPName() . " = \$value;\n";
             } else {
                 echo "                    \$property = new \\ReflectionProperty(\$object, ";
                 var_export($prop->getPHPNAme());
                 echo ");\n                    \$property->setAccessible(true);\n                    \$property->setValue(\$document, \$value);\n";
             }
             echo "            }\n";
         }
         echo "    }\n\n\n";
         foreach ($collections->getEvents() as $ev) {
             $this->context['ev'] = $ev;
             if ($collection->hasEvent($ev)) {
                 echo "    /**\n     *  Code for ";
                 echo $ev . " events for objects " . $collection->getClass() . "\n     */\n        protected function event_";
                 echo $ev . "_" . sha1($collection->getClass()) . "(\$document, Array \$args)\n        {\n            \$class = \$this->get_class(\$document);\n";
                 if (!$collection->isTrait()) {
                     echo "            if (\$class != " . $collection->getClassCode() . " && !is_subclass_of(\$class, " . $collection->getClassCode() . ")) {\n                throw new \\Exception(\"Class invalid class name (\$class) expecting  \"  . ";
                     echo $collection->getClassCode() . ");\n            }\n";
                 }
                 echo "\n";
                 foreach ($collection->getParentAndTraits() as $parent) {
                     $this->context['parent'] = $parent;
                     if ($parent->hasEvent($ev)) {
                         $method = "event_" . $ev . "_" . sha1($parent->getClass());
                         $this->context['method'] = $method;
                         echo "                \$this->" . $method . "(\$document, \$args);\n";
                     }
                 }
                 echo "\n";
                 foreach ($collection->getMethodsByAnnotation($ev) as $method) {
                     $this->context['method'] = $method;
                     echo "                " . $method->toCode($collection, '$document') . "\n                if (\$return === FALSE) {\n                    throw new \\RuntimeException;\n                }\n";
                 }
                 echo "\n";
                 if ($ev == "postCreate" || $ev == "postUpdate") {
                     echo "                \$col = \$args[1]->getDatabase()->references_queue;\n";
                     ActiveMongo2\Template\Templates::exec("reference/deferred.tpl.php", compact('ev', 'collection'), $this->context);
                     if ($ev == "postUpdate") {
                         ActiveMongo2\Template\Templates::exec("reference/update.tpl.php", compact('ev', 'collection'), $this->context);
                     }
                 }
                 echo "\n";
                 foreach ($collection->getPlugins($ev) as $plugin) {
                     $this->context['plugin'] = $plugin;
                     echo "                " . $plugin->toCode($collection, '$document') . "\n                if (\$return === FALSE) {\n                    throw new \\RuntimeException;\n                }\n";
                 }
                 echo "        }\n";
             }
         }
         echo "\n";
     }
     echo "}\n\nclass ActiveMongo2Mapped\n{\n    protected \$class;\n    protected \$data;\n\n    public function __construct(\$name, Array \$data)\n    {\n        \$this->class = \$name;\n        \$this->data  = \$data;\n    }\n\n    public function getClass()\n    {\n        return \$this->class;\n    }\n\n    public function getOriginal()\n    {\n        return \$this->data;\n    }\n\n    public function ";
     echo $instance . "_setOriginal(Array \$data)\n    {\n        \$this->data = \$data;\n    }\n}\n\n";
     ActiveMongo2\Template\Templates::exec('validator', $this->context);
     echo "\nreturn array(\n    \"ns\" => ";
     var_export(trim($namespace, '\\'));
     echo ",\n    \"validator\" => ";
     var_export($valns);
     echo ",\n);\n";
     if ($return) {
         return ob_get_clean();
     }
 }
Exemple #2
0
 public function _render(array $vars = array(), $return = false)
 {
     global $file_54a244ae094fd, $line_54a244ae094fd;
     $this->context = $vars;
     extract($vars);
     if ($return) {
         ob_start();
     }
     $_54a244ae094fd = array_push($file_54a244ae094fd, 'Documents.tpl.php') - 1;
     $line_54a244ae094fd[$_54a244ae094fd] = 1;
     echo "<?php\n\nnamespace ";
     $line_54a244ae094fd[$_54a244ae094fd] = 3;
     echo trim($namespace, '\\') . ";\n\nuse ";
     $line_54a244ae094fd[$_54a244ae094fd] = 5;
     echo $valns . " as v;\nuse MongoClient;\nuse ActiveMongo2\\Connection;\nuse Notoj\\Annotation;\nuse Notoj;\n\n";
     $line_54a244ae094fd[$_54a244ae094fd] = 11;
     $instance = '_' . uniqid(true);
     $this->context['instance'] = $instance;
     $line_54a244ae094fd[$_54a244ae094fd] = 12;
     echo "\nclass Mapper\n{\n    protected \$mapper = ";
     $line_54a244ae094fd[$_54a244ae094fd] = 15;
     echo agpmedia\FileUtil\dump_array($collections->byName(), true) . ";\n    protected \$class_mapper = ";
     $line_54a244ae094fd[$_54a244ae094fd] = 16;
     echo agpmedia\FileUtil\dump_array($collections->byClass(), true) . ";\n    protected static \$loaded = array();\n    protected \$connection;\n    protected \$connections;\n    protected \$class_connections = ";
     $line_54a244ae094fd[$_54a244ae094fd] = 20;
     var_export($collections->byConnection());
     echo ";\n    protected \$ns = array();\n    protected \$ns_by_name = array();\n\n    public function __construct(Connection \$conn)\n    {\n        \$this->connection = \$conn;\n        spl_autoload_register(array(\$this, '__autoloader'));\n    }\n\n    public function setDatabases(Array \$conns, Array \$ns)\n    {\n        \$this->connections = \$conns;\n\n        \$this->ns = \$ns;\n        foreach (\$this->class_connections as \$class => \$conn) {\n            \$ns =\"\";\n            if (!empty(\$this->ns[\$conn])) {\n                \$ns = \$this->ns[\$conn];\n            }\n            if (!empty(\$this->class_mapper[\$class])) {\n                \$this->ns_by_name[ \$this->class_mapper[\$class]['name'] ] = \$ns;\n            }\n        }\n\n        return \$this;\n    }\n\n    public function getRelativePath(\$object, \$dir)\n    {\n        if (\$dir[0] == '/') {\n            return \$dir;\n        }\n        \$info = \$this->mapClass(\$object);\n        return __DIR__ . \$info['dir'] . \"/\" . \$dir;\n    }\n\n    protected function array_diff(Array \$arr1, Array \$arr2)\n    {\n        \$diff = array();\n        foreach (\$arr1 as \$key => \$value) {\n            if (empty(\$arr2[\$key]) || \$arr2[\$key] !== \$arr1[\$key]) {\n                \$diff[\$key] = \$value;\n            }\n        }\n        return \$diff;\n    }\n\n    public function getCollections()\n    {\n        return array(\n";
     $line_54a244ae094fd[$_54a244ae094fd] = 71;
     foreach ($collections as $collection) {
         $this->context['collection'] = $collection;
         $line_54a244ae094fd[$_54a244ae094fd] = 72;
         if ($collection->getName()) {
             $line_54a244ae094fd[$_54a244ae094fd] = 73;
             echo "                ";
             var_export($collection->getClass());
             echo " => \$this->ns_by_name[";
             var_export($collection->getName());
             echo "]. ";
             var_export($collection->getName());
             echo ",\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 74;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 75;
     }
     $line_54a244ae094fd[$_54a244ae094fd] = 76;
     echo "        );\n    }\n\n    public function __autoloader(\$class)\n    {\n        \$class = strtolower(\$class);\n        if (!empty(\$this->class_mapper[\$class])) {\n            self::\$loaded[\$this->class_mapper[\$class]['file']] = true;\n            require __DIR__ . \$this->class_mapper[\$class]['file'];\n\n            return true;\n        }\n        return false;\n    }\n\n    public function getCollectionObject(\$col)\n    {\n        if (!is_scalar(\$col) || empty(\$this->mapper[\$col])) {\n            \$data = \$this->mapClass(\$col);     \n        } else {\n            \$data = \$this->mapper[\$col];\n        }\n\n        if (empty(self::\$loaded[\$data['file']])) {\n            if (!class_exists(\$data['class'], false)) {\n                require __DIR__ .  \$data['file'];\n            }\n            self::\$loaded[\$data['file']] = true;\n        }\n\n        \$data['name'] = \$this->ns_by_name[\$data['name']] . \$data['name'];\n\n        \$conn = \$this->class_connections[\$data['class']];\n\n        if (empty(\$this->connections[\$conn])) {\n            throw new \\RuntimeException(\"Cannot find connection \$conn\");\n        }\n\n        \$db = \$this->connections[\$conn];\n        if (!empty(\$data['is_gridfs'])) {\n            \$col = \$db->getGridFs(\$data['name']);\n        } else {\n            \$col = \$db->selectCollection(\$data['name']);\n        }\n\n        return [\$col, \$data['class']];\n    }\n\n    public function mapCollection(\$col)\n    {\n        if (empty(\$this->mapper[\$col])) {\n            throw new \\RuntimeException(\"Cannot map {\$col} collection to its class\");\n        }\n\n        \$data = \$this->mapper[\$col];\n\n        if (empty(self::\$loaded[\$data['file']])) {\n            if (!class_exists(\$data['class'], false)) {\n                require __DIR__ .  \$data['file'];\n            }\n            self::\$loaded[\$data['file']] = true;\n        }\n\n        return \$data;\n    }\n\n    public function onQuery(\$table, &\$query)\n    {\n        if (!is_array(\$query)) {\n            if (\$query instanceof \\MongoId) {\n                \$query = ['_id' => \$query];\n            } else if (is_scalar(\$query)) {\n                if (is_numeric(\$query)) {\n                    \$query = ['_id' => [\n                        '\$in' => [\$query . '', 0+\$query],\n                    ]];\n                } else if (preg_match('/^[0-9a-f]{24}\$/i', \$query)) {\n                    \$query = ['_id' => [\n                        '\$in' => [\$query, new \\MongoId(\$query)],\n                    ]];\n                } else {\n                    \$query = ['_id' => \$query];\n                }\n            }\n        }\n\n        switch (\$table) {\n";
     $line_54a244ae094fd[$_54a244ae094fd] = 163;
     foreach ($collections as $collection) {
         $this->context['collection'] = $collection;
         $line_54a244ae094fd[$_54a244ae094fd] = 164;
         echo "            case ";
         var_export($collection->getClass());
         echo ":\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 165;
         if ($collection->is('SingleCollection') && $collection->getParent()) {
             $line_54a244ae094fd[$_54a244ae094fd] = 166;
             echo "                    \$query[";
             var_export($collection->getDiscriminator());
             echo "] = ";
             var_export($collection->getClass());
             echo ";\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 167;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 168;
         foreach ($collection->getMethodsByAnnotation('onQuery') as $method) {
             $this->context['method'] = $method;
             $line_54a244ae094fd[$_54a244ae094fd] = 169;
             echo "                    " . $method->toCode($collection, '$query') . "\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 170;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 171;
         foreach ($collection->getPlugins('onQuery') as $plugin) {
             $this->context['plugin'] = $plugin;
             $line_54a244ae094fd[$_54a244ae094fd] = 172;
             echo "                    " . $plugin->toCode($collection, '$query') . "\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 173;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 174;
         echo "            break;\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 175;
     }
     $line_54a244ae094fd[$_54a244ae094fd] = 176;
     echo "        }\n    }\n\n    public function mapClass(\$class)\n    {\n        if (is_object(\$class)) {\n            \$class = \$this->get_class(\$class);\n        }\n\n        \$class = strtolower(\$class);\n        if (empty(\$this->class_mapper[\$class])) {\n";
     $line_54a244ae094fd[$_54a244ae094fd] = 187;
     foreach ($collections as $collection) {
         $this->context['collection'] = $collection;
         $line_54a244ae094fd[$_54a244ae094fd] = 188;
         if ($collection->is('SingleCollection')) {
             $line_54a244ae094fd[$_54a244ae094fd] = 189;
             echo "                if (\$class == ";
             var_export($collection->getClass());
             echo " ||  \$class == ";
             var_export($collection->getName());
             echo "){\n                    return ";
             $line_54a244ae094fd[$_54a244ae094fd] = 190;
             var_export(['name' => $collection->getName(), 'dynamic' => true, 'prop' => $collection->getDiscriminator(), 'class' => NULL]);
             echo ";\n                }\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 192;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 193;
     }
     $line_54a244ae094fd[$_54a244ae094fd] = 194;
     echo "            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        \$data = \$this->class_mapper[\$class];\n\n        if (empty(self::\$loaded[\$data['file']])) {\n            if (!class_exists(\$data['class'], false)) {\n                require __DIR__ . \$data['file'];\n            }\n            self::\$loaded[\$data['file']] = true;\n        }\n\n        return \$data;\n    }\n\n    protected function is_array(\$array)\n    {\n        if (is_array(\$array)) {\n            \$keys = array_keys(\$array);\n            \$expected = range(0, count(\$array)-1);\n            return count(array_diff(\$keys, \$expected)) == 0;\n        }\n        return false;\n    }\n\n    protected function array_unique(\$array, \$toRemove)\n    {\n        \$return = array();\n        \$count  = array();\n        foreach (\$array as \$key => \$value) {\n            \$val = serialize(\$value);\n            if (empty(\$count[\$val])) {\n                \$count[\$val] = 0;\n            }\n            \$count[\$val]++; \n        }\n        foreach (\$toRemove as \$value) {\n            \$val = serialize(\$value);\n            if (!empty(\$count[\$val]) && \$count[\$val] != 1) {\n                return true;\n            }\n        }\n        return false;\n    }\n\n    public function mapObject(\$object)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->class_mapper[\$class];\n    }\n\n    public function getReflection(\$name)\n    {\n        \$class = strtolower(\$name);\n        if (empty(\$this->class_mapper[\$class])) {\n            if (empty(\$this->mapper[\$name])) {\n                throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n            }\n            \$class = \$this->mapper[\$name]['class'];\n        }\n\n        return new \\ActiveMongo2\\Reflection\\Collection(\$this->{\"reflect_\" . sha1(\$class)}(), \$this);\n    }\n\n    public function getReference(\$object, Array \$extra = array())\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"get_reference_\" . sha1(\$class)}(\$object, \$extra);\n    }\n\n    public function populateFromArray(\$object, Array \$data)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"populate_from_array_\" . sha1(\$class)}(\$object, \$data);\n    }\n\n\n    public function getDocument(\$object)\n    {\n        if (\$object instanceof \\ActiveMongo2\\Reference) {\n            \$object = \$object->getObject();\n        }\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"get_array_\" . sha1(\$class)}(\$object);\n    }\n\n    public function validate(\$object)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"validate_\" . sha1(\$class)}(\$object);\n    }\n\n    public function set_property(\$object, \$name, \$value)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"set_property_\" . sha1(\$class)}(\$object, \$name, \$value);\n    }\n\n    public function get_property(\$object, \$name)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"get_property_\" . sha1(\$class)}(\$object, \$name);\n    }\n\n    public function update(\$object, Array &\$doc, Array \$old)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"update_\" . sha1(\$class)}(\$doc, \$old);\n    }\n\n    public function getRawDocument(\$object)\n    {\n        if (!empty(\$object->";
     $line_54a244ae094fd[$_54a244ae094fd] = 338;
     echo $instance . ") && \$object->" . $instance . " instanceof ActiveMongo2Mapped) {\n            return \$object->";
     $line_54a244ae094fd[$_54a244ae094fd] = 339;
     echo $instance . "->getOriginal();\n        }\n\n        return array();\n    }\n\n    public function populate(&\$object, \$data)\n    {\n        \$class = strtolower(\$this->get_class(\$object));\n\n        if (empty(\$this->class_mapper[\$class])) {\n            throw new \\RuntimeException(\"Cannot map class {\$class} to its document\");\n        }\n\n        return \$this->{\"populate_\" . sha1(\$class)}(\$object, \$data);\n    }\n\n    public function trigger(\$w, \$event, \$object, Array \$args = array())\n    {\n        if (!\$w) return;\n        if (\$object instanceof \\ActiveMongo2\\Reference) {\n            \$class = strtolower(\$object->getClass());\n        } else {\n            \$class = strtolower(\$this->get_class(\$object));\n        }\n        \$method = \"event_{\$event}_\" . sha1(\$class);\n        if (!is_callable(array(\$this, \$method))) {\n            throw new \\RuntimeException(\"Cannot trigger {\$event} event on '\$class' objects\");\n        }\n\n        return \$this->\$method(\$object, \$args);\n    }\n\n    public function getMapping(\$class)\n    {\n        if (is_object(\$class)) {\n            \$class = \$this->get_class(\$class);\n        }\n        \$func  = \"get_mapping_\" . sha1(\$class);\n        if (!is_callable(array(\$this, \$func))) {\n            throw new \\Exception(\"Cannot map \$class\");\n        }\n        return \$this->\$func();\n    }\n\n    public function getObjectClass(\$col, \$doc)\n    {\n        if (\$doc instanceof \\MongoGridFsFile) {\n            \$doc = \$doc->file;\n        }\n        if (\$col instanceof \\MongoCollection) {\n            \$col = \$col->getName();\n        }\n\n        \$class = NULL;\n        switch (\$col) {\n";
     $line_54a244ae094fd[$_54a244ae094fd] = 395;
     foreach ($collections as $collection) {
         $this->context['collection'] = $collection;
         $line_54a244ae094fd[$_54a244ae094fd] = 396;
         if ($collection->is('GridFs')) {
             $line_54a244ae094fd[$_54a244ae094fd] = 397;
             echo "            case \$this->ns_by_name[";
             var_export($collection->getname());
             echo "] . ";
             var_export($collection->getName() . '.files');
             echo ":\n            case \$this->ns_by_name[";
             $line_54a244ae094fd[$_54a244ae094fd] = 398;
             var_export($collection->getname());
             echo "] . ";
             var_export($collection->getName() . '.chunks');
             echo ":\n            case ";
             $line_54a244ae094fd[$_54a244ae094fd] = 399;
             var_export($collection->getName() . '.files');
             echo ":\n            case ";
             $line_54a244ae094fd[$_54a244ae094fd] = 400;
             var_export($collection->getName() . '.chunks');
             echo ":\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 401;
         } else {
             $line_54a244ae094fd[$_54a244ae094fd] = 402;
             if ($collection->getName()) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 403;
                 echo "                case \$this->ns_by_name[";
                 var_export($collection->getname());
                 echo "] . ";
                 var_export($collection->getName());
                 echo ":\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 404;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 405;
             echo "            case ";
             var_export($collection->getName());
             echo ":\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 406;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 407;
         if (!$collection->is('SingleCollection')) {
             $line_54a244ae094fd[$_54a244ae094fd] = 408;
             echo "                    \$class = ";
             var_export($collection->getClass());
             echo ";\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 409;
         } else {
             $line_54a244ae094fd[$_54a244ae094fd] = 410;
             echo "                    if (!empty(" . $collection->getDiscriminator(true)->getPHPVariable() . ")) {\n                        \$class = ";
             $line_54a244ae094fd[$_54a244ae094fd] = 411;
             echo $collection->getDiscriminator(true)->getPHPVariable() . ";\n                    }\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 413;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 414;
         echo "                break;\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 415;
     }
     $line_54a244ae094fd[$_54a244ae094fd] = 416;
     echo "        }\n\n        if (empty(\$class)) {\n            throw new \\RuntimeException(\"Cannot get class for collection {\$col}\");\n        }\n\n        return \$class;\n    }\n\n    public function get_class(\$object)\n    { \n        if (\$object instanceof \\ActiveMongo2\\Reference) {\n            \$class = \$object->getClass();\n        } else {\n            \$class = strtolower(get_class(\$object));\n        }\n\n        return \$class;\n    }\n\n    public function updateProperty(\$document, \$key, \$value)\n    {\n        \$class  = strtolower(\$this->get_class(\$document));\n        \$method = \"update_property_\" . sha1(\$class);\n        if (!is_callable(array(\$this, \$method))) {\n            throw new \\RuntimeException(\"Cannot trigger {\$event} event on '\$class' objects\");\n        }\n\n        return \$this->\$method(\$document, \$key, \$value);\n    }\n\n    public function ensureIndex(\$background)\n    {\n\n";
     $line_54a244ae094fd[$_54a244ae094fd] = 450;
     $is_new = version_compare(MongoClient::VERSION, '1.5.0', '>');
     $this->context['is_new'] = $is_new;
     $line_54a244ae094fd[$_54a244ae094fd] = 451;
     echo "\n";
     $line_54a244ae094fd[$_54a244ae094fd] = 452;
     foreach ($collections->getIndexes() as $id => $index) {
         $this->context['id'] = $id;
         $this->context['index'] = $index;
         $line_54a244ae094fd[$_54a244ae094fd] = 453;
         $next = uniqid(true);
         $this->context['next'] = $next;
         $line_54a244ae094fd[$_54a244ae094fd] = 454;
         if (!empty($index['col'])) {
             $line_54a244ae094fd[$_54a244ae094fd] = 455;
             $col = $index['col'];
             $this->context['col'] = $col;
             $line_54a244ae094fd[$_54a244ae094fd] = 456;
         } else {
             $line_54a244ae094fd[$_54a244ae094fd] = 457;
             $col = $index['prop']->getParent();
             $this->context['col'] = $col;
             $line_54a244ae094fd[$_54a244ae094fd] = 458;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 459;
         echo "\n            \$conn = \$this->class_connections[";
         $line_54a244ae094fd[$_54a244ae094fd] = 460;
         var_export($col->getClass());
         echo "];\n            if (empty(\$this->connections[\$conn])) {\n                goto skip_";
         $line_54a244ae094fd[$_54a244ae094fd] = 462;
         echo $next . ";\n            }\n            \$db = \$this->connections[\$conn];\n\n        try {\n            \$col = \$db->createCollection(\$this->ns_by_name[";
         $line_54a244ae094fd[$_54a244ae094fd] = 467;
         var_export($col->getName());
         echo "] . ";
         var_export($col->getName());
         echo "); \n\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 469;
         if ($is_new) {
             $line_54a244ae094fd[$_54a244ae094fd] = 470;
             echo "            \$return = \$col->createIndex(\n                ";
             $line_54a244ae094fd[$_54a244ae094fd] = 471;
             echo agpmedia\FileUtil\dump_array($index['field']);
             echo ",\n                ";
             $line_54a244ae094fd[$_54a244ae094fd] = 472;
             echo agpmedia\FileUtil\dump_array($index['extra']);
             echo "\n            );\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 474;
         } else {
             $line_54a244ae094fd[$_54a244ae094fd] = 475;
             echo "            \$return = \$col->ensureIndex(\n                ";
             $line_54a244ae094fd[$_54a244ae094fd] = 476;
             echo agpmedia\FileUtil\dump_array($index['field']);
             echo ",\n                ";
             $line_54a244ae094fd[$_54a244ae094fd] = 477;
             echo agpmedia\FileUtil\dump_array($index['extra']);
             echo "\n            );\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 479;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 480;
         echo "        } catch (\\MongoException \$e) {\n            // delete index and try to rebuild it\n            \$col->deleteIndex(";
         $line_54a244ae094fd[$_54a244ae094fd] = 482;
         echo agpmedia\FileUtil\dump_array($index['field']);
         echo ");\n\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 484;
         if ($is_new) {
             $line_54a244ae094fd[$_54a244ae094fd] = 485;
             echo "            \$col->createIndex(\n                ";
             $line_54a244ae094fd[$_54a244ae094fd] = 486;
             echo agpmedia\FileUtil\dump_array($index['field']);
             echo ",\n                ";
             $line_54a244ae094fd[$_54a244ae094fd] = 487;
             echo agpmedia\FileUtil\dump_array($index['extra']);
             echo "\n            );\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 489;
         } else {
             $line_54a244ae094fd[$_54a244ae094fd] = 490;
             echo "            \$col->ensureIndex(\n                ";
             $line_54a244ae094fd[$_54a244ae094fd] = 491;
             echo agpmedia\FileUtil\dump_array($index['field']);
             echo ",\n                ";
             $line_54a244ae094fd[$_54a244ae094fd] = 492;
             echo agpmedia\FileUtil\dump_array($index['extra']);
             echo "\n            );\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 494;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 495;
         echo "        }\n        skip_";
         $line_54a244ae094fd[$_54a244ae094fd] = 496;
         echo $next . ":\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 497;
     }
     $line_54a244ae094fd[$_54a244ae094fd] = 498;
     echo "    }\n\n";
     $line_54a244ae094fd[$_54a244ae094fd] = 500;
     foreach ($collections as $collection) {
         $this->context['collection'] = $collection;
         $line_54a244ae094fd[$_54a244ae094fd] = 501;
         echo "\n    protected function set_property_";
         $line_54a244ae094fd[$_54a244ae094fd] = 502;
         echo sha1($collection->getClass()) . "(\$object, \$name, \$value)\n    {\n        switch (\$name) {\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 505;
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             $line_54a244ae094fd[$_54a244ae094fd] = 506;
             echo "        case ";
             var_export($prop->getPHPName());
             echo ":\n        case ";
             $line_54a244ae094fd[$_54a244ae094fd] = 507;
             var_export($prop->getName());
             echo ":\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 508;
             if ($prop->isPublic()) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 509;
                 echo "                \$object->" . $prop->getPHPName() . " = \$value;\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 510;
             } else {
                 $line_54a244ae094fd[$_54a244ae094fd] = 511;
                 echo "                \$property = new \\ReflectionProperty(\$object, ";
                 var_export($prop->getPHPName());
                 echo ");\n                \$property->setAccessible(true);\n                \$property->setValue(\$object, \$value);\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 514;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 515;
             echo "            break;\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 516;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 517;
         echo "        default:\n            throw new \\RuntimeException(\"Missing property {\$name}\");\n        }\n\n        return true;\n    }\n\n    /**\n     *  Populate from \$_POST for collection ";
         $line_54a244ae094fd[$_54a244ae094fd] = 525;
         echo $collection->GetClass() . "\n     */\n    protected function populate_from_array_";
         $line_54a244ae094fd[$_54a244ae094fd] = 527;
         echo sha1($collection->getClass()) . "(\$object, Array \$data)\n    {\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 529;
         if ($collection->GetName()) {
             $line_54a244ae094fd[$_54a244ae094fd] = 530;
             echo "        if (array_key_exists(";
             var_export($collection->GetName());
             echo ", \$data)) {\n            \$data = \$data[";
             $line_54a244ae094fd[$_54a244ae094fd] = 531;
             var_export($collection->getName());
             echo "];\n        }\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 533;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 534;
         echo "        \n";
         $line_54a244ae094fd[$_54a244ae094fd] = 535;
         if ($collection->GetParent()) {
             $line_54a244ae094fd[$_54a244ae094fd] = 536;
             echo "        // populate parent data first\n        \$this->populate_from_array_";
             $line_54a244ae094fd[$_54a244ae094fd] = 537;
             echo sha1($collection->GetParent()->getClass()) . "(\$object, \$data);\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 538;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 539;
         echo "\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 540;
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             $line_54a244ae094fd[$_54a244ae094fd] = 541;
             if ($prop->isId() || $prop->getAnnotation()->has('ReferenceMany,EmbedMany')) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 542;
                 echo "                // we cannot handle " . $prop->GetName() . " at the moment\n";
                 continue;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 545;
             echo "\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 546;
             foreach (array_unique([$prop->getName(), $prop->getPHPName()]) as $var) {
                 $this->context['var'] = $var;
                 $line_54a244ae094fd[$_54a244ae094fd] = 547;
                 echo "\n            if (array_key_exists(";
                 $line_54a244ae094fd[$_54a244ae094fd] = 548;
                 var_export($var);
                 echo ", \$data)) {\n                \$value = \$data[";
                 $line_54a244ae094fd[$_54a244ae094fd] = 549;
                 var_export($var);
                 echo "];\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 550;
                 if ($xcol = $prop->getReferenceCollection()) {
                     $line_54a244ae094fd[$_54a244ae094fd] = 551;
                     $xclass = $collections->ByName()[$xcol]['class'];
                     $this->context['xclass'] = $xclass;
                     $line_54a244ae094fd[$_54a244ae094fd] = 552;
                     if ($xclass) {
                         $line_54a244ae094fd[$_54a244ae094fd] = 553;
                         echo "                        if (!is_array(\$value)) {\n                            throw new \\RuntimeException(\"";
                         $line_54a244ae094fd[$_54a244ae094fd] = 554;
                         var_export($prop->getName());
                         echo " must be an array\");\n                        }\n                        if (";
                         $line_54a244ae094fd[$_54a244ae094fd] = 556;
                         var_export($prop->getType() == 'Reference');
                         echo " && !empty(\$value['_id'])) {\n                            \$value = \$this->connection->getCollection(";
                         $line_54a244ae094fd[$_54a244ae094fd] = 557;
                         var_export($xcol);
                         echo ")\n                                ->findOne(\$value['_id']);\n                        } else {\n";
                         $line_54a244ae094fd[$_54a244ae094fd] = 560;
                         if ($prop->isPublic()) {
                             $line_54a244ae094fd[$_54a244ae094fd] = 561;
                             echo "                                \$oldValue = \$object->" . $prop->getPHPName() . ";\n";
                             $line_54a244ae094fd[$_54a244ae094fd] = 562;
                         } else {
                             $line_54a244ae094fd[$_54a244ae094fd] = 563;
                             echo "                                \$property = new \\ReflectionProperty(\$object, ";
                             var_export($var);
                             echo ");\n                                \$property->setAccessible(true);\n                                \$oldValue = \$property->getValue(\$object);\n";
                             $line_54a244ae094fd[$_54a244ae094fd] = 566;
                         }
                         $line_54a244ae094fd[$_54a244ae094fd] = 567;
                         echo "                            \$docValue =  \$oldValue ?: new \\" . $xclass . ";\n                            \$this->populate_from_array_";
                         $line_54a244ae094fd[$_54a244ae094fd] = 568;
                         echo sha1($xclass) . "(\$docValue, \$value);\n                            \$value = \$docValue;\n                        }\n";
                         $line_54a244ae094fd[$_54a244ae094fd] = 571;
                     }
                     $line_54a244ae094fd[$_54a244ae094fd] = 572;
                 }
                 $line_54a244ae094fd[$_54a244ae094fd] = 573;
                 echo "\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 574;
                 if ($prop->isPublic()) {
                     $line_54a244ae094fd[$_54a244ae094fd] = 575;
                     echo "                    \$object->" . $prop->getPHPName() . " = \$value; \n";
                     $line_54a244ae094fd[$_54a244ae094fd] = 576;
                 } else {
                     $line_54a244ae094fd[$_54a244ae094fd] = 577;
                     echo "                    \$property = new \\ReflectionProperty(\$object, ";
                     var_export($prop->getPHPName());
                     echo ");\n                    \$property->setAccessible(true);\n                    \$property->setValue(\$object, \$value);\n";
                     $line_54a244ae094fd[$_54a244ae094fd] = 580;
                 }
                 $line_54a244ae094fd[$_54a244ae094fd] = 581;
                 echo "    \n            }\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 583;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 584;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 585;
         echo "    }\n\n\n    protected function get_property_";
         $line_54a244ae094fd[$_54a244ae094fd] = 588;
         echo sha1($collection->getClass()) . "(\$object, \$name)\n    {\n        switch (\$name) {\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 591;
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             $line_54a244ae094fd[$_54a244ae094fd] = 592;
             echo "        case ";
             var_export($prop->getPHPName());
             echo ":\n        case ";
             $line_54a244ae094fd[$_54a244ae094fd] = 593;
             var_export($prop->getName());
             echo ":\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 594;
             if ($prop->isPublic()) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 595;
                 echo "                \$return = \$object->" . $prop->getPHPName() . ";\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 596;
             } else {
                 $line_54a244ae094fd[$_54a244ae094fd] = 597;
                 echo "                \$property = new \\ReflectionProperty(\$object, ";
                 var_export($prop->getPHPName());
                 echo ");\n                \$property->setAccessible(true);\n                \$return = \$property->getValue(\$object);\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 600;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 601;
             echo "            break;\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 602;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 603;
         echo "        case '_id': \n            //fallback to get the object ID when it is not part of the object (rare case)\n            if (!empty(\$object->";
         $line_54a244ae094fd[$_54a244ae094fd] = 605;
         echo $instance . ") && \$object->" . $instance . " instanceof ActiveMongo2Mapped) {\n                return \$object->";
         $line_54a244ae094fd[$_54a244ae094fd] = 606;
         echo $instance . "->getOriginal()['_id'];\n            }\n        default:\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 609;
         if ($collection->getParent()) {
             $line_54a244ae094fd[$_54a244ae094fd] = 610;
             echo "                return \$this->get_property_" . sha1($collection->getParent()) . "(\$object, \$name);\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 611;
         } else {
             $line_54a244ae094fd[$_54a244ae094fd] = 612;
             echo "                throw new \\RuntimeException(\"Missing property {\$name}\");\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 613;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 614;
         echo "        }\n\n        return \$return;\n    }\n\n    /**\n     *  Get update object ";
         $line_54a244ae094fd[$_54a244ae094fd] = 620;
         echo $collection->getClass() . " \n     */\n    protected function update_";
         $line_54a244ae094fd[$_54a244ae094fd] = 622;
         echo sha1($collection->getClass()) . "(Array &\$current, Array \$old, \$embed = false)\n    {\n        if (!\$embed && !empty(\$current['_id']) && \$current['_id'] != \$old['_id']) {\n            throw new \\RuntimeException(\"document ids cannot be updated\");\n        }\n\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 628;
         if (!$collection->getParent()) {
             $line_54a244ae094fd[$_54a244ae094fd] = 629;
             echo "            \$change = array();\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 630;
         } else {
             $line_54a244ae094fd[$_54a244ae094fd] = 631;
             echo "            \$change = \$this->update_" . sha1($collection->getParent()) . "(\$current, \$old, \$embed);\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 632;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 633;
         echo "\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 634;
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             $line_54a244ae094fd[$_54a244ae094fd] = 635;
             echo "            \$has_changed = false;\n            if (array_key_exists(";
             $line_54a244ae094fd[$_54a244ae094fd] = 636;
             var_export($prop . '');
             echo ", " . $prop->getPHPBaseVariable('$current') . ")\n                || array_key_exists(";
             $line_54a244ae094fd[$_54a244ae094fd] = 637;
             var_export($prop . '');
             echo ", \$old)) {\n                if (!array_key_exists(";
             $line_54a244ae094fd[$_54a244ae094fd] = 638;
             var_export($prop . '');
             echo ", " . $prop->getPHPBaseVariable('$current') . ")) {\n                    \$change['\$unset'][";
             $line_54a244ae094fd[$_54a244ae094fd] = 639;
             var_export($prop . '');
             echo "] = 1;\n                } else if (!array_key_exists(";
             $line_54a244ae094fd[$_54a244ae094fd] = 640;
             var_export($prop . '');
             echo ", \$old)) {\n                    \$change['\$set'][";
             $line_54a244ae094fd[$_54a244ae094fd] = 641;
             var_export($prop . '');
             echo "] = " . $prop->getPHPVariable('$current') . ";\n                    \$has_changed = true;\n                } else if (";
             $line_54a244ae094fd[$_54a244ae094fd] = 643;
             echo $prop->getPHPVariable('$current') . " !== \$old[";
             var_export($prop . '');
             echo "]) {\n                    \$has_changed = true;\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 645;
             if ($prop->getAnnotation()->has('Inc')) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 646;
                 echo "                        if (empty(\$old[";
                 var_export($prop . '');
                 echo "])) {\n                            \$prev = 0;\n                        } else {\n                            \$prev = \$old[";
                 $line_54a244ae094fd[$_54a244ae094fd] = 649;
                 var_export($prop . '');
                 echo "];\n                        }\n                        \$change['\$inc'][";
                 $line_54a244ae094fd[$_54a244ae094fd] = 651;
                 var_export($prop . '');
                 echo "] = " . $prop->GetPHPVariable('$current') . " - \$prev;\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 652;
             } else {
                 if ($prop->getAnnotation()->has('Embed')) {
                     $line_54a244ae094fd[$_54a244ae094fd] = 653;
                     echo "                        if (" . $prop->getPHPVariable('$current') . "['__embed_class'] != \$old[";
                     var_export($prop . '');
                     echo "]['__embed_class']) {\n                            \$change['\$set'][";
                     $line_54a244ae094fd[$_54a244ae094fd] = 654;
                     var_export($prop . '.');
                     echo " . \$index] = " . $prop->GetPHPVariable('$current') . ";\n                        } else {\n                            \$update = 'update_' . sha1(";
                     $line_54a244ae094fd[$_54a244ae094fd] = 656;
                     echo $prop->getPHPVariable('$current') . "['__embed_class']);\n                            \$diff = \$this->\$update(";
                     $line_54a244ae094fd[$_54a244ae094fd] = 657;
                     echo $prop->getPHPVariable('$current') . ", \$old[";
                     var_export($prop . '');
                     echo "], true);\n                            foreach (\$diff as \$op => \$value) {\n                                foreach (\$value as \$p => \$val) {\n                                    \$change[\$op][";
                     $line_54a244ae094fd[$_54a244ae094fd] = 660;
                     var_export($prop . '.');
                     echo " . \$p] = \$val;\n                                }\n                            }\n                        }\n";
                     $line_54a244ae094fd[$_54a244ae094fd] = 664;
                 } else {
                     if ($prop->getAnnotation()->has('EmbedMany')) {
                         $line_54a244ae094fd[$_54a244ae094fd] = 665;
                         echo "                        // add things to the array\n                        \$toRemove = array_diff_key(\$old[";
                         $line_54a244ae094fd[$_54a244ae094fd] = 666;
                         var_export($prop . '');
                         echo "], " . $prop->getPHPVariable('$current') . ");\n\n                        if (count(\$toRemove) > 0 && \$this->array_unique(\$old[";
                         $line_54a244ae094fd[$_54a244ae094fd] = 668;
                         var_export($prop . '');
                         echo "], \$toRemove)) {\n                            \$change['\$set'][";
                         $line_54a244ae094fd[$_54a244ae094fd] = 669;
                         var_export($prop . '');
                         echo "] = array_values(" . $prop->getPHPVariable('$current') . ");\n                        } else {\n                            foreach (";
                         $line_54a244ae094fd[$_54a244ae094fd] = 671;
                         echo $prop->getPHPVariable('$current') . " as \$index => \$value) {\n                                if (!array_key_exists(\$index, \$old[";
                         $line_54a244ae094fd[$_54a244ae094fd] = 672;
                         var_export($prop . '');
                         echo "])) {\n                                    \$change['\$push'][";
                         $line_54a244ae094fd[$_54a244ae094fd] = 673;
                         var_export($prop . '');
                         echo "]['\$each'][] = \$value;\n                                    continue;\n                                }\n                                if (\$value['__embed_class'] != \$old[";
                         $line_54a244ae094fd[$_54a244ae094fd] = 676;
                         var_export($prop . '');
                         echo "][\$index]['__embed_class']) {\n                                    \$change['\$set'][";
                         $line_54a244ae094fd[$_54a244ae094fd] = 677;
                         var_export($prop . '.');
                         echo " . \$index] = \$value;\n                                } else {\n                                    \$update = 'update_' . sha1(\$value['__embed_class']);\n                                    \$diff = \$this->\$update(\$value, \$old[";
                         $line_54a244ae094fd[$_54a244ae094fd] = 680;
                         var_export($prop . '');
                         echo "][\$index], true);\n                                    foreach (\$diff as \$op => \$value) {\n                                        foreach (\$value as \$p => \$val) {\n                                            \$change[\$op][";
                         $line_54a244ae094fd[$_54a244ae094fd] = 683;
                         var_export($prop . '.');
                         echo " . \$index . '.' . \$p] = \$val;\n                                        }\n                                    }\n                                }\n                            }\n\n                            foreach (\$toRemove as \$value) {\n                                if (!empty(\$value['__instance'])) {\n                                    \$change['\$pull'][";
                         $line_54a244ae094fd[$_54a244ae094fd] = 691;
                         var_export($prop . '');
                         echo "]['__instance']['\$in'][] = \$value['__instance'];\n                                } else {\n                                    \$change['\$pull'][";
                         $line_54a244ae094fd[$_54a244ae094fd] = 693;
                         var_export($prop . '');
                         echo "][] = \$value;\n                                }\n                            }\n                        }\n";
                         $line_54a244ae094fd[$_54a244ae094fd] = 697;
                     } else {
                         if ($prop->getAnnotation()->has('ReferenceMany,Array')) {
                             $line_54a244ae094fd[$_54a244ae094fd] = 698;
                             echo "                        // add things to the array\n                        \$toRemove = array_diff_key(\$old[";
                             $line_54a244ae094fd[$_54a244ae094fd] = 699;
                             var_export($prop . '');
                             echo "], " . $prop->getPHPVariable('$current') . ");\n\n                        if ((count(\$toRemove) > 0 && \$this->array_unique(\$old[";
                             $line_54a244ae094fd[$_54a244ae094fd] = 701;
                             var_export($prop . '');
                             echo "], \$toRemove)) || !\$this->is_array(\$old[";
                             var_export($prop . '');
                             echo "])) {\n                            \$change['\$set'][";
                             $line_54a244ae094fd[$_54a244ae094fd] = 702;
                             var_export($prop . '');
                             echo "] = array_values(" . $prop->getPHPVariable('$current') . ");\n                        } else {\n                            foreach (";
                             $line_54a244ae094fd[$_54a244ae094fd] = 704;
                             echo $prop->getPHPVariable('$current') . " as \$index => \$value) {\n                                if (!array_key_exists(\$index, \$old[";
                             $line_54a244ae094fd[$_54a244ae094fd] = 705;
                             var_export($prop . '');
                             echo "])) {\n";
                             $line_54a244ae094fd[$_54a244ae094fd] = 706;
                             if ($prop->getAnnotation()->has('ReferenceMany')) {
                                 $line_54a244ae094fd[$_54a244ae094fd] = 707;
                                 echo "                                        \$change['\$addToSet'][";
                                 var_export($prop . '');
                                 echo "]['\$each'][] = \$value;\n";
                                 $line_54a244ae094fd[$_54a244ae094fd] = 708;
                             } else {
                                 $line_54a244ae094fd[$_54a244ae094fd] = 709;
                                 echo "                                        \$change['\$push'][";
                                 var_export($prop . '');
                                 echo "]['\$each'][] = \$value;\n";
                                 $line_54a244ae094fd[$_54a244ae094fd] = 710;
                             }
                             $line_54a244ae094fd[$_54a244ae094fd] = 711;
                             echo "                                    continue;\n                                }\n\n                                if (!empty(\$old[";
                             $line_54a244ae094fd[$_54a244ae094fd] = 714;
                             var_export($prop . '');
                             echo "][\$index]['__instance']) && is_array(\$value)) {\n                                    // __instance is an internal variable that helps\n                                    // activemongo2 to remove sub objects from arrays easily.\n                                    // Its value is private to the library and it shouldn't change\n                                    // unless the value of the object changes\n                                    \$diff = \$this->array_diff(\n                                        \$value,\n                                        \$old[";
                             $line_54a244ae094fd[$_54a244ae094fd] = 721;
                             var_export($prop . '');
                             echo "][\$index]\n                                    );\n                                    if (count(\$diff) == 1 && !empty(\$diff['__instance'])) {\n                                        \$value['__instance'] = \$old[";
                             $line_54a244ae094fd[$_54a244ae094fd] = 724;
                             var_export($prop . '');
                             echo "][\$index]['__instance'];\n                                        ";
                             $line_54a244ae094fd[$_54a244ae094fd] = 725;
                             echo $prop->getPHPVariable('$current') . "[\$index] = \$value;\n                                    }\n                                }\n\n                                if (\$old[";
                             $line_54a244ae094fd[$_54a244ae094fd] = 729;
                             var_export($prop . '');
                             echo "][\$index] != \$value) {\n                                    \$change['\$set'][";
                             $line_54a244ae094fd[$_54a244ae094fd] = 730;
                             var_export($prop . '.');
                             echo " . \$index] = \$value;\n                                }\n                            }\n\n                            foreach (\$toRemove as \$value) {\n                                if (!empty(\$value['__instance'])) {\n                                    \$change['\$pull'][";
                             $line_54a244ae094fd[$_54a244ae094fd] = 736;
                             var_export($prop . '');
                             echo "]['__instance']['\$in'][] = \$value['__instance'];\n                                } else {\n                                    \$change['\$pull'][";
                             $line_54a244ae094fd[$_54a244ae094fd] = 738;
                             var_export($prop . '');
                             echo "] = \$value;\n                                }\n                            }\n                        }\n";
                             $line_54a244ae094fd[$_54a244ae094fd] = 742;
                         } else {
                             $line_54a244ae094fd[$_54a244ae094fd] = 743;
                             echo "                        \$change['\$set'][";
                             var_export($prop . '');
                             echo "] = " . $prop->getPHPVariable('$current') . ";\n";
                             $line_54a244ae094fd[$_54a244ae094fd] = 744;
                         }
                     }
                 }
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 745;
             echo "                }\n            }\n\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 748;
             $ann = $prop->getAnnotation();
             $this->context['ann'] = $ann;
             $line_54a244ae094fd[$_54a244ae094fd] = 749;
             if ($ann->has('Array,ReferenceMany,EmbedMany')) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 750;
                 if ($ann->has('Limit')) {
                     $line_54a244ae094fd[$_54a244ae094fd] = 751;
                     echo "                if (\$has_changed && !empty(\$change['\$push'][";
                     var_export($prop . '');
                     echo "])) {\n                    \$change['\$push'][";
                     $line_54a244ae094fd[$_54a244ae094fd] = 752;
                     var_export($prop . '');
                     echo "]['\$slice'] = ";
                     var_export(0 + current($prop->getAnnotation()->getOne('Limit')));
                     echo ";\n                }\n";
                     $line_54a244ae094fd[$_54a244ae094fd] = 754;
                 }
                 $line_54a244ae094fd[$_54a244ae094fd] = 755;
                 if ($ann->has('Sort')) {
                     $line_54a244ae094fd[$_54a244ae094fd] = 756;
                     echo "                if (\$has_changed && !empty(\$change['\$push'][";
                     var_export($prop . '');
                     echo "])) {\n                    \$change['\$sort'][";
                     $line_54a244ae094fd[$_54a244ae094fd] = 757;
                     var_export($prop . '');
                     echo "]['\$sort'] = ";
                     var_export(0 + current($prop->getAnnotation()->getOne('Limit')));
                     echo ";\n                }\n";
                     $line_54a244ae094fd[$_54a244ae094fd] = 759;
                 }
                 $line_54a244ae094fd[$_54a244ae094fd] = 760;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 761;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 762;
         echo "\n        return \$change;\n    }\n\n    protected function get_mapping_";
         $line_54a244ae094fd[$_54a244ae094fd] = 766;
         echo sha1($collection->getClass()) . "() \n    {\n        return array(\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 769;
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             $line_54a244ae094fd[$_54a244ae094fd] = 770;
             echo "                ";
             var_export($prop->getName(true));
             echo " => ";
             var_export($prop->getProperty());
             echo ",\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 771;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 772;
         echo "        );\n    }\n\n    /**\n     *  Populate objects ";
         $line_54a244ae094fd[$_54a244ae094fd] = 776;
         echo $collection->getClass() . " \n     */\n    protected function populate_";
         $line_54a244ae094fd[$_54a244ae094fd] = 778;
         echo sha1($collection->getClass()) . "(\\" . $collection->getClass() . " &\$object, \$data)\n    {\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 780;
         if ($p = $collection->getParent()) {
             $line_54a244ae094fd[$_54a244ae094fd] = 781;
             echo "            \$this->populate_" . sha1($p->getClass()) . "(\$object, \$data);\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 782;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 783;
         echo "\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 784;
         if ($collection->is('GridFs')) {
             $line_54a244ae094fd[$_54a244ae094fd] = 785;
             echo "            if (!\$data instanceof \\MongoGridFsFile) {\n                throw new \\RuntimeException(\"Internal error, trying to populate a GridFSFile with an array\");\n            }\n            \$data_file = \$data;\n            \$data      = \$data->file;\n            if (empty(\$data['metadata'])) {\n                \$data['metadata'] = [];\n            }\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 793;
         } else {
             $line_54a244ae094fd[$_54a244ae094fd] = 794;
             echo "\n            if (!is_array(\$data)) {\n                throw new \\RuntimeException(\"Internal error, trying to populate a document with a wrong data\");\n            }\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 798;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 799;
         echo "\n        \$doc = \$data;\n\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 802;
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             $line_54a244ae094fd[$_54a244ae094fd] = 803;
             if ($prop->getAnnotation()->has('ReferenceMany')) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 804;
                 echo "                if (!empty(" . $prop->getPHPVariable() . ")) {\n                    foreach(";
                 $line_54a244ae094fd[$_54a244ae094fd] = 805;
                 echo $prop->getPHPVariable() . " as \$id => \$sub) {\n                        if (empty(\$sub['__instance']) || !strpos(\$sub['__instance'], \$sub['\$ref'])) {\n                            \$sub['__instance'] = \$sub['\$ref'] . ':' . serialize(\$sub['\$id']) ;\n                        }\n                        ";
                 $line_54a244ae094fd[$_54a244ae094fd] = 809;
                 echo $prop->getPHPVariable() . "[\$id] = \$sub;\n                    }\n                }\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 812;
             } else {
                 if ($prop->getAnnotation()->has('Stream')) {
                     $line_54a244ae094fd[$_54a244ae094fd] = 813;
                     if ($prop->isPublic()) {
                         $line_54a244ae094fd[$_54a244ae094fd] = 814;
                         echo "                    \$object->" . $prop->getPHPName() . " = \$data_file->getResource();\n";
                         $line_54a244ae094fd[$_54a244ae094fd] = 815;
                     } else {
                         $line_54a244ae094fd[$_54a244ae094fd] = 816;
                         echo "                    \$property = new \\ReflectionProperty(\$object, ";
                         var_export($prop->getPHPName());
                         echo ");\n                    \$property->setAccessible(true);\n                    \$property->setValue(\$object, \$data_file->getResource());\n";
                         $line_54a244ae094fd[$_54a244ae094fd] = 819;
                     }
                     continue;
                 }
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 822;
             echo "\n            if (array_key_exists(";
             $line_54a244ae094fd[$_54a244ae094fd] = 823;
             var_export($prop . '');
             echo ", " . $prop->getPHPBaseVariable() . ")) {\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 824;
             foreach ($prop->getCallback('Hydratate') as $h) {
                 $this->context['h'] = $h;
                 $line_54a244ae094fd[$_54a244ae094fd] = 825;
                 echo "                    " . $h->toCode($prop, $prop->getPHPVariable()) . "\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 826;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 827;
             echo "\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 828;
             if ($prop->isPublic()) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 829;
                 echo "                    \$object->" . $prop->getPHPName() . " = " . $prop->getPHPVariable() . ";\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 830;
             } else {
                 $line_54a244ae094fd[$_54a244ae094fd] = 831;
                 echo "                    \$property = new \\ReflectionProperty(\$object, ";
                 var_export($prop->getPHPName());
                 echo ");\n                    \$property->setAccessible(true);\n                    \$property->setValue(\$object, ";
                 $line_54a244ae094fd[$_54a244ae094fd] = 833;
                 echo $prop->getPHPVariable() . ");\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 834;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 835;
             echo "                \n            }\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 837;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 838;
         echo "\n        if (empty(\$object->";
         $line_54a244ae094fd[$_54a244ae094fd] = 839;
         echo $instance . ")) {\n            \$object->";
         $line_54a244ae094fd[$_54a244ae094fd] = 840;
         echo $instance . " = new ActiveMongo2Mapped(";
         var_export($collection->getClass());
         echo ", \$data);\n        } else {\n            \$object->";
         $line_54a244ae094fd[$_54a244ae094fd] = 842;
         echo $instance . "->" . $instance . "_setOriginal(\$data);\n        }\n    }\n\n    /**\n     *  Get reference of  ";
         $line_54a244ae094fd[$_54a244ae094fd] = 847;
         echo $collection->getClass() . " object\n     */\n    protected function get_reference_";
         $line_54a244ae094fd[$_54a244ae094fd] = 849;
         echo sha1($collection->getClass()) . "(\\" . $collection->getClass() . " \$object, \$include = Array())\n    {\n        \$document = \$this->get_array_";
         $line_54a244ae094fd[$_54a244ae094fd] = 851;
         echo sha1($collection->getClass()) . "(\$object);\n        \$extra    = array();\n        if (\$include) {\n            \$extra  = array_intersect_key(\$document, \$include);\n        }\n\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 857;
         if ($cache = $collection->getRefCache()) {
             $line_54a244ae094fd[$_54a244ae094fd] = 858;
             echo "            \$extra = array_merge(\$extra,  array_intersect_key(\n                \$document, \n                ";
             $line_54a244ae094fd[$_54a244ae094fd] = 860;
             var_export($cache);
             echo "\n            ));\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 862;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 863;
         echo "        \n        foreach (\$extra as \$key => \$value) {\n            if (is_object(\$value)) {\n                if (\$value instanceof \\ActiveMongo2\\Reference) {\n                    \$extra[\$key] = \$value->getReference();\n                } else {\n                    \$extra[\$key] = \$this->getReference(\$value);\n                }\n            }\n        }\n\n        return array_merge(array(\n                '\$ref'  => ";
         $line_54a244ae094fd[$_54a244ae094fd] = 875;
         var_export($collection->getName());
         echo ", \n                '\$id'   => \$document['_id'],\n                '__class' => ";
         $line_54a244ae094fd[$_54a244ae094fd] = 877;
         var_export($collection->getClass());
         echo ",\n                '__instance' => ";
         $line_54a244ae094fd[$_54a244ae094fd] = 878;
         var_export($collection->getName());
         echo " . ':' . serialize(\$document['_id']),\n            )\n            , \$extra\n        );\n\n    }\n\n    /**\n     *  Validate ";
         $line_54a244ae094fd[$_54a244ae094fd] = 886;
         echo $collection->getClass() . " object\n     */\n    protected function get_array_";
         $line_54a244ae094fd[$_54a244ae094fd] = 888;
         echo sha1($collection) . "(\\" . $collection . " \$object, \$recursive = true)\n    {\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 890;
         if (!$collection->getParent()) {
             $line_54a244ae094fd[$_54a244ae094fd] = 891;
             echo "            \$doc = array();\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 892;
         } else {
             $line_54a244ae094fd[$_54a244ae094fd] = 893;
             echo "            \$doc = \$recursive ? \$this->get_array_" . sha1($collection->getParent()) . "(\$object) : array();\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 894;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 895;
         echo "\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 896;
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             $line_54a244ae094fd[$_54a244ae094fd] = 897;
             if ($prop->isPublic() && !$prop->isCustom()) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 898;
                 echo "                /* Public property " . $prop->getPHPName() . " -> " . $prop->getName() . " */\n                if (\$object->";
                 $line_54a244ae094fd[$_54a244ae094fd] = 899;
                 echo $prop->getPHPName() . " !== NULL) {\n                    ";
                 $line_54a244ae094fd[$_54a244ae094fd] = 900;
                 echo $prop->getPHPVariable() . " = \$object->" . $prop->getPHPName() . ";\n                }\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 902;
             } else {
                 if ($prop->isCustom()) {
                     $line_54a244ae094fd[$_54a244ae094fd] = 903;
                     echo "                /* public and custom property " . $prop->getPHPName() . " -> " . $prop->getName() . " */\n                if (!empty(\$object->";
                     $line_54a244ae094fd[$_54a244ae094fd] = 904;
                     echo $prop->getPHPName() . ")) {\n                    ";
                     $line_54a244ae094fd[$_54a244ae094fd] = 905;
                     echo $prop->getPHPVariable() . " = \$object->" . $prop->getPHPName() . ";\n                }\n";
                     $line_54a244ae094fd[$_54a244ae094fd] = 907;
                 } else {
                     $line_54a244ae094fd[$_54a244ae094fd] = 908;
                     echo "                \$property = new \\ReflectionProperty(\$object, ";
                     var_export($prop->getPHPName());
                     echo ");\n                \$property->setAccessible(true);\n                ";
                     $line_54a244ae094fd[$_54a244ae094fd] = 910;
                     echo $prop->getPHPVariable() . " = \$property->getValue(\$object);\n";
                     $line_54a244ae094fd[$_54a244ae094fd] = 911;
                 }
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 912;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 913;
         echo "\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 914;
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             $line_54a244ae094fd[$_54a244ae094fd] = 915;
             foreach ($prop->getCallback('DefaultValue') as $default) {
                 $this->context['default'] = $default;
                 $line_54a244ae094fd[$_54a244ae094fd] = 916;
                 echo "                if (empty(" . $prop->getPHPVariable() . ")) {\n                    ";
                 $line_54a244ae094fd[$_54a244ae094fd] = 917;
                 echo $default->toCode($prop) . "\n                    ";
                 $line_54a244ae094fd[$_54a244ae094fd] = 918;
                 echo $prop->getPHPVariable() . " = \$return;\n                }\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 920;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 921;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 922;
         echo "\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 923;
         if ($collection->is('SingleCollection')) {
             $line_54a244ae094fd[$_54a244ae094fd] = 924;
             echo "            // SINGLE COLLECTION\n            ";
             $line_54a244ae094fd[$_54a244ae094fd] = 925;
             echo $collection->getDiscriminator(true)->getPHPVariable() . " = ";
             var_export($collection->getClass());
             echo ";\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 926;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 927;
         echo "\n        if (empty(\$doc['_id'])) {\n            \$oldDoc = \$this->getRawDocument(\$object, false);\n            if (!empty(\$oldDoc['_id'])) {\n                \$doc['_id'] = \$oldDoc['_id'];\n            }\n        }\n\n        return \$doc;\n    }\n\n    protected function reflect_";
         $line_54a244ae094fd[$_54a244ae094fd] = 938;
         echo sha1($collection->getClass()) . "() \n    {\n        \$reflection = array(\n            'class'    => ";
         $line_54a244ae094fd[$_54a244ae094fd] = 941;
         var_export($collection->getClass());
         echo ",\n            'name'     => ";
         $line_54a244ae094fd[$_54a244ae094fd] = 942;
         var_export($collection->getName());
         echo ",\n            'collection'     => ";
         $line_54a244ae094fd[$_54a244ae094fd] = 943;
         var_export($collection->getName());
         echo ",\n            'annotation' => array(\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 945;
         foreach ($collection->getAnnotation() as $ann) {
             $this->context['ann'] = $ann;
             $line_54a244ae094fd[$_54a244ae094fd] = 946;
             echo "            ";
             var_export($ann);
             echo ",\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 947;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 948;
         echo "            ),\n            'properties'  => array(\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 950;
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             $line_54a244ae094fd[$_54a244ae094fd] = 951;
             echo "            ";
             var_export($prop->getPHPName());
             echo " => new \\ActiveMongo2\\Reflection\\Property(array(\n                'property' => ";
             $line_54a244ae094fd[$_54a244ae094fd] = 952;
             var_export($prop . '');
             echo ",\n                'type'     => ";
             $line_54a244ae094fd[$_54a244ae094fd] = 953;
             var_export($prop->getType());
             echo ",\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 954;
             if ($prop->getReferenceCollection()) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 955;
                 echo "                'collection' => ";
                 var_export($prop->getReferenceCollection());
                 echo ",\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 956;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 957;
             echo "                'annotation' => new Annotation(array(\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 958;
             foreach ($prop->getAnnotation() as $ann) {
                 $this->context['ann'] = $ann;
                 $line_54a244ae094fd[$_54a244ae094fd] = 959;
                 echo "                        ";
                 var_export($ann);
                 echo ",\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 960;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 961;
             echo "                )),\n            ), \$this),\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 963;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 964;
         echo "        ));\n\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 966;
         if ($collection->getParent()) {
             $line_54a244ae094fd[$_54a244ae094fd] = 967;
             echo "            \$reflection['properties'] = array_merge(\n                \$this->reflect_";
             $line_54a244ae094fd[$_54a244ae094fd] = 968;
             echo sha1($collection->GetParent()) . "()['properties'], \n                \$reflection['properties']\n            );\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 971;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 972;
         echo "        return \$reflection;\n    }\n\n    /**\n     *  Validate ";
         $line_54a244ae094fd[$_54a244ae094fd] = 976;
         echo $collection->getClass() . " object\n     */\n    protected function validate_";
         $line_54a244ae094fd[$_54a244ae094fd] = 978;
         echo sha1($collection->getClass()) . "(\\" . $collection->getClass() . " \$object)\n    {\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 980;
         if ($collection->getParent()) {
             $line_54a244ae094fd[$_54a244ae094fd] = 981;
             echo "            \$doc = array_merge(\n                \$this->validate_";
             $line_54a244ae094fd[$_54a244ae094fd] = 982;
             echo sha1($collection->getParent()) . "(\$object),\n                \$this->get_array_";
             $line_54a244ae094fd[$_54a244ae094fd] = 983;
             echo sha1($collection->getClass()) . "(\$object, false)\n            );\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 985;
         } else {
             $line_54a244ae094fd[$_54a244ae094fd] = 986;
             echo "            \$doc = \$this->get_array_" . sha1($collection->getClass()) . "(\$object);\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 987;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 988;
         echo "\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 989;
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             $line_54a244ae094fd[$_54a244ae094fd] = 990;
             if ($prop->getAnnotation()->has('Required')) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 991;
                 echo "            if (empty(" . $prop->getPHPVariable() . ")) {\n                throw new \\RuntimeException(\"";
                 $line_54a244ae094fd[$_54a244ae094fd] = 992;
                 echo $prop . '' . " cannot be empty\");\n            }\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 994;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 995;
             echo "            if (!empty(" . $prop->getPHPVariable() . ")) {\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 996;
             foreach ($prop->getCallback('Validate') as $val) {
                 $this->context['val'] = $val;
                 $line_54a244ae094fd[$_54a244ae094fd] = 997;
                 echo "                " . $val->toCode($prop, $prop->getPHPVariable()) . "\n                if (\$return === FALSE) {\n                    throw new \\RuntimeException(\"Validation failed for ";
                 $line_54a244ae094fd[$_54a244ae094fd] = 999;
                 echo $prop . '' . "\");\n                }\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 1001;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 1002;
             echo "\n\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 1004;
             if ($prop->getAnnotation()->has('Date')) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 1005;
                 echo "                    \$_date = \\date_create('" . "@" . "' . " . $prop->getPHPVariable() . "->sec);\n                    if (v\\validate_";
                 $line_54a244ae094fd[$_54a244ae094fd] = 1006;
                 echo sha1($collection->getClass() . "::" . $prop->getPHPName()) . "(\$_date) === false) {\n                        throw new \\RuntimeException(\"Validation failed for ";
                 $line_54a244ae094fd[$_54a244ae094fd] = 1007;
                 echo $prop . '' . "\");\n                    }\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 1009;
             } else {
                 if (!$prop->isCustom() && $validator->hasRules($collection->getClass() . "::" . $prop->getPHPName())) {
                     $line_54a244ae094fd[$_54a244ae094fd] = 1010;
                     echo "                    if (v\\validate_" . sha1($collection->getClass() . "::" . $prop->getPHPName()) . "(" . $prop->getPHPVariable() . ") === false) {\n                        throw new \\RuntimeException(\"Validation failed for ";
                     $line_54a244ae094fd[$_54a244ae094fd] = 1011;
                     echo $prop . '' . "\");\n                    }\n";
                     $line_54a244ae094fd[$_54a244ae094fd] = 1013;
                 }
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 1014;
             echo "            }\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 1015;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 1016;
         echo "\n        return \$doc;\n    }\n\n    protected function update_property_";
         $line_54a244ae094fd[$_54a244ae094fd] = 1020;
         echo sha1($collection->getClass()) . "(\\" . $collection->getClass() . " \$document, \$property, \$value)\n    {\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 1022;
         if ($collection->getParent()) {
             $line_54a244ae094fd[$_54a244ae094fd] = 1023;
             echo "            \$this->update_property_" . sha1($collection->getParent()) . "(\$document, \$property, \$value);\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 1024;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 1025;
         foreach ($collection->getProperties() as $prop) {
             $this->context['prop'] = $prop;
             $line_54a244ae094fd[$_54a244ae094fd] = 1026;
             echo "            if (\$property ==  ";
             var_export($prop . '');
             echo "\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 1027;
             foreach ($prop->getAnnotation()->getAll() as $annotation) {
                 $this->context['annotation'] = $annotation;
                 $line_54a244ae094fd[$_54a244ae094fd] = 1028;
                 echo "                 || \$property == ";
                 var_export('@' . $annotation['method']);
                 echo "\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 1029;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 1030;
             echo "            ) {\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 1031;
             if ($prop->isPublic()) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 1032;
                 echo "                    \$document->" . $prop->getPHPName() . " = \$value;\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 1033;
             } else {
                 $line_54a244ae094fd[$_54a244ae094fd] = 1034;
                 echo "                    \$property = new \\ReflectionProperty(\$object, ";
                 var_export($prop->getPHPNAme());
                 echo ");\n                    \$property->setAccessible(true);\n                    \$property->setValue(\$document, \$value);\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 1037;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 1038;
             echo "            }\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 1039;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 1040;
         echo "    }\n\n\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 1043;
         foreach ($collections->getEvents() as $ev) {
             $this->context['ev'] = $ev;
             $line_54a244ae094fd[$_54a244ae094fd] = 1044;
             echo "    /**\n     *  Code for ";
             $line_54a244ae094fd[$_54a244ae094fd] = 1045;
             echo $ev . " events for objects " . $collection->getClass() . "\n     */\n        protected function event_";
             $line_54a244ae094fd[$_54a244ae094fd] = 1047;
             echo $ev . "_" . sha1($collection->getClass()) . "(\$document, Array \$args)\n        {\n            \$class = \$this->get_class(\$document);\n            if (\$class != ";
             $line_54a244ae094fd[$_54a244ae094fd] = 1050;
             var_export($collection->getClass());
             echo " && !is_subclass_of(\$class, ";
             var_export($collection->getClass());
             echo ")) {\n                throw new \\Exception(\"Class invalid class name (\$class) expecting  \"  . ";
             $line_54a244ae094fd[$_54a244ae094fd] = 1051;
             var_export($collection->getClass());
             echo ");\n            }\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 1053;
             if ($collection->getParent()) {
                 $line_54a244ae094fd[$_54a244ae094fd] = 1054;
                 echo "                \$this->event_" . $ev . "_" . sha1($collection->getParent()->getClass()) . "(\$document, \$args);\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 1055;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 1056;
             echo "\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 1057;
             foreach ($collection->getMethodsByAnnotation($ev) as $method) {
                 $this->context['method'] = $method;
                 $line_54a244ae094fd[$_54a244ae094fd] = 1058;
                 echo "                " . $method->toCode($collection, '$document') . "\n                if (\$return === FALSE) {\n                    throw new \\RuntimeException;\n                }\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 1062;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 1063;
             echo "\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 1064;
             if ($ev == "postCreate" || $ev == "postUpdate") {
                 $line_54a244ae094fd[$_54a244ae094fd] = 1065;
                 echo "                \$col = \$args[1]->getDatabase()->references_queue;\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 1066;
                 ActiveMongo2\Template\Templates::exec("reference/deferred.tpl.php", compact('ev', 'collection'), $this->context);
                 $line_54a244ae094fd[$_54a244ae094fd] = 1067;
                 if ($ev == "postUpdate") {
                     $line_54a244ae094fd[$_54a244ae094fd] = 1068;
                     ActiveMongo2\Template\Templates::exec("reference/update.tpl.php", compact('ev', 'collection'), $this->context);
                     $line_54a244ae094fd[$_54a244ae094fd] = 1069;
                 }
                 $line_54a244ae094fd[$_54a244ae094fd] = 1070;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 1071;
             echo "\n";
             $line_54a244ae094fd[$_54a244ae094fd] = 1072;
             foreach ($collection->getPlugins($ev) as $plugin) {
                 $this->context['plugin'] = $plugin;
                 $line_54a244ae094fd[$_54a244ae094fd] = 1073;
                 echo "                " . $plugin->toCode($collection, '$document') . "\n                if (\$return === FALSE) {\n                    throw new \\RuntimeException;\n                }\n";
                 $line_54a244ae094fd[$_54a244ae094fd] = 1077;
             }
             $line_54a244ae094fd[$_54a244ae094fd] = 1078;
             echo "        }\n    \n";
             $line_54a244ae094fd[$_54a244ae094fd] = 1080;
         }
         $line_54a244ae094fd[$_54a244ae094fd] = 1081;
         echo "\n";
         $line_54a244ae094fd[$_54a244ae094fd] = 1082;
     }
     $line_54a244ae094fd[$_54a244ae094fd] = 1083;
     echo "}\n\nclass ActiveMongo2Mapped\n{\n    protected \$class;\n    protected \$data;\n\n    public function __construct(\$name, Array \$data)\n    {\n        \$this->class = \$name;\n        \$this->data  = \$data;\n    }\n\n    public function getClass()\n    {\n        return \$this->class;\n    }\n\n    public function getOriginal()\n    {\n        return \$this->data;\n    }\n\n    public function ";
     $line_54a244ae094fd[$_54a244ae094fd] = 1106;
     echo $instance . "_setOriginal(Array \$data)\n    {\n        \$this->data = \$data;\n    }\n}\n\n";
     $line_54a244ae094fd[$_54a244ae094fd] = 1112;
     echo substr($validator->getCode(), 5) . "\n\nreturn array(\n    \"ns\" => ";
     $line_54a244ae094fd[$_54a244ae094fd] = 1115;
     var_export(trim($namespace, '\\'));
     echo ",\n    \"validator\" => ";
     $line_54a244ae094fd[$_54a244ae094fd] = 1116;
     var_export($valns);
     echo ",\n);\n";
     array_pop($file_54a244ae094fd);
     if ($return) {
         return ob_get_clean();
     }
 }