|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Gitlab\Api; |
| 4 | + |
| 5 | +use Symfony\Component\OptionsResolver\OptionsResolver; |
| 6 | + |
| 7 | +class RepositoryFiles extends AbstractApi |
| 8 | +{ |
| 9 | + /** |
| 10 | + * @param int $project_id |
| 11 | + * @param string $file_path |
| 12 | + * @param string $ref |
| 13 | + * @return mixed |
| 14 | + */ |
| 15 | + public function getFile($project_id, $file_path, $ref) |
| 16 | + { |
| 17 | + return $this->get($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($file_path)), array( |
| 18 | + 'ref' => $ref |
| 19 | + )); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @param int $project_id |
| 24 | + * @param string $file_path |
| 25 | + * @param string $ref |
| 26 | + * @return mixed |
| 27 | + */ |
| 28 | + public function getRawFile($project_id, $file_path, $ref) |
| 29 | + { |
| 30 | + return $this->get($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($file_path).'/raw'), array( |
| 31 | + 'ref' => $ref, |
| 32 | + )); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @param int $project_id |
| 37 | + * @param array $parameters ( |
| 38 | + * |
| 39 | + * @var string $file_path Url encoded full path to new file. Ex. lib%2Fclass%2Erb. |
| 40 | + * @var string $branch Name of the branch. |
| 41 | + * @var string $start_branch Name of the branch to start the new commit from. |
| 42 | + * @var string $encoding Change encoding to 'base64'. Default is text. |
| 43 | + * @var string $author_email Specify the commit author's email address. |
| 44 | + * @var string $author_name Specify the commit author's name. |
| 45 | + * @var string $content File content. |
| 46 | + * @var string $commit_message Commit message. |
| 47 | + * ) |
| 48 | + * |
| 49 | + * @return mixed |
| 50 | + */ |
| 51 | + public function createFile($project_id, array $parameters = []) |
| 52 | + { |
| 53 | + $resolver = new OptionsResolver(); |
| 54 | + $resolver->setRequired('file_path'); |
| 55 | + $resolver->setRequired('branch'); |
| 56 | + $resolver->setDefined('start_branch'); |
| 57 | + $resolver->setDefined('encoding') |
| 58 | + ->setAllowedValues('encoding', ['text', 'base64']) |
| 59 | + ; |
| 60 | + $resolver->setDefined('author_email'); |
| 61 | + $resolver->setDefined('author_name'); |
| 62 | + $resolver->setRequired('content'); |
| 63 | + $resolver->setRequired('commit_message'); |
| 64 | + |
| 65 | + $resolved = $resolver->resolve($parameters); |
| 66 | + |
| 67 | + return $this->post($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($resolved['file_path'])), $resolved); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @param int $project_id |
| 72 | + * @param array $parameters ( |
| 73 | + * |
| 74 | + * @var string $file_path Url encoded full path to new file. Ex. lib%2Fclass%2Erb. |
| 75 | + * @var string $branch Name of the branch. |
| 76 | + * @var string $start_branch Name of the branch to start the new commit from. |
| 77 | + * @var string $encoding Change encoding to 'base64'. Default is text. |
| 78 | + * @var string $author_email Specify the commit author's email address. |
| 79 | + * @var string $author_name Specify the commit author's name. |
| 80 | + * @var string $content File content. |
| 81 | + * @var string $commit_message Commit message. |
| 82 | + * @var string $last_commit_id Last known file commit id. |
| 83 | + * ) |
| 84 | + * |
| 85 | + * @return mixed |
| 86 | + */ |
| 87 | + public function updateFile($project_id, array $parameters = []) |
| 88 | + { |
| 89 | + $resolver = new OptionsResolver(); |
| 90 | + $resolver->setRequired('file_path'); |
| 91 | + $resolver->setRequired('branch'); |
| 92 | + $resolver->setDefined('start_branch'); |
| 93 | + $resolver->setDefined('encoding') |
| 94 | + ->setAllowedValues('encoding', ['text', 'base64']) |
| 95 | + ; |
| 96 | + $resolver->setDefined('author_email'); |
| 97 | + $resolver->setDefined('author_name'); |
| 98 | + $resolver->setRequired('content'); |
| 99 | + $resolver->setRequired('commit_message'); |
| 100 | + $resolver->setDefined('last_commit_id'); |
| 101 | + |
| 102 | + $resolved = $resolver->resolve($parameters); |
| 103 | + |
| 104 | + return $this->put($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($resolved['file_path'])), $resolved); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @param int $project_id |
| 109 | + * @param array $parameters ( |
| 110 | + * |
| 111 | + * @var string $file_path Url encoded full path to new file. Ex. lib%2Fclass%2Erb. |
| 112 | + * @var string $branch Name of the branch. |
| 113 | + * @var string $start_branch Name of the branch to start the new commit from. |
| 114 | + * @var string $author_email Specify the commit author's email address. |
| 115 | + * @var string $author_name Specify the commit author's name. |
| 116 | + * @var string $commit_message Commit message. |
| 117 | + * ) |
| 118 | + * |
| 119 | + * @return mixed |
| 120 | + */ |
| 121 | + public function deleteFile($project_id, array $parameters = []) |
| 122 | + { |
| 123 | + $resolver = new OptionsResolver(); |
| 124 | + $resolver->setRequired('file_path'); |
| 125 | + $resolver->setRequired('branch'); |
| 126 | + $resolver->setDefined('start_branch'); |
| 127 | + $resolver->setDefined('author_email'); |
| 128 | + $resolver->setDefined('author_name'); |
| 129 | + $resolver->setRequired('commit_message'); |
| 130 | + |
| 131 | + $resolved = $resolver->resolve($parameters); |
| 132 | + |
| 133 | + return $this->delete($this->getProjectPath($project_id, 'repository/files/'.$this->encodePath($resolved['file_path'])), $resolved); |
| 134 | + } |
| 135 | +} |
0 commit comments