public function getRoutePath(array &$requirements_entitys, array &$requirements, array &$requirement_keys, \Symforce\AdminBundle\Compiler\Generator\PhpWriter $generator, \Symforce\AdminBundle\Compiler\Generator\PhpWriter $dispatcher, $with_entity, $path, $break_requirement_key = null, $first_entity_call = true)
 {
     $entity_object_name = '$' . $this->admin_name;
     if ($with_entity) {
         $requirements_entitys[$this->admin_class] = $this->eneity_id_name;
         if ($this->admin->getPropertySlugName()) {
             if ($this->admin->isPropertySlugNullable()) {
                 $requirements[$this->eneity_id_name] = '\\w*';
             } else {
                 $requirements[$this->eneity_id_name] = '\\w+';
             }
         } else {
             $requirements[$this->eneity_id_name] = '\\d+';
         }
         $property_slug_name = $this->admin->getPropertyIdName();
         $property_id_name = $this->admin->getPropertySlugName();
         if (!empty($property_slug_name)) {
             $generator->writeln(sprintf('$options["%s"] = $accessor->getValue(%s, "%s");', $this->eneity_id_name, $entity_object_name, $property_slug_name));
             $generator->writeln(sprintf('if( empty($options["%s"]) ) $options["%s"] = $accessor->getValue(%s, "%s");', $this->eneity_id_name, $this->eneity_id_name, $entity_object_name, $property_id_name));
         } else {
             $generator->writeln(sprintf('$options["%s"] = $accessor->getValue(%s, "%s");', $this->eneity_id_name, $entity_object_name, $property_id_name));
         }
         if ($first_entity_call) {
             $first_entity_call = false;
             $dispatcher->writeln(sprintf('%s = $controller->getPageObject("%s", $request->get("%s") );', $entity_object_name, $this->admin_name, $this->eneity_id_name));
         } else {
             $dispatcher->writeln(sprintf('$controller->setPageObject(%s, "%s", $request->get("%s") );', $entity_object_name, $this->admin_name, $this->eneity_id_name));
         }
     }
     if (!$first_entity_call) {
         if (!isset($requirement_keys[$this->eneity_id_name])) {
             if ($break_requirement_key) {
                 if ($with_entity) {
                     $path = '/' . $this->route_path . '/' . '{' . $this->eneity_id_name . '}' . $path;
                 } else {
                     $path = '/' . $this->route_path . $path;
                 }
             }
         } else {
             if ($break_requirement_key) {
                 // throw new \Exception(sprintf("%s: `{%s}` duplicate, maybe you missing `{%s}` in path `%s`", $this->err_msg, $this->eneity_id_name, $break_requirement_key, $path ));
             }
         }
     }
     if ($this->page_parent_entity) {
         $parent_entity_object_name = '$' . $this->parent_admin->getName();
         if ($with_entity) {
             $generator->writeln(sprintf('%s = $accessor->getValue(%s, "%s");', $parent_entity_object_name, $entity_object_name, $this->property_page_name));
         }
         $parent_generator = $this->page_loader->getPageGeneratorByClass($this->page_parent_entity);
         if (!$first_entity_call) {
             $dispatcher->writeln(sprintf('%s = $accessor->getValue(%s, "%s");', $parent_entity_object_name, $entity_object_name, $this->property_page_name));
         }
         if (!$break_requirement_key && !isset($requirement_keys[$this->eneity_id_name])) {
             $break_requirement_key = $this->eneity_id_name;
         }
         $path = $parent_generator->getRoutePath($requirements_entitys, $requirements, $requirement_keys, $generator, $dispatcher, true, $path, $break_requirement_key, $first_entity_call);
     }
     return $path;
 }
