Esempio n. 1
5
 /**
  * Simple method to move (small) uploaded files in the filesystem
  * If the file's size is 0, it simply returns;
  * @param UploadedFile $file the file to be moved
  * @param string $path the path where to copy the file
  * @param string $newBasename the file's new basename (name without extension)
  * @throws \RuntimeException in case of errors
  * @return null
  */
 public static function writeUploadedFileToPath(UploadedFile $file, $path, $newBasename)
 {
     if (!$file->getSize()) {
         return;
     }
     $ext = pathinfo($file->getClientFilename(), PATHINFO_EXTENSION);
     $filename = $newBasename . ".{$ext}";
     $path = $path . $filename;
     file_put_contents($path, $file->getStream()->getContents());
 }
Esempio n. 2
0
 public function upload($name)
 {
     $name = sprintf('%s%s.%s', $this->destination, $name, $this->extension);
     $this->file->moveTo($name);
     $this->oldFilename = $this->filename;
     $this->filename = $name;
     return $this->filename;
 }
Esempio n. 3
0
 public function requestFactory($acceptType = 'application/json')
 {
     $env = Environment::mock(['HTTP_ACCEPT' => $acceptType]);
     $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123');
     $headers = Headers::createFromEnvironment($env);
     $serverParams = $env->all();
     $body = new RequestBody();
     $uploadedFiles = UploadedFile::createFromEnvironment($env);
     $request = new Request('GET', $uri, $headers, [], $serverParams, $body, $uploadedFiles);
     return $request;
 }
Esempio n. 4
0
 /**
  * @param $method
  * @param string $url
  *
  * @return Request
  */
 public function makeRequest($method, $url = '/')
 {
     $env = Environment::mock();
     $uri = Uri::createFromString('http://example.com' . $url);
     $headers = Headers::createFromEnvironment($env);
     $serverParams = $env->all();
     $body = new RequestBody();
     $uploadedFiles = UploadedFile::createFromEnvironment($env);
     $request = new Request($method, $uri, $headers, [], $serverParams, $body, $uploadedFiles);
     return $request;
 }
 public function requestFactory($queryString = '')
 {
     $env = Environment::mock();
     $uri = Uri::createFromString('https://example.com:443/foo/bar' . $queryString);
     $headers = Headers::createFromEnvironment($env);
     $cookies = ['user' => 'john', 'id' => '123'];
     $serverParams = $env->all();
     $body = new RequestBody();
     $uploadedFiles = UploadedFile::createFromEnvironment($env);
     $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
     return $request;
 }
Esempio n. 6
0
 /**
  * @param string $method
  * @param string $uri
  * @return Request
  */
 public function makeRequest($method = 'GET', $uri = '')
 {
     $env = Environment::mock([]);
     $uri = Uri::createFromString($uri);
     $headers = Headers::createFromEnvironment($env);
     $cookies = [];
     $serverParams = $env->all();
     $body = new RequestBody();
     $uploadedFiles = UploadedFile::createFromEnvironment($env);
     $request = new Request($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
     $request = $request->withHeader("Content-Type", "application/x-www-form-urlencoded");
     return $request;
 }
 public function setupRequest($url, $method, $request, $files)
 {
     $env = Environment::mock(['REQUEST_URI' => $url, 'REQUEST_METHOD' => $method, 'HTTP_CONTENT_TYPE' => 'multipart/form-data; boundary=---foo']);
     $uri = Uri::createFromEnvironment($env);
     $headers = Headers::createFromEnvironment($env);
     $files = UploadedFile::createFromEnvironment(Environment::mock());
     $cookies = [];
     $serverParams = $env->all();
     $body = new RequestBody();
     $this->request = new Request($method, $uri, $headers, $cookies, $serverParams, $body, $files);
     $this->response = new Response();
     $app = $this->app;
     return $app($this->request, $this->response);
 }
 /**
  * @param array $serverParams
  * @return ServerRequest
  */
 public function makeServerRequest(array $serverParams)
 {
     // I have no idea why I need to type hint here, but phpstorm is complaining
     /** @var $serverRequest ServerRequest */
     $headers = Headers::createFromArray($serverParams);
     $cookies = Cookies::parseHeader($headers->get('Cookie', []));
     $serverRequest = new ServerRequest($serverParams, $cookies);
     $serverRequest = $serverRequest->withMethod($serverParams['REQUEST_METHOD']);
     $serverRequest = $serverRequest->withUri();
     foreach ($headers->all() as $header => $value) {
         $serverRequest = $serverRequest->withHeader($header, $value);
     }
     $serverRequest = $serverRequest->withUploadedFiles(UploadedFile::parseUploadedFiles($_FILES));
     return $serverRequest;
 }
Esempio n. 9
0
 public static function fromGlobals(array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null)
 {
     $environment = new Environment($server);
     $method = $environment['REQUEST_METHOD'];
     $uri = Uri::createFromEnvironment($environment);
     $headers = Headers::createFromEnvironment($environment);
     $cookies = Cookies::parseHeader($headers->get('Cookie', []));
     $serverParams = $environment->all();
     $body = new RequestBody();
     $uploadedFiles = UploadedFile::createFromEnvironment($environment);
     $request = new ServerRequest($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
     if ($method === 'POST' && in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])) {
         // parsed body must be $_POST
         $request = $request->withParsedBody($_POST);
     }
     return $request;
 }
Esempio n. 10
0
 public static function createFromGlobals(array $globals)
 {
     $env = new Collection($globals);
     $method = $env->get('REQUEST_METHOD');
     $uri = Uri::createFromGlobals($globals);
     $headers = Headers::createFromGlobals($globals);
     $cookies = Cookies::parseHeader($headers->get('Cookie', []));
     $serverParams = $globals;
     $body = new RequestBody();
     $uploadedFiles = UploadedFile::createFromGlobals($globals);
     $request = new static($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
     if ($method === 'POST' && in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])) {
         // parsed body must be $_POST
         $request = $request->withParsedBody($_POST);
     }
     return $request;
 }
Esempio n. 11
0
 /**
  * Validates a product and the relative uploaded file
  * Eventually sets the model's success and error messages (if any)
  * @param Product $product
  * @param UploadedFile $uploadedFile
  * @param ProductCreationModel $model
  */
 private function validateProduct(Product $product, UploadedFile $uploadedFile, ProductCreationModel $model)
 {
     // Validation rules:
     // Product must have a name
     // Product must have at least one tag
     // (extra) Product image, if present, must be an image (gif, png, jpeg)
     if (!$product->getName()) {
         $model->appendMessage("The product's name mustn't be empty!");
     }
     if (!$product->getTags()) {
         $model->appendMessage("The product must have at least one tag!");
     }
     if ($uploadedFile->getSize() && !in_array($uploadedFile->getClientMediaType(), static::$supportedImageMIMETYpes)) {
         $model->appendMessage("The product's image file is invalid (wrong type)!");
     }
     // if the model's messages are empty, validation passed
     $model->setIsSuccess(!$model->getMessages());
 }
 /**
  * @param array $mockEnv An array representing a mock environment.
  *
  * @return Request
  */
 public function requestFactory(array $mockEnv)
 {
     $env = Environment::mock();
     $uri = Uri::createFromString('https://example.com:443/foo/bar?abc=123');
     $headers = Headers::createFromEnvironment($env);
     $cookies = [];
     $serverParams = $env->all();
     $body = new RequestBody();
     $uploadedFiles = UploadedFile::createFromEnvironment($env);
     $request = new Request('GET', $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
     return $request;
 }