naneau /
filegen
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | namespace Naneau\FileGen; |
||
| 3 | |||
| 4 | use Naneau\FileGen\Directory; |
||
| 5 | use Naneau\FileGen\File; |
||
| 6 | use Naneau\FileGen\SymLink; |
||
| 7 | |||
| 8 | use Naneau\FileGen\Parameter\Set as ParameterSet; |
||
| 9 | |||
| 10 | use Naneau\FileGen\Structure\Exception as StructureException; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * A structure |
||
| 14 | */ |
||
| 15 | class Structure extends Directory |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The parameter definition |
||
| 19 | * |
||
| 20 | * @var ParameterSet |
||
| 21 | **/ |
||
| 22 | private $parameterDefinition; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Constructor |
||
| 26 | * |
||
| 27 | * @return void |
||
|
0 ignored issues
–
show
|
|||
| 28 | **/ |
||
| 29 | public function __construct() |
||
| 30 | { |
||
| 31 | // Although the root node (Structure) is a directory, it does not have |
||
| 32 | // a "name", relative to the root |
||
| 33 | parent::__construct(''); |
||
| 34 | |||
| 35 | // Initialize the parameter definition |
||
| 36 | $this->setParameterDefinition(new ParameterSet); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Add a file |
||
| 41 | * |
||
| 42 | * @param string $name |
||
| 43 | * @param FileContents|string $contents |
||
| 44 | * @param int $mode mode in octal 0XXX |
||
| 45 | * @return Structure |
||
| 46 | **/ |
||
| 47 | public function file($name, $contents = '', $mode = null) |
||
| 48 | { |
||
| 49 | // Create the file itself |
||
| 50 | $file = new File(basename($name), $contents, $mode); |
||
| 51 | |||
| 52 | $parent = $this->parentDirectory($name); |
||
| 53 | $parent->addChild($file); |
||
| 54 | |||
| 55 | return $this; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Add a directory |
||
| 60 | * |
||
| 61 | * @param string $name |
||
| 62 | * @param int $mode mode in octal 0XXX |
||
| 63 | * @return Structure |
||
| 64 | **/ |
||
| 65 | public function directory($name, $mode = null) |
||
| 66 | { |
||
| 67 | // Create the file itself |
||
| 68 | $directory = new Directory(basename($name), $mode); |
||
| 69 | |||
| 70 | $parent = $this->parentDirectory($name); |
||
| 71 | $parent->addChild($directory); |
||
| 72 | |||
| 73 | return $this; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Create a symlink |
||
| 78 | * |
||
| 79 | * @param string $from |
||
| 80 | * @param string $to |
||
| 81 | * @return Structure |
||
| 82 | **/ |
||
| 83 | public function link($from, $to) |
||
| 84 | { |
||
| 85 | // Create the file itself |
||
| 86 | $link = new SymLink($from, basename($to)); |
||
| 87 | |||
| 88 | $parent = $this->parentDirectory($to); |
||
| 89 | $parent->addChild($link); |
||
| 90 | |||
| 91 | return $this; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Add a parameter |
||
| 96 | * |
||
| 97 | * @param string $name name of the parameter |
||
| 98 | * @param string $description (optional) human readable description |
||
| 99 | * @return Structure |
||
| 100 | **/ |
||
| 101 | public function parameter($name, $description = null) |
||
| 102 | { |
||
| 103 | $this->getParameterDefinition()->add($name, $description); |
||
| 104 | |||
| 105 | return $this; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get the parameter definition |
||
| 110 | * |
||
| 111 | * @return ParameterSet |
||
| 112 | */ |
||
| 113 | public function getParameterDefinition() |
||
| 114 | { |
||
| 115 | return $this->parameterDefinition; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Set the parameter definition |
||
| 120 | * |
||
| 121 | * @param ParameterSet $parameterDefinition |
||
| 122 | * @return Structure |
||
| 123 | */ |
||
| 124 | public function setParameterDefinition(ParameterSet $parameterDefinition) |
||
| 125 | { |
||
| 126 | $this->parameterDefinition = $parameterDefinition; |
||
| 127 | |||
| 128 | return $this; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Create (and add) a parent directory for a path |
||
| 133 | * |
||
| 134 | * @param string $name |
||
| 135 | * @return void |
||
| 136 | **/ |
||
| 137 | private function parentDirectory($name) |
||
| 138 | { |
||
| 139 | // Parent path |
||
| 140 | $parentPath = dirname(trim($name, DIRECTORY_SEPARATOR)); |
||
| 141 | |||
| 142 | // There is no parent path (parent directory is the root) |
||
| 143 | if ($parentPath === '.') { |
||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | |||
| 147 | // Directories to add |
||
| 148 | $directories = explode(DIRECTORY_SEPARATOR, $parentPath); |
||
| 149 | |||
| 150 | $parent = $this; |
||
| 151 | $dirCount = count($directories); |
||
| 152 | for ($x = 0; $x < $dirCount; $x++) { |
||
| 153 | |||
| 154 | // Going through directories, highest level first |
||
| 155 | $directory = $directories[$x]; |
||
| 156 | |||
| 157 | if ($parent->hasChild($directory)) { |
||
| 158 | // If the parent already has a child by the name of $directory |
||
| 159 | |||
| 160 | // Fetch the current child by that name |
||
| 161 | $childDir = $parent->getChild($directory); |
||
| 162 | |||
| 163 | // Make sure we get a directory back (it may be a file) |
||
| 164 | if (!($childDir instanceof Directory)) { |
||
| 165 | throw new StructureException(sprintf( |
||
| 166 | 'Trying to add directory where there is a file already: "%s"', |
||
| 167 | $parent->getFullName() . DIRECTORY_SEPARATOR . $directory |
||
| 168 | )); |
||
| 169 | } |
||
| 170 | } else { |
||
| 171 | // Child directory does not exist yet, create a new one |
||
| 172 | $childDir = new Directory($directory); |
||
| 173 | |||
| 174 | // Add the child to the old parent |
||
| 175 | $parent->addChild($childDir); |
||
| 176 | } |
||
| 177 | |||
| 178 | // Next directory with the new parent |
||
| 179 | $parent = $childDir; |
||
| 180 | } |
||
| 181 | |||
| 182 | return $parent; |
||
| 183 | } |
||
| 184 | } |
||
| 185 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.