Пример #2
0
 public function path($object = null, $options = array())
 {
     if ($object && !$object instanceof $this->admin_class) {
         if (!$this->admin->tree || 0 !== $object) {
             throw new \Exception(sprintf("expect `%s`, get `%s` ", $this->admin->getClassName(), is_object($object) ? get_class($object) : gettext($object)));
         }
     }
     if (null === $object) {
         if ($this->isRequestObject()) {
             $object = $this->admin->getRouteObject();
         } else {
             if ($this->admin->tree && $this->admin->getTreeObjectId()) {
                 $object = $this->admin->getTreeObject();
             }
         }
     }
     if ($this->admin->tree) {
         if ($this->isRequestObject()) {
             $parent = $this->admin->getReflectionProperty($this->admin->tree['parent'])->getValue($object);
             if ($parent) {
                 $options['sf_admin_tree_parent'] = $this->admin->getReflectionProperty($this->admin->getPropertyIdName())->getValue($parent);
             } else {
                 $options['sf_admin_tree_parent'] = 0;
             }
             // todo check if it match $this->admin->getTreeObjectId(
         } else {
             if ($object) {
                 $options['sf_admin_tree_parent'] = $this->admin->getReflectionProperty($this->admin->getPropertyIdName())->getValue($object);
             } else {
                 if (0 === $object) {
                     $options['sf_admin_tree_parent'] = 0;
                 } else {
                     $options['sf_admin_tree_parent'] = $this->admin->getTreeObjectId();
                 }
             }
         }
     }
     if ($this->admin->workflow) {
         if (!isset($options['admin_route_workflow'])) {
             $options['admin_route_workflow'] = $this->admin->getRouteWorkflow();
             if (!$options['admin_route_workflow']) {
                 unset($options['admin_route_workflow']);
             }
         }
     }
     if ($this->isRequestObject()) {
         return $this->admin_loader->generateRoutePathWithObject($this->getAdminRouteName(), $object, $options);
     }
     return $this->admin_loader->generateRoutePathWithoutObject($this->getAdminRouteName(), $options);
 }
 public function generateActionPath(\Symforce\AdminBundle\Compiler\Cache\ActionCache $action, \Symfony\Component\Routing\RouteCollection $route_admin_collection)
 {
     $class = $this->loader->getCompileClass();
     $writer = $this->loader->getCompileAdminWriter();
     $route_name = $action->getAdminRouteName();
     $admin_name = $this->admin->getName();
     $admin_class = $this->admin->getClassName();
     $generator = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter();
     $dispatcher = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter();
     $requirement = array();
     $generator->write('function(PropertyAccessorInterface $accessor, AdminLoader $loader, ');
     if ($action->isRequestObject()) {
         $generator->write(sprintf('%s $%s_object, ', '\\' . $admin_class, $admin_name));
     }
     $generator->writeln('array $options = array()){')->indent();
     $dispatcher->writeln('function(AdminLoader $loader, Request $request){')->indent();
     $dispatcher->writeln(sprintf('$loader->setRouteAdminAction("%s", "%s");', $admin_name, $action->getName()));
     $dispatcher->writeln('$_sf_admin_route_parameters = array();');
     $dispatcher->writeln('$_sf_admin_route_parents = array();');
     $path = array();
     $route_generator_parents = array();
     $this->getRoutePatentGenerator($route_generator_parents);
     foreach ($route_generator_parents as $route_parent_generator) {
         $route_parent_generator->generateRouteDispatcher($dispatcher, $requirement, $path);
     }
     $dispatcher->writeln(sprintf('$%s = $loader->getAdminByClass("%s");', $admin_name, $admin_class));
     $dispatcher->writeln(sprintf('$%s->setRouteParameters($_sf_admin_route_parameters);', $admin_name));
     $dispatcher->writeln(sprintf('$%s->setRouteParents($_sf_admin_route_parents);', $admin_name));
     $route_parent = $this->admin->getRouteParent();
     if ($route_parent) {
         $admin_name = $this->admin->getName();
         $dispatcher->writeln(sprintf('$%s->setRouteParent($%s);', $admin_name, $route_parent->getName()));
         $dispatcher->writeln(sprintf('$%s->setRouteParentProperty("%s");', $admin_name, $this->admin->getRouteParentProperty()));
     }
     if ($action->isRequestObject()) {
         $dispatcher->writeln(sprintf('$%s->setRouteObjectId( $request->get("%s_id") );', $admin_name, $admin_name));
     }
     $dispatcher->writeln(sprintf('$action = $%s->getAction("%s");', $admin_name, $action->getName()));
     if ($action->isRequestObject()) {
         $generator->writeln(sprintf('$options["%s_id"] = $accessor->getValue($%s_object, "%s") ;', $admin_name, $admin_name, $this->admin->getPropertyIdName()));
     }
     $this->generateRouteGenerator($generator, $action->isRequestObject(), true);
     if ($route_parent) {
         $route_parent_renerator = $this->loader->getAdminRouteGenerator($route_parent->getName());
         if (!$route_parent_renerator->isSingleChild($this->admin_name)) {
             $path[] = 'of_' . $this->admin->getRouteParentProperty();
         }
     }
     $path[] = $admin_name;
     if ($this->admin->tree) {
         $id = 'sf_admin_tree_parent';
         $requirement[$id] = '\\d+';
         $generator->writeln(sprintf('if( !isset($options["%s"]) ) $options["%s"] = 0;', $id, $id));
         $path[] = sprintf('{%s}', $id);
         $dispatcher->writeln(sprintf('$%s->setTreeObjectId( $request->get("%s") );', $admin_name, $id));
     }
     if ($this->admin->workflow) {
         $id = 'admin_route_workflow';
         $requirement[$id] = '\\w+';
         $path[] = sprintf('{%s}', $id);
         $dispatcher->writeln(sprintf('$%s->setRouteWorkflow( $request->get("%s") );', $admin_name, $id));
         $generator->writeln(sprintf('if( !isset($options["%s"]) ) $options["%s"] = $loader->getAdminByClass("%s")->getRouteWorkflow();', $id, $id, $admin_class));
     }
     $owner = self::getPropertyValue($this->admin, 'property_owner_name');
     if ($owner) {
         $id = 'admin_route_owner_identity';
         $requirement[$id] = '\\w+';
         $path[] = sprintf('{%s}', $id);
         $dispatcher->writeln(sprintf('$%s->setRouteOwnerIdentity( $request->get("%s") );', $admin_name, $id));
         $generator->writeln(sprintf('if( !isset($options["%s"]) ) $options["%s"] = $loader->getAdminByClass("%s")->getRouteOwnerIdentity();', $id, $id, $admin_class));
         $id = 'admin_route_owner_id';
         $requirement[$id] = '\\d+';
         $path[] = sprintf('{%s}', $id);
         $dispatcher->writeln(sprintf('$%s->setRouteOwnerId( $request->get("%s") );', $admin_name, $id));
         $generator->writeln(sprintf('if( !isset($options["%s"]) ) $options["%s"] = $loader->getAdminByClass("%s")->getRouteOwnerId();', $id, $id, $admin_class));
     }
     $path[] = $action->getName();
     if ($action->isRequestObject()) {
         $path[] = sprintf('{%s_id}', $admin_name);
     }
     if ($action->isListAction()) {
         $id = 'admin_list_page';
         $requirement[$id] = '\\d+';
         $generator->writeln(sprintf('if( !isset($options["%s"]) ) $options["%s"] = 1;', $id, $id));
         $path[] = sprintf('{%s}.html', $id);
         $dispatcher->writeln(sprintf('$action->setPageNumber( $request->get("%s") );', $id));
     }
     $path = '/' . join('/', $path);
     $route_config = array('_controller' => 'Symforce\\AdminBundle\\Controller\\AdminController::adminAction', '_sf_route_name' => $route_name);
     $route = new \Symfony\Component\Routing\Route($path, $route_config, $requirement);
     $route_admin_collection->add($route_name, $route);
     $dispatcher->writeln('return $action;');
     $generator->writeln(sprintf('// path = %s ', $path));
     $generator->writeln('return $options;');
     $writer->write(sprintf('$this->admin["%s"]["generator"] = ', $route_name))->indent()->write($generator->getContent())->outdent()->writeln("};");
     $writer->write(sprintf('$this->admin["%s"]["dispatcher"] = ', $route_name))->indent()->write($dispatcher->getContent())->outdent()->writeln('};');
 }