예제 #1
0
 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>');
 }
예제 #2
0
 /**
  * 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;
 }
예제 #3
0
 /**
  * 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);
 }
예제 #4
0
 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));
 }
예제 #5
0
 /**
  * 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);
 }