public function actionGenerateXml($idTestContext) { //header('Content-Type: text/html; charset=utf-8'); //echo "AQUI SERÁ O GERADOR DO XML<br/>"; //echo "<a href='/mtcontrool/index.php/testContext/admin'><< Back to Test Context Area</a>"; $model = TestContext::model()->findByPk($idTestContext); //Instanciamos o objeto passando como valor a versão do XML e o encoding (código de caractéres) $dom = new DOMDocument('1.0', 'UTF-8'); //Nesse ponto, informamos para o objeto que não queremos espaços em branco no documento $dom->preserveWhiteSpaces = false; //Aqui, dizemos para o objeto que queremos gerar uma saída formatada $dom->formatOutput = true; //Pronto! Configurações inicias realizadas, agora partiremos para a criação dos elementos que compõe a árvore do documento XML //Criação do elemento root (elemento pai) $root = $dom->createElement('test-context'); //Vamos criar o elemento nodeOne, conforme o exemplo anterior $id = $dom->createElement('id'); $user = $dom->createElement('user'); $app = $dom->createElement('app'); $appId = $dom->createElement('app-id'); $appName = $dom->createElement('app-name'); $platform = $dom->createElement('platform'); $platformId = $dom->createElement('platform-id'); $platformName = $dom->createElement('platform-name'); $device = $dom->createElement('device'); $deviceId = $dom->createElement('device-id'); $deviceName = $dom->createElement('device-name'); $description = $dom->createElement('description'); //Agora o elemento nodeTwo //$nodeTwo = $dom->createElement('nodeTwo'); //criados os elementos, vamos adicionar um valor para cada um deles $idTxt = $dom->createTextNode($model->id); $userTxt = $dom->createTextNode($model->id_user); $appIdTxt = $dom->createTextNode($model->id_app); $appNameTxt = $dom->createTextNode($model->iDAPP->name); $platformIdTxt = $dom->createTextNode($model->id_platform); $platformNameTxt = $dom->createTextNode($model->iDPLATFORM->name); $deviceIdTxt = $dom->createTextNode($model->id_device); $deviceNameTxt = $dom->createTextNode($model->iDDEVICE->description); $descriptionTxt = $dom->createTextNode($model->description); /*preencher com todas as instancias de elementos*/ //Pronto! Elementos criados, o próximo passo é organizar essa bagunça, para isso, usaremos o método appendChild() e diremos quem é elemento pai e quem é elemento filho $id->appendChild($idTxt); $description->appendChild($descriptionTxt); $user->appendChild($userTxt); $app->appendChild($appId); $appId->appendChild($appIdTxt); $app->appendChild($appName); $appName->appendChild($appNameTxt); $platform->appendChild($platformId); $platformId->appendChild($platformIdTxt); $platform->appendChild($platformName); $platformName->appendChild($platformNameTxt); $device->appendChild($deviceId); $deviceId->appendChild($deviceIdTxt); $device->appendChild($deviceName); $deviceName->appendChild($deviceNameTxt); $instances = $dom->createElement('instances'); /*$instanceElement = $dom->createElement('instance-element-1'); $instanceElementIdTxt = $dom->createTextNode('id do elemento'); $instanceElement->appendChild($instanceElementIdTxt); $instances->appendChild($instanceElement);*/ $cont = 0; foreach ($model->elementInsts as $value) { $instanceElement = $dom->createElement('instance-element-' . $cont); $instanceElementId = $dom->createElement('id-element'); $instanceElementIdTxt = $dom->createTextNode($value->id); $instanceElementName = $dom->createElement('name-element'); $instanceElementNameTxt = $dom->createTextNode($value->iDELEMENT->description); $instanceElementDesc = $dom->createElement('desc-instance'); $instanceElementDescTxt = $dom->createTextNode($value->description); $instanceElementParams = $dom->createElement('params-instance'); $instanceElementType = $dom->createElement('type-instance'); $instanceElementTypeTxt = $dom->createTextNode($value->element_type); $instanceElement->appendChild($instanceElementId); $instanceElementId->appendChild($instanceElementIdTxt); //$instances->appendChild($instanceElement); $instanceElement->appendChild($instanceElementName); $instanceElementName->appendChild($instanceElementNameTxt); $instanceElement->appendChild($instanceElementDesc); $instanceElementDesc->appendChild($instanceElementDescTxt); $instanceElement->appendChild($instanceElementType); $instanceElementType->appendChild($instanceElementTypeTxt); $instanceElement->appendChild($instanceElementParams); $instanceElementParams->appendChild($instanceElementType); $instanceElementType->appendChild($instanceElementTypeTxt); if ($value->ELEMENT_TYPE == 'interval') { $instanceElementStartParam = $dom->createElement('start-param'); $instanceElementStartParamTxt = $dom->createTextNode($value->start_param); $instanceElementStartParam->appendChild($instanceElementStartParamTxt); $instanceElementParams->appendChild($instanceElementStartParam); $instanceElementEndParam = $dom->createElement('end-param'); $instanceElementEndParamTxt = $dom->createTextNode($value->end_param); $instanceElementEndParam->appendChild($instanceElementEndParamTxt); $instanceElementParams->appendChild($instanceElementEndParam); } $instances->appendChild($instanceElement); $cont = $cont + 1; } $root->appendChild($id); $root->appendChild($description); $root->appendChild($user); $root->appendChild($app); $root->appendChild($platform); $root->appendChild($device); $root->appendChild($instances); $dom->appendChild($root); //Dessa forma, dissemos que os elementos nodeOne e nodeTwo são filhos do elemento root, isto é, estão dentro de root ou um nível abaixo de root. //Para imprimir na tela, utilizamos o método saveXML() //header('Content-type: "text/xml"; charset="utf8"'); //header('Content-disposition: attachment; filename="filename.xml"'); $dom->saveXML(); //Por fim, para salvarmos o documento, utilizamos o método save() $filename = 'xml_test_context_' . $idTestContext . '.xml'; $dom->save('xml_testcontext/' . $filename); $url = Yii::app()->request->getBaseUrl(true); /*echo "<html> <head> <title>XML Download Example</title> </head> <body> <a href='".$url."/index.php/testContext/admin'><< Back Test Context Area</a> <br/> <a href='".$url."/xml_testcontext/".$filename."'>Download XML file</a> </body> </html>";*/ //EXECUTA O DOWNLOAD DO XML. return Yii::app()->getRequest()->sendFile($filename, @file_get_contents(Yii::app()->params['xmlDirTestContext'] . $filename)); //ABRE O ARQUIVO XML EM UMA ABA NO WEB-BROWSER //$this->redirect($url.'/xml_testcontext/'.$filename); }
/** * Test the fetching of a single value does not exist in the config */ public function testGetConfigSingleInvalid() { $config = ['test' => true]; $context = new TestContext($config); $this->assertEquals(null, $context->getConfig('foobar')); }