/** * Constructor * * @param String $id The Id of the pattern * @param Array $customData Optional data to load at runtime */ public function __construct($id, $customData = array()) { $this->id = Primer::cleanId($id); $this->path = Primer::$PATTERN_PATH . '/' . $this->id; // Check the path is valid if (!is_dir($this->path)) { throw new NotFoundException('Pattern not found: ' . $this->id); } $pathToPattern = $this->path; // If this is an alias we need to load the template of the parent pattern if (strpos($this->id, "~") !== false) { $parts = explode("~", $this->id); if (count($parts) > 1) { $pathToPattern = Primer::$PATTERN_PATH . '/' . $parts[0]; } } // Load the correct template with the correct template engine $templateClass = Primer::$TEMPLATE_CLASS; $this->template = new $templateClass($pathToPattern, 'template'); // Save the raw template string too $this->templateRaw = $this->template->raw(); // Get the title $idComponents = explode('/', $this->id); $this->title = ucwords(preg_replace('/(\\-|~)/', ' ', strtolower(end($idComponents)))); // Load the copy $this->loadCopy(); // Load the data $this->loadData($customData); }
protected function execute(InputInterface $input, OutputInterface $output) { $id = $input->getArgument('id'); $cleanId = Primer::cleanId($id); // Does the pattern already exist? $patternPath = Primer::$PATTERN_PATH . '/' . $cleanId; if (file_exists($patternPath)) { $output->writeln(''); $output->writeln('<error>`' . $cleanId . '` already exists</error>'); $output->writeln(''); return; } $success = @mkdir($patternPath, 0777, true); if (!$success) { $error = error_get_last(); $output->writeln(''); $output->writeln('<error>`' . $error['message'] . '` already exists</error>'); $output->writeln(''); return; } $templateClass = Primer::$TEMPLATE_CLASS; $templateExtension = $templateClass::$extension; @touch($patternPath . '/template.' . $templateExtension); @touch($patternPath . '/data.json'); $output->writeln('<info>Pattern `' . $cleanId . '` created</info>'); }
/** * Retrieve data for a patter * * @param String $id The id of the pattern * @param Boolean $resolveAlias Whether or not to resolve data from aliased patterns (e.g. button~outline -> button) * @return ViewData The decoded JSON data */ public static function getDataForPattern($id, $resolveAlias = false) { $data = array(); $id = Primer::cleanId($id); // Load the Patterns default data $defaultData = @file_get_contents(Primer::$PATTERN_PATH . '/' . $id . '/data.json'); if ($defaultData) { $json = json_decode($defaultData); if ($json) { // Merge in the data $data += (array) $json; } } if ($resolveAlias) { // Parent data - e.g. elements/button is the parent of elements/button~primary $parentData = array(); // Load parent data if this is inherit if (preg_match('/(.*?)~.*?/', $id, $matches)) { $parentData = FileSystem::getDataForPattern($matches[1]); } // Merge the parent and pattern data together, giving preference to the pattern data $data = array_replace_recursive((array) $parentData, (array) $data); } // Create the data structure $viewData = new ViewData($data); // Give the system a chance to mutate the data ViewData::fire($id, $viewData); // Return the data structure return $viewData; }
public function testCustomPatternData() { $primer = Primer::start(array('basePath' => __DIR__ . '/primer-test', 'templateClass' => HandlebarsTemplateEngine::class)); $pattern = new Pattern('components/patterns/custom-data', ['name' => 'Test name']); $output = $pattern->render(false); $this->assertEquals("Test name", $output); }
public function __construct($id) { $this->id = Primer::cleanId($id); $this->path = Primer::$PATTERN_PATH . '/' . $this->id; // Check the path is valid if (!is_dir($this->path)) { throw new NotFoundException('Group not found: ' . $this->id); } // Get the title $idComponents = explode('/', $this->id); $this->title = ucwords(preg_replace('/\\-/', ' ', strtolower(end($idComponents)))); $this->copy = $this->loadCopy(); // Load the patterns $this->patterns = new RenderList(Pattern::loadPatternsInPath($this->path)); }
public function testCustomViewPath() { $primer = Primer::start(array('basePath' => __DIR__ . '/primer-test', 'viewPath' => __DIR__ . '/primer-test/views-non-standard', 'templateClass' => HandlebarsTemplateEngine::class)); $output = $primer->getPatterns(['components/render/test'], false); $this->assertEquals("non-standard/template.hbs\ntest/template.hbs", $output); }
/** * Bootstrap the system */ public function setup() { $this->primer = \Rareloop\Primer\Primer::start(array('basePath' => __DIR__ . '/primer-test', 'templateClass' => Template::class)); }
/** * Bootstrap the system */ public function setup() { $this->primer = Primer::start(array('basePath' => __DIR__ . '/primer-test', 'templateClass' => HandlebarsTemplateEngine::class)); }
*/ 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!'; }); /** * Create an instance of Primer * * @var Primer */ $primer = Primer::start(['basePath' => __DIR__ . '/..', 'templateClass' => HandlebarsTemplateEngine::class]); return $primer;
/** * Get a selection of patterns * * @param Array $ids An array of pattern/group/section ids * @param boolean $showChrome Should we show the chrome * @return String */ public function getPatterns($ids, $showChrome = true) { $renderList = new RenderList(); foreach ($ids as $id) { $id = Primer::cleanId($id); // Check if the Id is for a pattern or a group $parts = explode('/', $id); if (count($parts) > 2) { // It's a pattern $renderList->add(new Pattern($id)); } elseif (count($parts) > 1) { // It's a group $renderList->add(new Group($id)); } else { // It's a section (e.g. all elements or all components) $renderList->add(new Section($id)); } } return $this->prepareViewForPatterns($renderList, $showChrome); }
/** * Test that patterns load when BASE_PATH isn't a parent of PATTERN_PATH */ public function testBasePathIsNotParentOfPatternPath() { $testPrimer = \Rareloop\Primer\Primer::start(array('basePath' => __DIR__ . '/primer-test', 'patternPath' => __DIR__ . '/patterns', 'templateClass' => Template::class)); $output = $testPrimer->getPatterns(array('elements/test-group/test'), false); $this->assertEquals($output, 123); }
/** * Bootstrap the system */ public function setup() { $this->primer = \Rareloop\Primer\Primer::start(array('basePath' => __DIR__ . '/primer-test')); }