VirtualPage::set PHP Method

set() public method

Associates code with the page. This code will be executed within a brand new page when called by URL.
public set ( callable $method_or_arg, callable $method = null )
$method_or_arg callable Optional argument
$method callable function($page){ .. }
    public function set($method_or_arg, $method = null)
    {
        $method = is_callable($method_or_arg) ? $method_or_arg : $method;
        $arg = is_callable($method_or_arg) ? null : $method_or_arg;
        $self = $this;
        if ($this->isActive($arg)) {
            $this->app->addHook('post-init', function () use($method, $self) {
                $page = $self->getPage();
                $page->id = $_GET[$self->name . '_id'];
                $self->app->stickyGET($self->name . '_id');
                try {
                    call_user_func($method, $page, $self);
                } catch (Exception $e) {
                    // post-init cannot catch StopInit
                    if ($e instanceof Exception_StopInit) {
                        return;
                    }
                    throw $e;
                    // exception occured possibly due to a nested page. We
                    // are already executing from post-init, so
                    // it's fine to ignore it.
                }
                //Imants: most likely forgetting is not needed, because we stop execution anyway
                //$self->app->stickyForget($self->name.'_id');
                //$self->app->stickyForget($self->name);
            });
            throw $this->exception('', 'StopInit');
        }
        return $this;
    }