create method for validating module file descriptor

This commit is contained in:
Manuel Raynaud
2013-10-17 15:06:13 +02:00
parent c9dbb610c1
commit a9d5554b7c
4 changed files with 66 additions and 49 deletions

View File

@@ -0,0 +1,35 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Module\Exception;
/**
* Class InvalidXmlDocumentException
* @package Thelia\Module\Exception
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class InvalidXmlDocumentException extends \RuntimeException
{
}

View File

@@ -21,51 +21,40 @@
/* */ /* */
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Module\Loader; namespace Thelia\Module;
use Symfony\Component\Config\Loader\FileLoader; use Thelia\Module\Exception\InvalidXmlDocumentException;
use Symfony\Component\Config\Util\XmlUtils;
/** /**
* Class XmlFileLoader * Class ModuleDescriptorValidator
* @package Thelia\Module\Loader * @package Thelia\Module
* @author Manuel Raynaud <mraynaud@openstudio.fr> * @author Manuel Raynaud <mraynaud@openstudio.fr>
*/ */
class XmlFileLoader extends FileLoader class ModuleDescriptorValidator
{ {
private $xsd_file;
/** public function __construct()
* Loads a resource.
*
* @param mixed $resource The resource
* @param string $type The resource type
*/
public function load($resource, $type = null)
{ {
$path = $this->locator->locate($resource); $this->xsd_file = __DIR__ . '/schema/module/module.xsd';
$xml = $this->parseFile($path);
} }
protected function parseFile($file) public function validate($xml_file)
{ {
$schema = str_replace('\\', '/',__DIR__.'/schema/module-1.0.xsd'); $dom = new \DOMDocument();
$dom = XmlUtils::loadFile($file, $schema); if ($dom->load($xml_file)) {
if($dom->schemaValidate($this->xsd_file)) {
return true;
}
}
return simplexml_import_dom($dom); throw new InvalidXmlDocumentException(sprintf("%s file is not a valid file", $xml_file));
} }
/** public function getDescriptor($xml_file) {
* Returns true if this class supports the given resource. $this->validate($xml_file);
*
* @param mixed $resource A resource return @simplexml_load_file($xml_file);
* @param string $type The resource type
*
* @return Boolean true if this class supports the given resource, false otherwise
*/
public function supports($resource, $type = null)
{
// TODO: Implement supports() method.
} }
} }

View File

@@ -2,23 +2,16 @@
<module xmlns="http://thelia.net/schema/module" <module xmlns="http://thelia.net/schema/module"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/module http://thelia.net/schema/module/module-1.0.xsd"> xsi:schemaLocation="http://thelia.net/schema/module http://thelia.net/schema/module/module-1.0.xsd">
<descriptif lang="fr"> <descriptive locale="en_US">
<titre>Produits factices</titre> <title>Cheque</title>
<chapo></chapo> </descriptive>
<description>Insertion des produits dans votre site Thelia permettant d'avoir un Thelia de test vite opérationnel.</description> <full-namespace>Cheque\\Cheque</full-namespace>
<postscriptum></postscriptum>
</descriptif>
<version>1.1</version> <version>1.1</version>
<auteur> <author>
<nom>Manuel Raynaud - mraynaud@openstudio.fr</nom> <name>Manuel Raynaud</name>
<societe></societe> <email>mraynaud@openstudio.fr</email>
<email></email> </author>
<web></web> <type>payment</type>
</auteur> <thelia>2.0.0</thelia>
<type>classique</type> <stability>alpha</stability>
<prerequis/>
<thelia>1.5.0</thelia>
<etat>production</etat>
<documentation>README.TXT</documentation>
<urlmiseajour></urlmiseajour>
</module> </module>