public function getFormOptions() { $options = parent::getFormOptions(); if (0) { $writer = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter(); $writer->writeln('new \\Symfony\\Component\\Security\\Core\\Validator\\Constraints\\UserPassword(array(')->indent()->outdent()->writeln('))'); $options['constraints'][] = $this->compilePhpCode($writer->getContent()); if (null !== $this->repeat) { $options['type'] = 'password'; //$options['invalid_message'] = 'The password fields must match.', $options['first_options'] = array('label' => $_options['label'], 'required' => $_options['required']); $options['second_options'] = array('label_render' => false, 'required' => $_options['required']); } } if (!property_exists($this->admin_object->class_name, $this->salt)) { $this->throwError("salt property(%s), not exist in class(%s)", $this->salt, $this->admin_object->class_name); } if (!property_exists($this->admin_object->class_name, $this->real_password)) { $this->throwError("real_password property(%s), not exist in class(%s)", $this->real_password, $this->admin_object->class_name); } if ($this->class_property === $this->real_password) { $this->throwError("real_password property(%s) can not be it self, class(%s)", $this->real_password, $this->admin_object->class_name); } if ($this->class_property === $this->salt) { $this->throwError("salt property(%s) can not be it self, class(%s)", $this->salt, $this->admin_object->class_name); } $options['salt_property'] = $this->salt; $options['password_property'] = $this->real_password; $options['required'] = $this->compilePhpCode('$action->isCreateAction()'); return $options; }
protected function compileGroup(\Symforce\AdminBundle\Entity\MenuGroup $group, $path) { $writer = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter(); $id = 'sf_menu_group_' . $group->slug; $writer->writeln(sprintf('<!-- sf menu group( %s ) -->', $group->name)); if ($group->group_css) { $group_css = $group->group_css; $child_css = $group->child_css; $writer->writeln(sprintf('<style type="text/css">%s</style>', $group_css)); } $writer->writeln(sprintf('<div id="%s" class="sf_menu_box %s">', $id, $group->group_class)); $writer->writeln(sprintf('<%s>', $group->group_tag)); foreach ($group->menu_list as $menu) { if ($menu->getParent()) { continue; } if ($menu->disabled) { continue; } $this->compileMenu($menu, $writer); } $writer->writeln(sprintf('</%s>', $group->group_tag)); $writer->writeln(sprintf('</div>')); $writer->writeln(sprintf('<!-- /sf menu group( %s ) -->', $group->name)); \Dev::write_file($path, $writer->getContent()); }
public function getFormOptions() { $options = parent::getFormOptions(); $writer = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter(); $writer->writeln('new \\Symforce\\AdminBundle\\Form\\Constraints\\Json(array(')->indent()->outdent()->writeln('))'); $options['constraints'][] = $this->compilePhpCode($writer->getContent()); return $options; }
public function getFormOptions() { $options = parent::getFormOptions(); $writer = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter(); $writer->writeln('new \\Symfony\\Component\\Validator\\Constraints\\Email(array(')->indent()->writeln('"checkMX" => false , ')->outdent()->writeln('))'); $options['constraints'][] = $this->compilePhpCode($writer->getContent()); return $options; }
public function getFormOptions() { $options = parent::getFormOptions(); if (null !== $this->max_length || null !== $this->max_length) { $max = $this->max_length ?: 0xff; $min = $this->min_length ?: 0; $writer = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter(); $writer->writeln('new \\Symfony\\Component\\Validator\\Constraints\\Url(array(')->indent()->outdent()->writeln('))'); $options['constraints'][] = $this->compilePhpCode($writer->getContent()); } return $options; }
public function getFormOptions() { $options = parent::getFormOptions(); if (null !== $this->min || null !== $this->max) { $max = $this->max ?: 0x7fffff; $min = $this->min ?: 0; $writer = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter(); $writer->writeln('new \\Symfony\\Component\\Validator\\Constraints\\Range(array(')->indent()->writeln(sprintf(' "min" => %d, ', $min))->writeln(sprintf(' "max" => %d, ', $max))->outdent()->writeln('))'); $options['constraints'][] = $this->compilePhpCode($writer->getContent()); $options['attr']['min'] = $min; $options['attr']['max'] = $max; } if ($this->greater_than || $this->less_than) { $writer = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter(); $writer->writeln('new \\Symforce\\AdminBundle\\Form\\Constraints\\CompareValidator(array(')->indent(); if ($this->greater_than) { $writer->writeln(sprintf(' "greater_than" => %s, ', var_export($this->greater_than, 1))); } if ($this->less_than) { $writer->writeln(sprintf(' "less_than" => %s, ', var_export($this->less_than, 1))); } if ($this instanceof Range) { if ($this->_unit) { $writer->writeln(sprintf(' "unit" => %s, ', var_export(array($this->_unit->getPath(), $this->_unit->getDomain()), 1))); } } $writer->outdent()->writeln('), $this)'); $options['subscribers'][] = $this->compilePhpCode($writer->getContent()); } return $options; }
public function writeCache() { static $_psr4_map = null; if (null === $_psr4_map) { $_psr4_file = dirname((new \ReflectionClass('Composer\\Autoload\\ClassLoader'))->getFileName()) . '/autoload_psr4.php'; if (!file_exists($_psr4_file)) { throw new \Exception(sprintf("psr4 file(%s) not exits!", $_psr4_file)); } $_psr4_map = (include $_psr4_file); } $_class_file = null; foreach ($_psr4_map as $_namespace => $_namespace_dir) { if (!empty($_namespace_dir)) { $_pos = strpos($this->getName(), $_namespace); if (0 === $_pos) { $_class_file = $_namespace_dir[0] . '/' . str_replace('\\', '/', substr($this->getName(), strlen($_namespace))) . '.php'; } } } if (!$_class_file) { throw new \Exception(sprintf("can not resolve file for class(%s) by psr4 rule!", $this->getName())); } $shortName = pathinfo($_class_file, \PATHINFO_FILENAME); $namespace = $this->getNamespace(); $writer = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter(); $writer->writeln("<?php\n"); if (!empty($namespace)) { $writer->writeln("namespace " . $namespace . ";\n"); } $imports = $this->getUseStatements(); foreach ($imports as $alias => $use) { $_alias = substr($use, -1 - strlen($alias)); if ($_alias == '\\' . $alias) { $writer->writeln(sprintf("use %s ;", $use)); } else { $writer->writeln(sprintf("use %s as %s ;", $use, $alias)); } } $writer->writeln('')->writeln('/**')->writeln(' * This code has been auto-generated by the SymforceAdminBundule')->writeln(' * Manual changes to it will be lost.')->writeln(' */'); if ($this->isAbstract()) { $writer->write('abstract '); } else { if ($this->isFinal()) { $writer->write('final '); } } $writer->write('class ' . $shortName); if ($this->getParentClassName()) { $writer->write(' extends ' . $this->getParentClassName()); } $writer->writeln(' {')->indent(); $lazy_writer = $this->getLazyWriter(); foreach ($this->getProperties() as $property) { $property->writeCache($lazy_writer, $writer); } foreach ($this->lazy_properties as $name => $value) { $writer->writeln("\npublic \${$name} = " . $this->propertyEncode($value) . " ;"); // $lazy_writer->writeln( '$this->' . $name . ' = ' . . ' ; ' ); } if ($this->hasMethod('__wakeup')) { $lazy_writer->writeln($this->getMethod('__wakeup')->getBody()); $this->getMethod('__wakeup')->setBody($lazy_writer->getContent()); } else { $this->setMethod(\CG\Generator\PhpMethod::create('__wakeup')->setFinal(true)->setVisibility('protected')->setBody($lazy_writer->getContent())); } foreach ($this->getMethods() as $method) { if ($method instanceof PhpMethod) { $method->flushLazyCode(); $_body = $method->getWriter()->getContent(); } else { $_body = $method->getBody(); } $writer->write("\n"); if ($method->getDocblock()) { $writer->writeln($method->getDocblock()); } if ($method->isFinal()) { $writer->write('final '); } $writer->write($method->getVisibility())->write(' function ')->write($method->getName()); $ps = $method->getParameters(); if (empty($ps)) { $writer->write('()'); } else { $writer->writeln('(')->indent(); foreach ($method->getParameters() as $i => $p) { if ($p->getType()) { if (in_array($p->getType(), array('mixed'))) { $writer->write('/** @var ' . $p->getType() . ' */'); } else { $writer->write($p->getType() . ' '); } } if ($p->isPassedByReference()) { $writer->write(' & '); } $writer->write(' $')->write($p->getName()); if ($p->hasDefaultValue()) { $writer->write(' = ' . json_encode($p->getDefaultValue())); } if ($i < count($ps) - 1) { $writer->writeln(","); } else { $writer->write("\n"); } } $writer->writeln(')')->outdent(); } $writer->writeln('{')->indent()->writeln($_body)->outdent()->writeln("}"); } $writer->outdent()->writeln('}'); $content = $writer->getContent(); /** * convert the fake php code * '#php{% $this->admin->trans("test.form.enabled.choices.no") %}' */ $content = preg_replace_callback(self::PHP_CODE, function ($m) { return stripslashes($m[1]); }, $content); $_class_dir = dirname($_class_file); if (!file_exists($_class_dir)) { if (!@mkdir($_class_dir, 0755)) { throw new \Exception(sprintf("mkdir(%s) error!", $_class_dir)); } } \Dev::write_file($_class_file, $content); return $_class_file; }
/** * @return \Symforce\AdminBundle\Compiler\Generator\PhpClass */ public function compile() { parent::compile(); // $this->children->properties $rc = $this->admin_object->reflection; $admin_class = $this->admin_object->getCompileClass(); $class = $this->getCompileClass(); $twig_writer = $this->_twig->getWriter(); // generate twig template $twig_writer->writeln('{% set _object = object %}')->writeln('{% block content_view %}')->writeln('<div class="form-horizontal">')->indent(); $children = array(); foreach ($this->admin_object->form->children->properties as $property_name => $_property) { if (isset($this->children->properties[$property_name])) { $property = $this->children->properties[$property_name]; $property->_annot = true; } else { $property = new ViewProperty($this->children, $this->admin_object, $property_name); $property->lazyInitialize(); $children[$property_name] = $property; } if (null !== $_property->group && null === $property->group) { $property->set_group($_property->group); } if (null !== $_property->position && null === $property->position) { $property->set_position($_property->position); } } foreach ($rc->getProperties() as $p) { if ($p->isStatic()) { continue; } $property_name = $p->getName(); if (isset($children[$property_name])) { continue; } if (isset($this->children->properties[$property_name])) { $property = $this->children->properties[$property_name]; } else { $property = new ViewProperty($this->children, $this->admin_object, $property_name); $property->lazyInitialize(); $property->_no_form_and_view = true; } $children[$property_name] = $property; } $macro_writer = new \Symforce\AdminBundle\Compiler\Generator\PhpWriter(); foreach ($children as $property_name => $property) { $label = null; if ($property->form_element) { $label = $property->form_element->label; } else { if (isset($this->admin_object->action_collection->children['list']->children->properties[$property_name])) { $label = $this->admin_object->action_collection->children['list']->children->properties[$property_name]->label; } else { $label = $property->label; } } $admin_twig_calss = var_export($this->admin_object->class_name, 1); $admin_twig_code = sprintf('sf_admin_class(%s)', $admin_twig_calss); $admin_class->addLazyArray('properties_label', $property_name, array($label->getPath(), $label->getDomain())); $macro_writer->write('{% ' . sprintf('macro macro_label_%s(admin=false)', $property_name) . ' %}')->write('{% if admin is same as(false) %}{% set admin = ' . $admin_twig_code . ' %}{% endif %}'); if ($this->admin_object->_final_template) { $macro_writer->write('{% ' . sprintf('import "%s" as parent_macro', $this->admin_object->_final_template) . ' %}')->write('{% ' . sprintf('if twig_macro_exists(parent_macro, "macro_label_%s") ', $property_name) . ' %}')->indent()->write('{{ ' . sprintf('parent_macro.macro_label_%s(admin)', $property_name) . '}}')->outdent()->write('{% else %}')->indent()->write('{{' . $property->compileLabel() . ' }}')->outdent()->write('{% endif %}'); } else { $macro_writer->write('{{ ' . $property->compileLabel() . ' }}'); } $macro_writer->writeln('{% endmacro %} '); $macro_writer->writeln('{% ' . sprintf('macro macro_value_%s(_object, _property_value=false, admin=false)', $property_name) . ' %}')->indent()->writeln('{% if admin is same as(false) %}{% set admin = ' . $admin_twig_code . ' %}{% endif %}'); if ($this->admin_object->generator->getParameter('kernel.debug')) { $macro_writer->writeln(sprintf('{{ sf_check_class(_object, %s) }}', $admin_twig_calss)); } $macro_writer->writeln('{% if _property_value is same as(false) %}{% set _property_value = ' . $property->getTwigValue() . ' %}{% endif %}'); if ($this->admin_object->_final_template) { $macro_writer->writeln('{% ' . sprintf('import "%s" as parent_macro', $this->admin_object->_final_template) . ' %}')->writeln('{% ' . sprintf('if twig_macro_exists(parent_macro, "macro_value_%s") ', $property_name) . ' %}')->indent()->writeln('{{ ' . sprintf('parent_macro.macro_value_%s(_object, _property_value, admin)', $property_name) . '}}')->outdent()->writeln('{% else %}')->indent()->writeln($property->compileValue())->outdent()->writeln('{% endif %}'); } else { $macro_writer->writeln($property->compileValue()); } $macro_writer->outdent()->writeln('{% endmacro %} '); $macro_writer->write("\n"); } if ($this->admin_object->workflow) { $macro_writer->writeln('{% macro macro_workflow_report(admin=false) %}')->indent()->writeln('{% if admin is same as(false) %}{% set admin = ' . $admin_twig_code . ' %}{% endif %}'); $this->admin_object->workflow->compileListAction($macro_writer); $macro_writer->outdent()->writeln('{% endmacro %}'); } \Dev::write_file($this->admin_object->_template_path, $macro_writer->getContent()); if ($this->admin_object->form->groups) { foreach ($this->admin_object->form->groups as $_group) { $_annot = array('id' => $_group->id, 'label' => $_group->label); if ($_group->name) { $_annot['name'] = $_group->name; } if (null !== $_group->position) { $_annot['position'] = $_group->position; } $this->_groups[$_group->id] = new ActionPropertyGroup($_annot); } } if (!isset($this->_groups['default'])) { $this->_groups['default'] = new ActionPropertyGroup('default'); } foreach ($children as $property_name => $property) { if ($property->form_element && 'apppassword' === $property->form_element->compile_form_type) { continue; } if ($property->_no_form_and_view) { continue; } if ($property->group) { if (isset($this->_groups[$property->group])) { $group = $this->_groups[$property->group]; } else { $group = new ActionPropertyGroup($property->group); $this->_groups[$property->group] = $group; } } else { $group = $this->_groups['default']; } $group->add($property->class_property, $property->position); } foreach ($this->_groups as $group) { $group->fixLabel($this->admin_object->form->tr_node, $this->admin_object->generator->sf_domain); $group->sort(); } $_anonymous_children = array(); foreach ($this->admin_object->_route_assoc->_anonymous_children as $child_admin_name => $child_properties) { $child_admin = $this->admin_object->generator->getAdminByName($child_admin_name); if (!$child_admin->_final_template) { continue; } $_anonymous_children[$child_admin_name] = $child_properties; } if (count($this->_groups) > 1) { foreach ($this->_groups as $group) { if (!count($group->properties)) { continue; } $twig_writer->writeln('{% set view_property_count = 0 %}')->writeln('<fieldset class="view-group">')->indent(); foreach ($group->properties as $property_name) { $children[$property_name]->compileView($twig_writer); } $twig_writer->write('{% if view_property_count > 0 %}')->write('<legend class="view-group-header">')->write($group->label->getTwigCode())->write('</legend>')->writeln('{% endif %}')->outdent()->writeln('</fieldset>'); } if (!empty($_anonymous_children)) { $twig_writer->writeln('{% set view_property_count = 0 %}')->writeln('<fieldset class="view-group">')->indent(); $this->compileAnonymousChildren($_anonymous_children, $twig_writer); $twig_writer->write('{% if view_property_count > 0 %}')->write('<legend class="view-group-header">Others</legend>')->writeln('{% endif %}')->outdent()->writeln('</fieldset>'); } } else { foreach ($this->_groups as $group) { $twig_writer->writeln('{% set view_property_count = 0 %}'); foreach ($group->properties as $property_name) { $children[$property_name]->compileView($twig_writer); } } $this->compileAnonymousChildren($_anonymous_children, $twig_writer); } $twig_writer->writeln('{% block _admin_view_extra %}')->writeln('{% endblock %}')->outdent()->writeln('</div>')->writeln('{% endblock %}'); return $class; }
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('};'); }
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; }