/** * Singleton function * * @return Twig A singleton instance of the Twig engine */ public static function instance() { if (!isset(self::$_instance)) { self::$_instance = new Twig(); Event::fire('twig.init', self::$_instance); } return self::$_instance; }
public static function instance() { if (!isset(self::$_instance)) { self::$_instance = new Handlebars(); Event::fire('handlebars.init', self::$_instance); } return self::$_instance; }
public static function instance() { if (!isset(Cli::$instance)) { Cli::$instance = new Cli(); // Add the inbuilt commands Cli::$instance->add(new \Rareloop\Primer\Commands\PatternMake()); Cli::$instance->add(new \Rareloop\Primer\Commands\Serve()); Event::fire('cli.init', Cli::$instance); } return Cli::$instance; }
public static function fire($eventId, &$data = false) { $event = new EventObject($eventId, $data); // Trigger the exact event Event::eventDispatcherInstance()->dispatch($eventId, $event); // If this is a namespaced event then trigger namespace:* too if (strpos($eventId, ".") !== false) { $parts = explode(".", $eventId); $namespace = array_shift($parts); Event::eventDispatcherInstance()->dispatch("{$namespace}.*", $event); } }
public static function fire($id, &$data) { Event::fire('data.' . $id, $data, $id); // Fire off events that let us listen for parent paths as well as the main template // e.g. components/misc/* and components/* $parts = explode('/', $id); // We don't need to do the full path as we've already done it array_pop($parts); $eventString = ''; foreach ($parts as $part) { $eventString .= '/' . $part; $eventString = trim($eventString, '/'); Event::fire('data.' . $eventString . '/*', $data, $id); } }
public static function composer($name, $callable) { return Event::listen("view.{$name}", $callable); }
/** * Listen for when the CLI is created */ Event::listen('cli.init', function ($cli) { // Register custom commands here }); /** * Listen for whole page render events */ Event::listen('render', function ($data) { $data->primer->environment = 'development'; }); /** * Listen for when new Handlebars objects are created so that we can register any required helpers */ Event::listen('handlebars.init', function ($handlebars) { }); /** * Listen for when a View (not pattern template) is about to be rendered * view.[viewName] - below example would call when views/pattern.handlebars is loaded */ View::composer('pattern', function ($data, $eventId) { // $data->id = 'testing'; }); /** * A function that calls anytime a data for a pattern is loaded * Useful for dynamically generating pattern data, e.g. sprites * $data is the raw output of the data.json */ ViewData::composer('elements/forms/input', function ($data) { // $data->label = 'boo yah!'; });
protected function prepareViewForPatterns(RenderList $renderList, $showChrome = false) { $bodyClasses = array('not-template'); // If we're in minimal mode then add a new class to the body if (!$showChrome) { $bodyClasses[] = 'minimal'; } $viewData = new ViewData(['primer' => ['items' => $renderList->render($showChrome), 'bodyClass' => implode(' ', $bodyClasses)]]); Event::fire('render', $viewData); return View::render('template', $viewData); }
public function testRenderEventContainsPrimerData() { $testData = new \stdClass(); $testData->count = 0; $event = Event::listen('render', function ($data) use($testData) { // Is the data the right type? $this->assertEquals(ViewData::class, get_class($data)); $array = $data->toArray(); $this->assertArrayHasKey('primer', $array); $this->assertArrayHasKey('bodyClass', $array['primer']); $this->assertArrayHasKey('template', $array['primer']); $this->assertArrayHasKey('items', $array['primer']); // Increment the count $testData->count++; }); $output = $this->primer->getTemplate('render-event'); $this->assertEquals(1, $testData->count); $event->stop(); }