checkGivenArguments.func.php 3.21 KB
Newer Older
Felix Flor's avatar
Felix Flor committed
1 2
<?php

Felix Flor's avatar
Felix Flor committed
3
namespace APHPBreakdown;
Felix Flor's avatar
Felix Flor committed
4

5
/**
6
 *
7
 * @param array[] $argv		- array containing the arguments given at program start
8
 *
9
 * @throws \Exception 		- if the given argument is not of type array
10
 *
Felix Flor's avatar
Felix Flor committed
11 12
 * this funtion perform an output if the arguments they are given are valid.
 * if it is not valid exit and give an error message.
13
 *
14
 **/
Felix Flor's avatar
Felix Flor committed
15

16 17
function checkGivenArguments($argv) {

18 19 20 21 22
    if($argv[1] == '-v' || $argv[1] == '--version'){
        echo "APHPBreakdown version ".VERSION." ".BUILD_DATE."\n";
        exit(0);
    }

23
	if(!is_array($argv))
Felix Flor's avatar
Felix Flor committed
24
		throw new \Exception ("APHPBreakdown: Invalid parameter type! Must: array, Is: " . gettype($argv));
Felix Flor's avatar
Felix Flor committed
25

26
	$acceptedOutputArguments = array("--console-basic","--console-full", "--console-makefile", "--testfile");
27
	$acceptedTemplateArguments = array("--basic", "--advanced", "--ci");
28

29 30
	$path = realpath($argv[1]);

31
	if(!isset($path) || !isset($argv[2]))
32
		exitWithErrorMessage("APHPBreakdown: Not enough arguments given!\n\t\t aphpbreakdown <PATH> <OPTIONS> [--testfile <TEMPLATE>] [--testfile <EXPORT_FILENAME>]\n\t\t <PATH> /path/to/dir\n\t\t <OPTIONS> --console-basic | --console-full | --console-makefile | --testfile\n\t\t [--testfile <TEMPLATE>] --basic | --advanced | --ci\n\t\t [<EXPORT_FILENAME>] optional, no path, don't forget file extension\n");
33 34

	if(!is_dir($path))
Felix Flor's avatar
Felix Flor committed
35
		exitWithErrorMessage("APHPBreakdown: There is no directory found on path \"$path\"\n");
Felix Flor's avatar
Felix Flor committed
36

37 38 39 40 41
	/**
	 * checks the readability of file and directories given in argument.
	 * if anything is not readable prints out the info what is not readable
	 * and exits at last.
	 **/
42
	$filesAndDirectoriesInfo = getFilesystemPathAndReadability($path);
43 44 45 46 47 48 49 50
	$notReadableFileOrDirectoryCounter = 0;
	foreach($filesAndDirectoriesInfo as $fileAndDirectorieInfo){
		if(!$fileAndDirectorieInfo['isReadable']){
			echo ($fileAndDirectorieInfo['isFile']) ? "File \"" : "Directory \"";
			echo $fileAndDirectorieInfo['path']."\" is not readable. \n";
			$notReadableFileOrDirectoryCounter++;
		}
	}
51

52
	if($notReadableFileOrDirectoryCounter > 0)
Felix Flor's avatar
Felix Flor committed
53
		exitWithErrorMessage("APHPBreakdown: There are sone or more Files which are not readable. Check the Output above for Details.\nChange the file Permissions. To do that use this command \"$ chmod u+r <FILE>\"\n");
54

Felix Flor's avatar
Felix Flor committed
55
	if(!isset($argv[2]))
56
		exitWithErrorMessage("APHPBreakdown: You have to give argument for output. ( aphpbreakdown <PATH> <OUTPUT> [ --console-basic | --console-full | --console-makefile | --testfile ] ).\n");
Felix Flor's avatar
Felix Flor committed
57

58
	if(!in_array($argv[2], $acceptedOutputArguments))
59
		exitWithErrorMessage("APHPBreakdown: The Argument you have given is not valid. Avaible Options: <OUTPUT> [ --console-basic | --console-full | --console-makefile | --testfile ].\n");
60

61
	if($argv[2] == "--testfile" && !isset($argv[3]))
62
		exitWithErrorMessage("APHPBreakdown: Missing Argument for --testfile. Avaible Options: [<TEMPLATE>] [ --basic | --advanced | --ci].\n");
63 64

	if($argv[2] == "--testfile" && !in_array($argv[3], $acceptedTemplateArguments))
65
		exitWithErrorMessage("APHPBreakdown: The Argument you have given is not valid. Avaible Options: [<TEMPLATE>] [ --basic | --advanced | --ci].\n");
66

67
	if(preg_match('/[^A-Za-z0-9 _ .-]/', $argv[4]) && isset($argv[4]))
Felix Flor's avatar
Felix Flor committed
68
		exitWithErrorMessage("APHPBreakdown: You use inadmissible charachters for filename! Not Path! \n");
69

Felix Flor's avatar
Felix Flor committed
70
}