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

View File

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