public function generateRouteGenerator(\Symforce\AdminBundle\Compiler\Generator\PhpWriter $generator, $use_object, $is_first = false)
 {
     $admin_name = $this->admin->getName();
     $route_parent = $this->admin->getRouteParent();
     if ($route_parent) {
         if ($is_first) {
             $generator->writeln(sprintf('$%s = $loader->getAdminByClass("%s");', $admin_name, $this->admin->getClassName()));
             $generator->writeln(sprintf('$_sf_admin_route_parameters = $%s->getRouteParameters();', $admin_name));
         }
         $route_parent_name = $route_parent->getName();
         $parent_property = $this->admin->getRouteParentProperty();
         $generator->writeln(sprintf('if( !isset($options["%s_id"] )) {', $route_parent_name))->indent();
         if ($use_object) {
             $generator->writeln(sprintf('$%s_object = $%s_object ? $accessor->getValue($%s_object, "%s") : null ;', $route_parent_name, $admin_name, $admin_name, $parent_property))->writeln(sprintf('if( $%s_object ) {', $route_parent_name))->indent()->writeln(sprintf('$options["%s_id"] = $accessor->getValue($%s_object, "%s") ;', $route_parent_name, $route_parent_name, $route_parent->getPropertyIdName()))->outdent()->writeln(sprintf('} else if( isset($_sf_admin_route_parameters["%s_id"] )) {', $route_parent_name))->indent()->writeln(sprintf('$options["%s_id"] = $_sf_admin_route_parameters["%s_id"] ;', $route_parent_name, $route_parent_name))->outdent()->writeln('} else {')->indent()->writeln(sprintf(' $options["%s_id"] = $loader->getAdminByClass("%s")->getRouteObjectId() ;', $route_parent_name, $route_parent->getClassName()))->outdent()->writeln('}');
         } else {
             $generator->writeln(sprintf('if( isset($_sf_admin_route_parameters["%s_id"] )) {', $route_parent_name))->indent()->writeln(sprintf(' $options["%s_id"] = $_sf_admin_route_parameters["%s_id"] ;', $route_parent_name, $route_parent_name))->outdent()->writeln('} else {')->indent()->writeln(sprintf(' $options["%s_id"] = $loader->getAdminByClass("%s")->getRouteObjectId() ;', $route_parent_name, $route_parent->getClassName()))->outdent()->writeln('} ');
         }
         $generator->outdent()->writeln('}');
         $route_parent_generator = $this->loader->getAdminRouteGenerator($route_parent_name);
         $route_parent_generator->generateRouteGenerator($generator, $use_object);
     }
 }
Exemplo n.º 2
0
 public function onController(Controller $controller, Request $request)
 {
     throw new \Exception(sprintf("%s:%s action %s:onController is not implemented", $this->admin->getName(), $this->admin->getClassName(), $this->name));
 }
