public function register(Application $app) { if (class_exists('\\Imagick')) { $app['intervention.driver'] = 'imagick'; } else { $app['intervention.driver'] = 'gd'; } $app['intervention.image'] = $app->share(function (Application $app) { return new ImageManager(array('driver' => $app['intervention.driver'])); }); /** * For calling like: * return $app['intervention.response']($image); */ $app['intervention.response'] = $app->protect(function (Image $image) use($app) { return $app->stream(function () use($image) { echo $image->response(); }, 200); }); }
public function testAction(Application $app) { $method = __METHOD__; return $app->stream(function () use($method, $app) { var_dump($method, $app['module']); }); }
/** * @param Application $app * @param Request $request * @param Photo $photo * @param int $width * @param int $height * @param string $algorithm * @return Response */ public function thumbnailAction(Application $app, Request $request, $photo, $width, $height = null, $algorithm = null) { if (!$photo) { return $app->abort(404); } $thumbnailPath = $app['photos']->getThumbnail($photo, $width, $height, $algorithm); $stream = function () use($thumbnailPath) { $resource = imagecreatefromjpeg($thumbnailPath); imagejpeg($resource); }; return $app->stream($stream, 200, ['Content-type' => 'image/jpeg', 'Content-Disposition' => sprintf('filename="%s"', $photo->filename)]); }
public function testStreamActuallyStreams() { $i = 0; $stream = function () use(&$i) { $i++; }; $app = new Application(); $response = $app->stream($stream); $this->assertEquals(0, $i); $request = Request::create('/stream'); $response->prepare($request); $response->sendContent(); $this->assertEquals(1, $i); }
/** * @see ControllerProviderInterface::connect */ public function connect(Application $app) { // Global layout $app->before(function () use($app) { $app['twig']->addGlobal('layout', $app['twig']->loadTemplate('layout.twig')); }); // Error management if (!$app['debug']) { $app->error(function (\Exception $e, $code) use($app) { if ($code >= 400 && $code < 500) { $message = $e->getMessage(); } else { $message = 'Whoops, looks like something went wrong.'; } // In case twig goes wrong, exemple: no route found means the // $app->before() wont be executed try { $app['user'] = false; $app['twig']->addGlobal('layout', $app['twig']->loadTemplate('layout.twig')); return $app['twig']->render('error.twig', array('message' => $message, 'code' => $code)); } catch (\Exception $e) { return new Response('Whoops, looks like something went very wrong.', $code); } }); } // creates a new controller based on the default route $controllers = $app['controllers_factory']; // Homepage + form handler $controllers->get('/', function (Request $request) use($app) { return $app['twig']->render('index.twig', array('form' => $app['short_url.form']->createView(), 'last' => $app['short_url']->getLastShorten(10))); })->bind('short_url_homepage'); // Handle the form submission $controllers->post('/', function (Request $request) use($app) { $form = $app['short_url.form']; $form->bind($request); if ($form->isValid()) { $data = $form->getData(); $email = $app['user']['email'] ? $app['user']['email'] : null; $id = $app['short_url']->add($data['url'], $email); $url_details = $app['short_url']->getById($id); $r_url = $app['url_generator']->generate('short_url_details', array('short_code' => $url_details['short_code'])); return $app->redirect($r_url); } else { return $app['twig']->render('index.twig', array('form' => $form->createView(), 'last' => $app['short_url']->getLastShorten(10))); } $app->abort(404, "Nothing found!"); }); // Details $controllers->get('/{short_code}/details', function ($short_code) use($app) { $url_details = $app['short_url']->getByShortCode($short_code); $last_redirects = $app['short_url']->getLastRedirects($url_details['id']); $redirects_counter = $app['short_url']->getRedirectCounter($url_details['id']); return $app['twig']->render('details.twig', array('long_url' => $url_details['url'], 'short_code' => $short_code, 'last_redirects' => $last_redirects, 'redirects_counter' => $redirects_counter)); })->bind('short_url_details'); // QRCode $controllers->get('/{short_code}.png', function ($short_code) use($app) { $url_details = $app['short_url']->getByShortCode($short_code); if ($url_details) { $short_url = $app['url_generator']->generate('short_url_redirect', array('short_code' => $short_code), true); $file = $_SERVER['DOCUMENT_ROOT'] . "/qr/{$short_code}.png"; if (!file_exists($file)) { QRcode::png($short_url, $file, 'L', 4, 2); } $stream = function () use($file) { readfile($file); }; return $app->stream($stream, 200, array('Content-Type' => 'image/png')); } $app->abort(404, "That shorten url does not exist!"); })->bind('short_url_qrcode'); // Shorten the url in the query string (?url=) $controllers->get('/shorten/', function (Request $request) use($app) { $url = rawurldecode($request->get('url')); $errors = $app['validator']->validateValue($url, new Assert\Url()); if ($url && !$errors->has(0)) { $id = $app['short_url']->add($url); $url_details = $app['short_url']->getById($id); $r_url = $app['url_generator']->generate('short_url_details', array('short_code' => $url_details['short_code'])); return $app->redirect($r_url); } else { $app->abort(404, $errors->get(0)->getMessage()); } $app->abort(404, "The url query string parameter is required."); })->bind('short_url_shorten'); // Redirects to the last shorten url $controllers->get('/last/', function () use($app) { $urls = $app['short_url']->getLastShorten(1); if ($urls[0]['id']) { $app['short_url']->incrementCounter($urls[0]['id']); return $app->redirect($urls[0]['url']); } $app->abort(404, "Nothing found!"); })->bind('short_url_last'); // Redirects to the corresponding url $controllers->get('/{short_code}', function ($short_code) use($app) { $url = $app['short_url']->getByShortCode($short_code); if ($url) { $app['short_url']->incrementCounter($url['id']); return $app->redirect($url['url']); } $app->abort(404, "That shorten url does not exist!"); })->bind('short_url_redirect'); // User's last shorten urls $controllers->get('/mine/', function () use($app) { if (!$app['user']['email']) { $app->abort(401, "You must be authenticated to access this page."); } return $app['twig']->render('mine.twig', array('last' => $app['short_url']->getLastShorten(10, $app['user']['email']))); })->bind('short_url_mine'); return $controllers; }
private function streamImage(Application $app, Image $image) { $image->encode(null, $this->default_quality); $mime = self::getMimeFromData($image->getEncoded()); return $app->stream(function () use($image) { // echo $image->response(); echo $image->getEncoded(); }, 200, array('Content-Type' => $mime)); }
} return $this->finish(true, $new_build_path); } } $app->post('/deploy/{service}', function (Application $app, Request $request, $service) { if (isset($app['config'][$service])) { $config = $app['config'][$service]; $secret = $request->get("secret"); if ($secret != $config['secret']) { return new Response("Forbidden", 403, array('Content-Type' => 'text/plain')); } $deployer = new Deployer($service, $config); $stream = function () use($deployer, $request) { $deployer->deploy($request); }; return $app->stream($stream, 200, array('Content-Type' => 'text/plain')); } return "ERROR: missing service: " . $service; }); $app->post('/rollback/{service}', function (Application $app, Request $request, $service) { if (isset($app['config'][$service])) { $config = $app['config'][$service]; $secret = $request->get("secret"); if ($secret != $config['secret']) { return new Response("Forbidden", 403, array('Content-Type' => 'text/plain')); } $deployer = new Deployer($service, $config); $deployer->recursive_unlink($config['current_path']); if (!@rename($config['old_path'], $config['current_path'])) { $deployer->recurse_copy($config['old_path'], $config['current_path']); }
$finder = new Finder(); $finder->files()->in($basePath)->name('*.js')->sort(function (SplFileInfo $first, SplFileInfo $second) { return !strcmp($first->getRealPath(), $second->getRealPath()); }); foreach ($finder as $file) { $files[str_replace('v', 'version ', $file->getRelativePath())][] = $file; } return $app['twig']->render('/pages/download.html.twig', array('relativeBaseFolder' => $relativeBaseFolder, 'files' => $files)); })->bind('download'); $app->get('/get/{filename}', function ($filename) use($app, $relativeBaseFolder) { $filename = __DIR__ . '/' . $relativeBaseFolder . '/' . $filename; if ($app['filesystem']->exists($filename)) { $file = new File($filename, true); $fileInfos = new SplFileInfo($filename); return $app->stream(function () use($fileInfos) { readfile($fileInfos->getRealPath()); }, 200, array('Content-Type' => $file->getMimeType(), 'Content-Length' => $fileInfos->getSize(), 'Content-Disposition' => 'attachment; filename="' . $fileInfos->getFilename() . '"')); } else { $app->abort(404, 'The file you are looking for cannot be found !'); } })->bind('get')->assert('filename', '[a-zA-Z0-9-_/.]*'); // Demonstrations $app->get('/demonstrations', function () use($app) { return $app['twig']->render('/pages/demonstrations.html.twig'); })->bind('demonstrations'); // Prices $app->get('/prices', function () use($app) { return $app['twig']->render('/pages/prices.html.twig'); })->bind('prices'); // Errors $app->error(function (Exception $exception, $code) use($app) {