/** * Test stack */ public function testStack() { $self = $this; $stack = array(); $this->depthList->runin(function ($value) use(&$stack) { $stack[] = $value; }, function ($value) use(&$stack, $self) { $self->assertEquals(array_pop($stack), $value); }); $this->assertEmpty($stack, 'Stack should be empty after runin()'); }
/** * Constructor * * @param array|\Traversable $renderList */ public function __construct($renderList) { $this->root = new Navigation\Navigation(); $depthList = new DepthList($renderList); $depthList->runin(array($this, 'onOpen'), array($this, 'onClose')); }
/** * Export paragraph's customize into a zip file * * @param int $rootId * @return string|null $zip * @return string */ public function export($rootId, $zip = null) { $rootId = (int) $rootId; $info = $this->getSiteInfo(); $domainList = $this->getDomainList(); $sheetModel = $this->getSheetModel(); $paragraphModel = $this->getParagraphModel(); $dom = new DOMImplementation(); $document = $dom->createDocument(static::GPML_NAMESPACE, static::GPML_ROOT, $dom->createDocumentType(static::GPML_ROOT, static::GPML_PUBLIC, static::GPML_SYSTEM)); if (empty($zip)) { $suffix = ''; $zipPath = static::PUBLIC_DIR . static::TEMP_DIR . 'paragraph-export-' . $rootId; while (is_file($zipPath . $suffix . '.zip')) { $suffix--; } $zip = $zipPath . $suffix . '.zip'; } $zipPath = (string) $zip; $zip = new ZipArchive(); $open = $zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE); if (true !== $open) { throw new \RuntimeException(sprintf('%s: "%s" cannot be created as a zip file (errno #%d)', __METHOD__, $zipPath, $open)); } $gpml = $document->documentElement; // $gpml->setAttribute( 'xmlns', 'http://gridguyz.com/#gpml' ); $gpml->setAttribute('version', '1.0'); $gpml->setAttribute('db-schema', $info->getSchema()); foreach ($domainList as $domain) { $domainTextNode = $document->createTextNode((string) $domain); $domainElement = $document->createElement('domain'); $domainElement->appendChild($domainTextNode); $gpml->appendChild($domainElement); } $nodeStack = array($gpml); $paragraphs = new DepthList($paragraphModel->getMapper()->findRenderListData($rootId)); $paragraphs->runin(function ($paragraph) use(&$nodeStack, $document, $zip) { $nodeStack[] = $node = $document->createElement('paragraph'); $node->setAttribute('id', $paragraph['id']); $node->setAttribute('type', $paragraph['type']); if (isset($paragraph['name'])) { $node->setAttribute('name', $paragraph['name']); } if (!empty($paragraph['proxyData'])) { foreach ($paragraph['proxyData'] as $property) { $propNode = $document->createElement('paragraph-property'); $propNode->setAttribute('locale', empty($property['locale']) ? '*' : $property['locale']); $propNode->setAttribute('name', $property['name']); if (isset($property['value'])) { $propNode->setAttribute('value', $this->setPropertyNodeValue($zip, $propNode, $property['value'])); } $node->appendChild($propNode); } } }, function () use(&$nodeStack) { $append = array_pop($nodeStack); end($nodeStack)->appendChild($append); }); /* @var $sheet \Grid\Customize\Model\Sheet\Structure */ $sheet = $sheetModel->find($rootId); foreach ($sheet->rules as $rule) { /* @var $rule \Grid\Customize\Model\Rule\Structure */ $ruleNode = $document->createElement('customize-rule'); $ruleNode->setAttribute('selector', $rule->selector); $ruleNode->setAttribute('media', $rule->media); foreach ($rule->getRawPropertyNames() as $property) { $propNode = $document->createElement('customize-property'); $priority = $rule->getRawPropertyPriority($property); $propNode->setAttribute('name', $property); $propNode->setAttribute('value', $this->setPropertyNodeValue($zip, $propNode, $rule->getRawPropertyValue($property), true)); if ($priority) { $propNode->setAttribute('priority', $priority); } $ruleNode->appendChild($propNode); } $gpml->appendChild($ruleNode); } $extra = trim($sheet->getExtraContent()); if (!empty($extra)) { $extraText = $document->createTextNode("\n{$extra}\n"); $extraNode = $document->createElement('customize-extra'); $extraNode->appendChild($extraText); $gpml->appendChild($extraNode); } $document->formatOutput = true; $zip->addFromString('paragraph.xml', $document->saveXML()); $zip->close(); return $zipPath; }