Exemplo n.º 3
0
 /**
  * {@inheritDoc}
  */
 public function validate(\Symfony\Component\Form\FormEvent $event)
 {
     $options = $this->options;
     $form = $event->getForm();
     $data = $form->getData();
     if (!$data) {
         return;
     }
     $admin = $this->admin;
     $parent = $form->getParent();
     $object = $parent->getData();
     $unit = '';
     if (isset($options['unit'])) {
         $unit = $this->admin->trans($options['unit'][0], null, $options['unit'][1]);
     }
     if (isset($options['greater_than'])) {
         if (property_exists($object, $options['greater_than'])) {
             $property = $options['greater_than'];
             if ($parent->has($property)) {
                 $_data = $parent->get($property)->getData();
             } else {
                 $_data = $admin->getReflectionProperty($property)->getValue($object);
             }
             $label = $admin->getPropertyLabel($property);
             if ($_data && !is_numeric($_data)) {
                 $error = sprintf("配置错误, 比较目标(%s,%s) 不是数值类型", $label, $property);
                 $form->addError(new \Symfony\Component\Form\FormError($error));
             } else {
                 if (!$_data || $data < $_data) {
                     $error = sprintf("该值应该大于%s %s%s", $label, $_data, $unit);
                     $form->addError(new \Symfony\Component\Form\FormError($error));
                 }
             }
         } else {
             $error = sprintf("配置错误, 比较目标(%s) 不存是%s属性", $property, $this->admin->getClassName());
             $form->addError(new \Symfony\Component\Form\FormError($error));
         }
     }
     if (isset($options['less_than'])) {
         if (property_exists($object, $options['less_than'])) {
             $property = $options['less_than'];
             if ($parent->has($property)) {
                 $_data = $parent->get($property)->getData();
             } else {
                 $_data = $admin->getReflectionProperty($property)->getValue($object);
             }
             $label = $admin->getPropertyLabel($property);
             if ($_data && !is_numeric($_data)) {
                 $error = sprintf("配置错误, 比较目标(%s,%s) 不是数值类型", $label, $property);
                 $form->addError(new \Symfony\Component\Form\FormError($error));
             } else {
                 if (!$_data || $data > $_data) {
                     $error = sprintf("该值应该小于%s %s%s", $label, $_data, $unit);
                     $form->addError(new \Symfony\Component\Form\FormError($error));
                 }
             }
         } else {
             $error = sprintf("配置错误, 比较目标(%s) 不存是%s属性", $property, $this->admin->getClassName());
             $form->addError(new \Symfony\Component\Form\FormError($error));
         }
     }
 }
 public function addRoute(\ReflectionMethod $m, \Symforce\AdminBundle\Compiler\Annotation\Route $annot, array $as = null)
 {
     $this->err_msg = sprintf("%s->%s(@%s): ", $m->getDeclaringClass()->getName(), $m->getName(), self::ROUTE_ANNOT_CLASS);
     $admin_name = null;
     if ($annot->admin) {
         if (!preg_match('/^\\w+$/', $annot->admin)) {
             throw new \Exception(sprintf("%s admin `%s` invalid ", $this->err_msg, $annot->admin));
         }
         $admin_name = $annot->admin;
     } else {
         $admin_name = $this->page_loader->getControlerOwnerName($m);
     }
     $action_name = null;
     if ($annot->action) {
         if (!preg_match('/^\\w+$/', $annot->action)) {
             throw new \Exception(sprintf("%s admin `%s` invalid ", $this->err_msg, $annot->action));
         }
         $action_name = $annot->action;
     } else {
         $action_name = $annot->action ?: strtolower(preg_replace('/Action$/', '', $m->getName()));
     }
     if ($admin_name !== $this->admin_name) {
         $this->page_loader->addOtherControlerAction($admin_name, $action_name, $m);
         return;
     }
     $this->page_loader->addPageAction($admin_name, $action_name, $m);
     $controller_name = basename(str_replace('\\', '/', $m->getDeclaringClass()->getName()));
     if (!$annot->name) {
         $annot->name = $admin_name . '.' . $action_name;
     }
     if ($annot->alias) {
         if (!preg_match('/^(.+?)\\s*\\:\\s*(.+)$/', $annot->alias, $ms)) {
             throw new \Exception(sprintf("%s alias `%s` invalid ", $this->err_msg, $annot->alias));
         }
         $admin_alias = $ms[1];
         $action_alias = $ms[2];
         if (!$this->loader->hasAdminName($admin_alias)) {
             throw new \Exception(sprintf("%s admin name `%s` not exists in alias `%s`", $this->err_msg, $admin_alias, $annot->alias));
         }
         $page_generator_alias = $this->page_loader->getPageGeneratorByName($admin_alias);
         $page_generator_alias->addAlias($action_alias, $admin_name, $action_name, $m);
     }
     if (!$annot->template) {
         $annot->template = $this->admin->getBundleName() . ':' . preg_replace('/Controller$/', '', $controller_name) . ':' . $admin_name . '.' . $action_name . '.html.twig';
     }
     $requirement_keys = array();
     if (!$annot->path) {
         if ('index' === $action_name && !$annot->entity) {
             $annot->path = '/' . $this->route_path . '/';
         } else {
             if ('view' === $action_name && null === $annot->entity) {
                 $annot->entity = true;
             }
             if ($annot->entity) {
                 $annot->path = '/' . $this->route_path . '/' . $action_name . '/{' . $this->eneity_id_name . '}';
                 $requirement_keys[$this->eneity_id_name] = true;
             } else {
                 $annot->path = '/' . $this->route_path . '/' . $action_name . '/';
             }
         }
     } else {
         $annot->path = '/' . ltrim($annot->path, '/');
         if (preg_match_all('/\\{(.+?)\\}/', $annot->path, $ms)) {
             foreach ($ms[1] as $requirement_key) {
                 if (isset($requirement_keys[$requirement_key])) {
                     throw new \Exception(sprintf("%s : `%s` duplicate in path `%s`", $this->err_msg, $annot->path, $requirement_key));
                 }
                 $requirement_keys[$requirement_key] = true;
             }
         }
         if (isset($requirement_keys['entity_id'])) {
             $annot->path = str_replace('{entity_id}', '{' . $this->eneity_id_name . '}', $annot->path);
             $annot->entity = true;
             $requirement_keys[$this->eneity_id_name] = true;
             unset($requirement_keys['entity_id']);
         }
         if (isset($requirement_keys[$this->eneity_id_name])) {
             $annot->entity = true;
         }
     }
     $generator = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter();
     $dispatcher = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter();
     $dispatcher_args = array();
     $entity_object_name = '$' . $this->admin_name;
     $generator->write('function(\\Symfony\\Component\\PropertyAccess\\PropertyAccessorInterface $accessor,');
     if ($annot->entity) {
         $generator->write(sprintf('%s %s, ', '\\' . $this->admin_class, $entity_object_name));
     } else {
         if ($this->page_parent_entity) {
             $entity_object_name = '$' . $this->parent_admin->getName();
             $generator->write(sprintf('%s %s, ', '\\' . $this->parent_admin->getClassName(), $entity_object_name));
         }
     }
     $generator->writeln('array $options = array() ){')->indent();
     $dispatcher->writeln('function(\\Symfony\\Component\\PropertyAccess\\PropertyAccessorInterface $accessor, \\' . $m->getDeclaringClass()->getName() . ' $controller, \\Symfony\\Component\\HttpFoundation\\Request $request){')->indent();
     $dispatcher->writeln(sprintf('$controller->setPageAdmin("%s");', $this->admin_name));
     $requirements = array();
     $requirements_entitys = array();
     $route_config = array('_controller' => $m->getDeclaringClass()->getName() . '::dispatchAction', '_sf_web_page' => $admin_name . ':' . $action_name);
     $path = $this->getRoutePath($requirements_entitys, $requirements, $requirement_keys, $generator, $dispatcher, $annot->entity, $annot->path);
     $defaults = array();
     if ($annot->defaults) {
         $defaults = $annot->defaults;
     }
     foreach ($requirements_entitys as $requirement_key) {
         if (isset($requirement_keys[$requirement_key])) {
             unset($requirement_keys[$requirement_key]);
         }
     }
     if ($annot->requirements) {
         foreach ($annot->requirements as $requirement_key => $value) {
             if (isset($requirements[$requirement_key])) {
                 throw new \Exception(sprintf("%s `%s` duplicate requirements:%s ", $this->err_msg, $path, $requirement_key));
             }
             $requirements[$requirement_key] = $value;
         }
     }
     $add_default = function ($i, $name, $value) use(&$dispatcher_args, $generator) {
         $dispatcher_args[$i] = sprintf('$request->get("%s", %s)', $name, var_export($value, 1));
         $generator->writeln(sprintf(' if( !isset($options["%s"]) ) $options["%s"] = %s;', $name, $name, var_export($value, 1)));
     };
     foreach ($m->getParameters() as $i => $p) {
         $requirement_key = $p->getName();
         if (isset($requirement_keys[$requirement_key])) {
             if (isset($defaults[$requirement_key])) {
                 $add_default($i, $requirement_key, $defaults[$requirement_key]);
                 unset($defaults[$requirement_key]);
             } else {
                 if ($p->isDefaultValueAvailable()) {
                     $add_default($i, $requirement_key, $p->getDefaultValue());
                 } else {
                     $dispatcher_args[$i] = sprintf('$request->get("%s")', $p->getName());
                 }
             }
             unset($requirement_keys[$requirement_key]);
         } else {
             if (!$p->isDefaultValueAvailable()) {
                 // check if is request
                 if ($p->getClass()) {
                     $requirement_class = $p->getClass()->name;
                     if ('Symfony\\Component\\HttpFoundation\\Request' === $requirement_class) {
                         $dispatcher_args[$i] = '$request';
                     } else {
                         if (isset($requirements_entitys[$requirement_class])) {
                             $dispatcher_args[$i] = '$' . $this->loader->getNameByClass($requirement_class);
                         } else {
                             if ($this->loader->hasSuperAdminClass($requirement_class)) {
                                 $super_class = $this->loader->getSuperAdminClass($requirement_class);
                                 $dispatcher_args[$i] = '$controller->getAdminByClass(' . var_export($super_class, 1) . ')';
                             } else {
                                 throw new \Exception(sprintf("%s parameter:(%s %s) not defined on path:%s", $this->err_msg, $requirement_class, $requirement_key, $annot->path));
                             }
                         }
                     }
                 } else {
                     if (isset($defaults[$requirement_key])) {
                         $add_default($i, $requirement_key, $defaults[$requirement_key]);
                         unset($defaults[$requirement_key]);
                     } else {
                         throw new \Exception(sprintf("`%s` no value", $requirement_key));
                     }
                 }
             } else {
                 // maybe need add to other place
                 $dispatcher_args[$i] = var_export($p->getDefaultValue(), 1);
             }
         }
     }
     if (!empty($defaults)) {
         throw new \Exception(sprintf("%s defaults:%s not defined on path:%s", $this->err_msg, json_encode($defaults), $annot->path));
     }
     if (!empty($requirement_keys)) {
         throw new \Exception(sprintf("%s path option `{%s}` not defined on path:%s", $this->err_msg, json_encode($requirement_keys), $annot->path));
     }
     $this->action_cache[$action_name] = $annot;
     $route = new \Symfony\Component\Routing\Route($path, $route_config, $requirements);
     $this->page_loader->addPageRoute($admin_name, $action_name, $route, $m, $annot);
     $page_class = $this->page_loader->getCompileClass();
     $writer = $page_class->getLazyWriter();
     $generator->writeln('return $options;');
     $dispatcher->writeln(sprintf('return $controller->%s(%s);', $m->getName(), join(', ', $dispatcher_args)));
     $writer->write(sprintf('$this->cache["%s:%s"]["generator"] = ', $admin_name, $action_name))->indent()->write($generator->getContent())->writeln('}')->outdent()->writeln(";");
     $writer->write(sprintf('$this->cache["%s:%s"]["dispatcher"] = ', $admin_name, $action_name))->indent()->write($dispatcher->getContent())->writeln('}')->outdent()->writeln(";");
     return true;
 }