/** * 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; }
/** * @dataProvider loadingDataProvider */ public function testLoadingData($id, $resolveAlias, $expected) { $viewData = FileSystem::getDataForPattern($id, $resolveAlias); $this->assertEquals($expected, $viewData->toArray()); }
/** * Load the data for this pattern */ protected function loadData() { $this->data = FileSystem::getDataForPattern($this->id, true); }
public function testLoadingDataShouldTriggerEventsForParentPaths() { $testData = new \stdClass(); $testData->count = 0; $event1 = ViewData::composer('components/events/*', function ($data) use($testData) { // Is the data the right type? $this->assertEquals(ViewData::class, get_class($data)); // Increment the count $testData->count++; }); $event2 = ViewData::composer('components/*', function ($data) use($testData) { // Is the data the right type? $this->assertEquals(ViewData::class, get_class($data)); // Increment the count $testData->count++; }); $viewData = FileSystem::getDataForPattern('components/events/loading-data'); $this->assertEquals(2, $testData->count); // Unbind event so it can't mess with other tests $event1->stop(); $event2->stop(); }