Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 6b95d8a

Browse files
committed
Merge pull request #38 from frne/file_validation_test
Checks for INI, JSON, XML and YAML file validation.
2 parents 66d5c86 + 5ca9053 commit 6b95d8a

File tree

8 files changed

+447
-5
lines changed

8 files changed

+447
-5
lines changed

README.md

+56-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ It currently ships with the following Diagnostic Checks:
2424
* [SecurityAdvisory](#securityadvisory) - check installed composer dependencies against SensioLabs SA database,
2525
* [StreamWrapperExists](#streamwrapperexists) - make sure given stream wrapper is available.
2626

27+
File validation checks:
28+
29+
* [IniFile](#inifile) - check if given INI file is available and valid,
30+
* [JsonFile](#jsonfile) - check if given JSON file is available and valid,
31+
* [XmlFile](#xmlfile) - check if given XML file is available and valid,
32+
* [YamlFile](#yamlfile) - check if given YAML file is available and valid
33+
2734
## Using diagnostics with Zend Framework 2
2835

2936
1. Install the [ZFTool module](https://github.com/zendframework/ZFTool).
@@ -421,7 +428,6 @@ $checkLocal = new Memcache('127.0.0.1'); // default port
421428
$checkBackup = new Memcache('10.0.30.40', 11212);
422429
````
423430

424-
425431
### PhpVersion
426432

427433
Check if current PHP version matches the given requirement. The test accepts 2 parameters - baseline version and
@@ -489,7 +495,6 @@ $security = new SecurityAdvisory();
489495
$security = new SecurityAdvisory('/var/www/project/composer.lock');
490496
````
491497

492-
493498
### StreamWrapperExists
494499

495500
Check if a given stream wrapper (or an array of wrappers) is available. For example:
@@ -505,3 +510,52 @@ $checkCompression = new StreamWrapperExists(array(
505510
'zip'
506511
));
507512
````
513+
514+
### IniFile
515+
516+
Read an INI-file from the given path and try to parse it.
517+
518+
````php
519+
<?php
520+
use ZendDiagnostics\Check\IniFile;
521+
522+
$checkIniFile = new IniFile('/my/path/to/file.ini');
523+
$checkIniFile = new IniFile(['file1.ini', 'file2.ini', '...']);
524+
````
525+
526+
### JsonFile
527+
528+
Read a JSON-file from the given path and try to decode it.
529+
530+
````php
531+
<?php
532+
use ZendDiagnostics\Check\JsonFile;
533+
534+
$checkJsonFile = new JsonFile('/my/path/to/file.json');
535+
$checkJsonFile = new JsonFile(['file1.json', 'file2.json', '...']);
536+
````
537+
538+
539+
### XmlFile
540+
541+
Read an XML-file from the given path, try to parse it and validate it agaist its DTD schema if possible.
542+
543+
````php
544+
<?php
545+
use ZendDiagnostics\Check\XmlFile;
546+
547+
$checkXmlFile = new XmlFile('/my/path/to/file.xml');
548+
$checkXmlFile = new XmlFile(['file1.xml', 'file2.xml', '...']);
549+
````
550+
551+
### YamlFile
552+
553+
Read a YAML-file from the given path and try to parse it.
554+
555+
````php
556+
<?php
557+
use ZendDiagnostics\Check\YamlFile;
558+
559+
$checkYamlFile = new YamlFile('/my/path/to/file.yml');
560+
$checkYamlFile = new YamlFile(['file1.yml', 'file2.yml', '...']);
561+
````

composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
},
2020
"require-dev": {
2121
"zendframework/zend-loader" : "2.*",
22-
"sensiolabs/security-checker": "1.3.*@dev"
22+
"sensiolabs/security-checker": "1.3.*@dev",
23+
"symfony/yaml": "v2.3.11"
2324
},
2425
"suggest" : {
2526
"ext-bcmath" : "Required by Check\\CpuPerformance",
26-
"sensiolabs/security-checker": "Required by Check\\SecurityAdvisory"
27+
"sensiolabs/security-checker": "Required by Check\\SecurityAdvisory",
28+
"symfony/yaml": "Required by Check\\YamlFile"
2729
},
2830
"extra": {
2931
"branch-alias": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace ZendDiagnostics\Check;
4+
5+
use InvalidArgumentException;
6+
use Traversable;
7+
use ZendDiagnostics\Result\Failure;
8+
use ZendDiagnostics\Result\FailureInterface;
9+
use ZendDiagnostics\Result\ResultInterface;
10+
use ZendDiagnostics\Result\Success;
11+
12+
/**
13+
* Abstract class for handling different file checks
14+
*/
15+
abstract class AbstractFileCheck extends AbstractCheck
16+
{
17+
/**
18+
* @var array|Traversable
19+
*/
20+
protected $files;
21+
22+
/**
23+
* @param string|array|Traversable $files Path name or an array / Traversable of paths
24+
* @throws InvalidArgumentException
25+
*/
26+
public function __construct($files)
27+
{
28+
if (is_object($files) && !$files instanceof Traversable) {
29+
throw new InvalidArgumentException(
30+
'Expected a file name (string) , an array or Traversable of strings, got ' . get_class($files)
31+
);
32+
}
33+
34+
if (!is_object($files) && !is_array($files) && !is_string($files)) {
35+
throw new InvalidArgumentException('Expected a file name (string) or an array of strings');
36+
}
37+
38+
if (is_string($files)) {
39+
$this->files = array($files);
40+
} else {
41+
$this->files = $files;
42+
}
43+
}
44+
45+
/**
46+
* @return ResultInterface
47+
*/
48+
public function check()
49+
{
50+
foreach ($this->files as $file) {
51+
if (!is_file($file) or !is_readable($file)) {
52+
return new Failure(sprintf('File "%s" does not exist or is not readable!', $file));
53+
}
54+
55+
if (($validationResult = $this->validateFile($file)) instanceof FailureInterface) {
56+
return $validationResult;
57+
}
58+
}
59+
60+
return new Success('All files are available and valid');
61+
}
62+
63+
/**
64+
* Validates a specific file type and returns a check result
65+
*
66+
* @param string $file
67+
* @return ResultInterface
68+
*/
69+
abstract protected function validateFile($file);
70+
}

src/ZendDiagnostics/Check/IniFile.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace ZendDiagnostics\Check;
4+
5+
use ZendDiagnostics\Result\Failure;
6+
use ZendDiagnostics\Result\Success;
7+
use ZendDiagnostics\Result\ResultInterface;
8+
9+
/**
10+
* Checks if an INI file is available and valid
11+
*/
12+
class IniFile extends AbstractFileCheck
13+
{
14+
/**
15+
* @param string $file
16+
* @return ResultInterface
17+
*/
18+
protected function validateFile($file)
19+
{
20+
if (!is_array($ini = parse_ini_file($file)) or count($ini) < 1 ) {
21+
return new Failure(sprintf('Could not parse INI file "%s"!', $file));
22+
}
23+
24+
return new Success();
25+
}
26+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace ZendDiagnostics\Check;
4+
5+
use ZendDiagnostics\Result\Failure;
6+
use ZendDiagnostics\Result\Success;
7+
use ZendDiagnostics\Result\ResultInterface;
8+
9+
/**
10+
* Checks if a JSON file is available and valid
11+
*/
12+
class JsonFile extends AbstractFileCheck
13+
{
14+
/**
15+
* @param string $file
16+
* @return ResultInterface
17+
*/
18+
protected function validateFile($file)
19+
{
20+
if (is_null(json_decode(file_get_contents($file)))) {
21+
return new Failure(sprintf('Could no decode JSON file "%s"', $file));
22+
}
23+
24+
return new Success();
25+
}
26+
}

src/ZendDiagnostics/Check/XmlFile.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace ZendDiagnostics\Check;
4+
5+
use XMLReader;
6+
use ZendDiagnostics\Result\Failure;
7+
use ZendDiagnostics\Result\Success;
8+
use ZendDiagnostics\Result\ResultInterface;
9+
10+
/**
11+
* Checks if an XML file is available and valid
12+
*/
13+
class XmlFile extends AbstractFileCheck
14+
{
15+
/**
16+
* @param string $file
17+
* @return ResultInterface
18+
*/
19+
protected function validateFile($file)
20+
{
21+
$xmlReader = new XMLReader();
22+
23+
if (!$xmlReader->open($file)) {
24+
return new Failure(sprintf('Could not open "%s" with XMLReader!', $file));
25+
}
26+
27+
$xmlReader->setParserProperty(XMLReader::VALIDATE, true);
28+
29+
if (!$xmlReader->isValid()) {
30+
return new Failure(sprintf('File "%s" is not valid XML!', $file));
31+
}
32+
33+
if (!@simplexml_load_file($file)) {
34+
return new Failure(sprintf('File "%s" is not well-formed XML!', $file));
35+
}
36+
37+
return new Success();
38+
}
39+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace ZendDiagnostics\Check;
4+
5+
use Symfony\Component\Yaml\Exception\ParseException;
6+
use Symfony\Component\Yaml\Parser;
7+
use ZendDiagnostics\Result\Failure;
8+
use ZendDiagnostics\Result\ResultInterface;
9+
use ZendDiagnostics\Result\Success;
10+
11+
/**
12+
* Checks if a YAML file is available and valid
13+
*/
14+
class YamlFile extends AbstractFileCheck
15+
{
16+
/**
17+
* @param string $file
18+
* @return ResultInterface
19+
*/
20+
protected function validateFile($file)
21+
{
22+
if (class_exists('Symfony\Component\Yaml\Parser')) {
23+
$parser = new Parser();
24+
25+
try {
26+
$parser->parse(file_get_contents($file));
27+
} catch (ParseException $e) {
28+
return new Failure(sprintf('Unable to parse YAML file "%s"!', $file), $e->getMessage());
29+
}
30+
31+
return new Success();
32+
}
33+
34+
if (function_exists('yaml_parse_file')) {
35+
if (@yaml_parse_file($file) === false) {
36+
return new Failure(sprintf('Unable to parse YAML file "%s"!', $file));
37+
}
38+
39+
return new Success();
40+
}
41+
42+
return new Failure('No YAML-parser found! Please install "symfony/yaml" or "PECL yaml >= 0.4.0".');
43+
}
44+
}

0 commit comments

Comments
 (0)