function testGenerateHelperFunctions() { $name = 'namespaced_name'; $Route = new AkRoute('/author/:name'); AkRouterHelper::generateHelperFunctionsFor($name, $Route); $this->assertTrue(function_exists('namespaced_name_url')); $this->assertTrue(function_exists('namespaced_name_path')); }
/** * Returns the URL for the set of +$options+ provided. This takes the same options * as url_for. For a list, see the documentation for AKActionController::urlFor. * Note that it'll set ('only_path' => true) so you'll get /controller/action instead of the * http://example.com/controller/action part (makes it harder to parse httpd log files) */ public function url_for($options = array(), $parameters_for_method_reference = null) { if ($options instanceof AkBaseModel) { $options = AkRouterHelper::getUrlParamsForModel($options); } $default_options = array('only_path' => true); $options = array_merge($default_options, $options); return $this->_controller->urlFor($options, $parameters_for_method_reference); }
public function connectNamed($name, $url_pattern = '', $defaults = array(), $requirements = array(), $conditions = array()) { $this->_logNamedRoute($name); $this->handleApiShortcuts($url_pattern, $defaults, $requirements); if (isset($defaults['conditions'])) { throw new Exception('Could not connect named route with conditions'); } $Route = new AkRoute($url_pattern, $defaults, $requirements, $conditions); if ($this->generate_helper_functions && !empty($name)) { AkRouterHelper::generateHelperFunctionsFor($name, $Route); } return $this->addRoute($name, $Route); }
/** * Redirects the browser to the target specified in +options+. This parameter can take one of three forms: * * * <tt>Array</tt>: The URL will be generated by calling $this->urlFor with the +options+. * * <tt>String starting with protocol:// (like http://)</tt>: Is passed straight through * as the target for redirection. * * <tt>String not containing a protocol</tt>: The current protocol and host is prepended to the string. * * <tt>back</tt>: Back to the page that issued the Request-> Useful for forms that are * triggered from multiple places. * Short-hand for redirectTo(Request->env["HTTP_REFERER"]) * * Examples: * redirectTo(array('action' => 'show', 'id' => 5)); * redirectTo('http://www.bermilabs.com'); * redirectTo('/images/screenshot.jpg'); * redirectTo('back'); * * The redirection happens as a "302 Moved" header. */ public function redirectTo($options = array(), $parameters_for_method_reference = null) { if ($options instanceof AkBaseModel) { $options = AkRouterHelper::getUrlParamsForModel($options); } $this->_handleFlashAttribute($parameters_for_method_reference); $this->_closeSession(); if (is_string($options)) { if (preg_match('/^\\w+:\\/\\/.*/', $options)) { if ($this->hasPerformed()) { $this->_doubleRenderError(); } AK_LOG_EVENTS && !empty($this->_Logger) ? $this->_Logger->message('Redirected to ' . $options) : null; $this->Response->redirect($options); $this->Response->redirected_to = $options; $this->performed_redirect = true; } elseif ($options == 'back') { $this->redirectTo($this->Request->getReferer()); } else { $this->redirectTo($this->Request->getProtocol() . $this->Request->getHostWithPort() . $options); } } else { if (empty($parameters_for_method_reference)) { $this->redirectTo($this->urlFor($options)); $this->Response->redirected_to = $options; } else { $this->redirectTo($this->urlFor($options, $parameters_for_method_reference)); $this->Response->redirected_to = $options; $this->Response->redirected_to_method_params = $parameters_for_method_reference; } } }
/** * * Creates a form and a scope around a specific model object, which is then used as a base for questioning about * values for the fields. Examples: * * <?php $f = $form_helper->form_for('person', $Person, array('url' => array('action' => 'update'))); ?> * First name: <?= $f->text_field('first_name'); ?> * Last name : <?= $f->text_field('last_name'); ?> * Biography : <?= $f->text_area('biography'); ?> * Admin? : <?= $f->check_box('admin'); ?> * <?= $f->end_form_tag(); ?> * * The form_for yields a form_builder object, in this example as $f, which emulates the API for the stand-alone * FormHelper methods, but without the object name. So instead of <tt>$form_helper->text_field('person', 'name');</tt>, * you get away with <tt>$f->text_field('name');</tt>. * * That in itself is a modest increase in comfort. The big news is that form_for allows us to more easily escape the instance * variable convention, so while the stand-alone approach would require <tt>$form_helper->text_field('person', 'name', array('object' => $Person));</tt> * to work with local variables instead of instance ones, the form_for calls remain the same. You simply declare once with * <tt>'person', $Person</tt> and all subsequent field calls save <tt>'person'</tt> and <tt>'object' => $Person</tt>. * * Also note that form_for doesn't create an exclusive scope. It's still possible to use both the stand-alone FormHelper methods * and methods from FormTagHelper. Example: * * <?php $f = $form_helper->form_for('person', $Person, array('url' => array('action' => 'update'))); ?> * First name: <?= $f->text_field('first_name'); ?> * Last name : <?= $f->text_field('last_name'); ?> * Biography : <?= $f->text_area('person', $Biography); ?> * Admin? : <?= $form_helper->check_box_tag('person[admin]', $Person->company->isAdmin()); ?> * <?= $f->end_form_tag(); ?> * * Note: This also works for the methods in FormOptionHelper and DateHelper that are designed to work with an object as base. * Like collection_select and datetime_select. */ public function form_for($object_name, &$object = null, $options = array()) { $this->object_name = $object_name; if (empty($object)) { $object = $this->_controller->{$object_name}; } if (!$object instanceof AkBaseModel) { throw new Exception('$object is of type ' . gettype($object) . '. Expected instance of AkBaseModel.'); } if (empty($options['url'])) { $options['url'] = AkRouterHelper::getUrlParamsForModel($object); } $url_for_options = $options['url']; $_SESSION['_csrf_token'] = sha1(Ak::uuid() . AK_REMOTE_IP . time()); echo $this->_controller->ak_form_tag_helper->form_tag($url_for_options, $options); echo '<div style="margin:0;padding:0;display:inline"><input name="authenticity_token" type="hidden" value="' . $_SESSION['_csrf_token'] . '" /></div>'; if (!$object->isNewRecord()) { echo '<div style="margin:0;padding:0;display:inline"><input name="_method" type="hidden" value="put" /></div>'; } return $this->fields_for($object_name, $object); }
$multiple_expected_php .= $expected_php; } $AkSintags = new AkSintagsParser(); $php = $AkSintags->parse($sintags); if ($php != $expected_php) { $test_results['errors'][] = " FAILED!\n" . "\n-------\nWith Sintags: \n" . $sintags . "\n" . "\n-------\ngenerated: \n" . $php . "\n" . "\n-------\nwhile expected: \n" . $expected_php . "\n-------------\n"; } else { $test_results['success'][] = $sintags; } } if ($all_in_one_test) { $AkSintags = new AkSintagsParser(); $php = $AkSintags->parse($multiple_sintags); if ($php != $multiple_expected_php) { $test_results['errors'][] = " FAILED!\n" . "\n-------\nWith Sintags: \n" . $multiple_sintags . "\n" . "\n-------\ngenerated: \n" . $php . "\n" . "\n-------\nwhile expected: \n" . $multiple_expected_php . "\n-------------\n"; } else { $test_results['success'][] = $sintags; } } } _run_from_file('sintags_blocks_data.txt'); _run_from_file('sintags_test_data.txt'); AkRouterHelper::generateHelperFunctionsFor('named_route', new AkRoute('/')); _run_from_file('sintags_helpers_data.txt'); echo "\nRunning Sintags tests\n"; echo count((array) @$test_results['success']) . " tests passed\n"; if (!empty($test_results['errors'])) { echo join("\n", $test_results['errors']); echo count($test_results['errors']) . " failure/s!!!"; } echo "\nCompleted running tests.\n";
public function Helper($match, $state, $position = null, $is_inline_function = false) { switch ($state) { case AK_LEXER_ENTER: if (preg_match('/=+$/', trim($match))) { $this->avoid_php_tags = $this->_current_function_opening = false; $this->output .= '<?php ' . $this->_convertSintagsVarToPhp(trim($match, " =(\n\t" . $this->_SINTAGS_OPEN_HELPER_TAG)) . ' = ('; return true; } $method_name = trim($match, " =(\n\t" . $this->_SINTAGS_OPEN_HELPER_TAG); $helper = $this->_getHelperNameForMethod($method_name); $named_route = in_array($method_name, AkRouterHelper::getDefinedFunctions()); if ($helper || $named_route) { $this->avoid_php_tags = !$is_inline_function && !strstr($match, '='); $this->_current_function_opening = strlen($this->output); if (!$this->avoid_php_tags) { $this->output .= $is_inline_function ? '' : '<?php echo '; } if ($named_route) { $this->output .= "{$method_name}("; } else { if (!strpos($helper, 'helper')) { $method_name = AkInflector::variablize($method_name); } if ($helper == 'controller') { $this->output .= "\$controller->{$method_name}("; } else { $this->output .= "\$controller->{$helper}->{$method_name}("; } } return true; } else { $this->addError(Ak::t('Could not find a helper to handle the method "%method" you called in your view', array('%method' => $method_name))); } return false; break; case AK_LEXER_UNMATCHED: $match = trim($match); if ($match == ',') { $this->output .= $match . ' '; } elseif ($match == $this->_SINTAGS_HASH_KEY_VALUE_DELIMITER) { if (empty($this->_inside_array) && empty($this->_has_last_argument_params)) { $current_function = substr($this->output, $this->_current_function_opening); $function_opening = strrpos($current_function, '(') + 1; $last_comma = strrpos($current_function, ',') + 1; $insert_point = $function_opening > $last_comma && $last_comma === 1 ? $function_opening : $last_comma; $this->output = substr($this->output, 0, $this->_current_function_opening + $insert_point) . ' array(' . ltrim(substr($this->output, $this->_current_function_opening + $insert_point)); $this->_has_last_argument_params = true; } $this->output .= ' => '; } break; case AK_LEXER_EXIT: $this->output .= (!empty($this->_has_last_argument_params) ? ')' : '') . ')' . ($this->avoid_php_tags ? '' : ($is_inline_function ? '' : '; ?>')); $this->_has_last_argument_params = false; break; } return true; }
private function connectNamed($name, $url_pattern, $defaults = array(), $requirements = array(), $conditions = array()) { $this->handleApiShortcuts($url_pattern, $defaults, $requirements); $Route = new AkRoute($url_pattern, $defaults, $requirements, $conditions); if ($this->generate_helper_functions) { AkRouterHelper::generateHelperFunctionsFor($name, $Route); } return $this->addRoute($name, $Route); }
public function test_sintags_helpers() { AkRouterHelper::generateHelperFunctionsFor('named_route', $this->mock('AkRoute')); $this->_run_from_file('sintags_helpers_data.txt'); }