public function testAddGroupsToTheMix() { $form = new Event(); $form->setData(['subject' => 'subject1', 'event_date' => '2014-01-01', 'event_time' => '13:00', 'event_end' => '13:01']); $form->bind(new \ArrayObject(['groups' => [(object) ['id' => 1, 'name_short' => '01']]])); $this->assertTrue($form->isValid(), print_r($form->getMessages(), true)); }
/** * Initializer. * * @param string $env Environment */ public function __construct($env = 'local') { $this->environment = $env; $this->load(); // Register event Event::bind('assets.built', [$this, 'load']); }
function _before_call($method, &$params) { Event::bind('system.output', 'Output::AJAX'); parent::_before_call($method, $params); }
static function default_dispatcher() { if (Input::$AJAX && Input::$AJAX['widget']) { $widget = Widget::factory(Input::$AJAX['widget']); $method = 'on_' . (Input::$AJAX['object'] ?: 'unknown') . '_' . (Input::$AJAX['event'] ?: 'unknown'); if (method_exists($widget, $method)) { Event::bind('system.output', 'Output::AJAX'); $widget->{$method}(); } return; } $args = Input::args(); $default_page = Config::get('system.default_page'); if (!$default_page) { $default_page = 'index'; } //从末端开始尝试 /* home/page/edit.1 home/page/index.php Index_Controller::edit(1) home/page/index.php Index_Controller::index('edit', 1) home/page.php Page_Controller::edit(1) home/page.php Page_Controller::index('edit', 1) */ $file = end($args); if (!preg_match('/[^\\\\]\\./', $file)) { //有非法字符的只能是参数 $path = implode('/', $args); // home/page/edit/index => index, NULL $candidates[($path ? $path . '/' : '') . $default_page] = array($default_page, NULL); $candidates[$path] = array($file, NULL); // home/page/edit => edit, NULL } if ($args) { $params = array_pop($args); $file = $args ? end($args) : $default_page; $path = $args ? implode('/', $args) : $default_page; $candidates[$path] = array($file, $params); // home/page.php => page, edit|1 } else { $candidates[$default_page] = array($default_page, NULL); } $class = NULL; foreach ($candidates as $path => $candidate) { if (Core::load(CONTROLLER_BASE, $path)) { $class = str_replace('/', '_', $path); $params = array(); if (preg_match_all('/(.*?[^\\\\])\\./', $candidate[1] . '.', $parts)) { foreach ($parts[1] as $part) { $params[] = strtr($part, array('\\.' => '.')); } } Config::set('system.controller_path', $path); Config::set('system.controller_class', $class); break; } } if (!$class) { URI::redirect('error/404'); } if (Input::$AJAX) { $class .= AJAX_SUFFIX; if (!class_exists($class, false)) { Core::load(CONTROLLER_BASE, 'ajax'); $class = 'AJAX' . CONTROLLER_SUFFIX; } $controller = new $class(); $object = Input::$AJAX['object']; $event = Input::$AJAX['event']; $method = $params[0]; if (!$method || $method[0] == '_') { $method = 'index_'; } $method .= '_' . ($object ? $object . '_' : '') . $event; if (method_exists($controller, $method)) { array_shift($params); } else { $method = 'index_' . ($object ? $object . '_' : '') . $event; if (!method_exists($controller, $method)) { $method = NULL; } } if ($method) { Controller::$CURRENT = $controller; Config::set('system.controller_method', $method); Config::set('system.controller_params', $params); $controller->_before_call($method, $params); call_user_func_array(array($controller, $method), $params); $controller->_after_call($method, $params); } } else { $class .= CONTROLLER_SUFFIX; $controller = new $class(); $method = $params[0]; if ($method && $method[0] != '_' && method_exists($controller, $method)) { array_shift($params); } elseif ($method && $method[0] != '_' && method_exists($controller, 'do_' . $method)) { $method = 'do_' . $method; array_shift($params); } else { $method = 'index'; } Controller::$CURRENT = $controller; Config::set('system.controller_method', $method); Config::set('system.controller_params', $params); $controller->_before_call($method, $params); call_user_func_array(array($controller, $method), $params); $controller->_after_call($method, $params); } }
function _after_call($method, &$params) { parent::_after_call($method, $params); $this->layout->controller = $this; Event::bind('system.output', array($this->layout, 'render')); }
} } } } public static function bind($event, Closure $func) { self::$events[$event][] = $func; } } /*########################### Playground ################################*/ Event::bind('hello', function ($args) { echo "Hello World!"; echo "<pre>"; print_r($args); echo "</pre>"; return 'Salam'; }); Event::bind(sha1(time()), function ($args) { echo "Hashed Function"; }); Event::bind(0, function () { echo "Zero"; }); Event::trigger('hello', array(258, 741)); Event::trigger('hello', array(654, 147), function ($r) { echo "<h1>Returned: {$r}</h1>"; }); Event::trigger(0); echo "<pre>"; print_r(Event::$events); echo "</pre>";