Yaffs site version 1.1
[yaffs-website] / vendor / symfony / process / ProcessUtils.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Process;
13
14 use Symfony\Component\Process\Exception\InvalidArgumentException;
15
16 /**
17  * ProcessUtils is a bunch of utility methods.
18  *
19  * This class contains static methods only and is not meant to be instantiated.
20  *
21  * @author Martin HasoĊˆ <martin.hason@gmail.com>
22  */
23 class ProcessUtils
24 {
25     /**
26      * This class should not be instantiated.
27      */
28     private function __construct()
29     {
30     }
31
32     /**
33      * Escapes a string to be used as a shell argument.
34      *
35      * @param string $argument The argument that will be escaped
36      *
37      * @return string The escaped argument
38      */
39     public static function escapeArgument($argument)
40     {
41         //Fix for PHP bug #43784 escapeshellarg removes % from given string
42         //Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
43         //@see https://bugs.php.net/bug.php?id=43784
44         //@see https://bugs.php.net/bug.php?id=49446
45         if ('\\' === DIRECTORY_SEPARATOR) {
46             if ('' === $argument) {
47                 return escapeshellarg($argument);
48             }
49
50             $escapedArgument = '';
51             $quote = false;
52             foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
53                 if ('"' === $part) {
54                     $escapedArgument .= '\\"';
55                 } elseif (self::isSurroundedBy($part, '%')) {
56                     // Avoid environment variable expansion
57                     $escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
58                 } else {
59                     // escape trailing backslash
60                     if ('\\' === substr($part, -1)) {
61                         $part .= '\\';
62                     }
63                     $quote = true;
64                     $escapedArgument .= $part;
65                 }
66             }
67             if ($quote) {
68                 $escapedArgument = '"'.$escapedArgument.'"';
69             }
70
71             return $escapedArgument;
72         }
73
74         return "'".str_replace("'", "'\\''", $argument)."'";
75     }
76
77     /**
78      * Validates and normalizes a Process input.
79      *
80      * @param string $caller The name of method call that validates the input
81      * @param mixed  $input  The input to validate
82      *
83      * @return mixed The validated input
84      *
85      * @throws InvalidArgumentException In case the input is not valid
86      *
87      * Passing an object as an input is deprecated since version 2.5 and will be removed in 3.0.
88      */
89     public static function validateInput($caller, $input)
90     {
91         if (null !== $input) {
92             if (is_resource($input)) {
93                 return $input;
94             }
95             if (is_string($input)) {
96                 return $input;
97             }
98             if (is_scalar($input)) {
99                 return (string) $input;
100             }
101             // deprecated as of Symfony 2.5, to be removed in 3.0
102             if (is_object($input) && method_exists($input, '__toString')) {
103                 @trigger_error('Passing an object as an input is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
104
105                 return (string) $input;
106             }
107
108             throw new InvalidArgumentException(sprintf('%s only accepts strings or stream resources.', $caller));
109         }
110
111         return $input;
112     }
113
114     private static function isSurroundedBy($arg, $char)
115     {
116         return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1];
117     }
118 }