Ejemplo n.º 1
0
 /**
  * Setups the doctrine's connection
  * @param Configuration $configuration the configuration needed to connect to the database
  */
 public function setup(Configuration $configuration)
 {
     $logger = LoggerSingleton::getInstance()->getLogger();
     $connectionParams = ['driver' => getenv('DB_DRIVER'), 'host' => getenv('DB_HOST'), 'user' => getenv('DB_USER'), 'password' => getenv('DB_PASSWORD'), 'dbname' => getenv('DB_NAME')];
     if ($connectionParams['driver'] === 'pdo_sqlite') {
         $connectionParams['path'] = __DIR__ . "/../../../" . getenv('DB_NAME');
     }
     $logger->debug("Database setup - " . $connectionParams['dbname']);
     $this->configuration = $configuration;
     $this->connection = $connectionParams;
 }
Ejemplo n.º 2
0
<?php

use Doctrine\ORM\Tools\Setup;
use name\giacomofurlan\DoctrineSingleton;
$container = $app->getContainer();
$settings = $container->get('settings')['logger'];
$loggerSingleton = \name\giacomofurlan\LoggerSingleton::getInstance();
$loggerSingleton->createLogger($settings['name'], $settings['path']);
// monolog (SlimApp)
$container['logger'] = function ($container) {
    $loggerSingleton = \name\giacomofurlan\LoggerSingleton::getInstance();
    return $loggerSingleton->getLogger();
};
// twig
$container['view'] = function ($container) {
    $settings = $container->get('settings')['renderer'];
    $view = new \Slim\Views\Twig($settings['templates'], ['cache' => getenv('DEV_MODE') === '1' ? false : $settings['cache'], 'debug' => getenv('DEV_MODE') === '1']);
    $view->addExtension(new \Slim\Views\TwigExtension($container['router'], $container['request']->getUri()));
    return $view;
};
/** @var \Doctrine\ORM\Configuration $doctrineConfig */
/** @var DoctrineSingleton $doctrineSingleton */
$doctrineConfig = Setup::createAnnotationMetadataConfiguration(array(__DIR__ . DIRECTORY_SEPARATOR), getenv('DEV_MODE') === "1");
$doctrineSingleton = DoctrineSingleton::getInstance();
$doctrineSingleton->setup($doctrineConfig);
Ejemplo n.º 3
0
 /**
  * Handle the edit requests
  * GET: the edit form
  * POST: the result view
  *
  * @param Request $request
  * @param Response $response
  * @param integer $productId
  * @return Response
  */
 protected function handleEdit(Request $request, Response $response, $productId)
 {
     $logger = LoggerSingleton::getInstance()->getLogger();
     switch ($request->getMethod()) {
         case 'GET':
             $model = new ProductCreationModel();
             $product = null;
             try {
                 $product = $this->service->read($productId);
                 $model->setProduct($product);
             } catch (NotFoundException $ex) {
                 $logger->notice(__METHOD__ . " (" . __LINE__ . "): " . $ex->getMessage());
                 return $response->withStatus(302)->withAddedHeader("Location", $this->container->get('router')->pathFor('productList'));
             }
             return TwigUtil::render($this->container, $response, 'product/edit_form.twig', $model->jsonSerialize());
         case 'POST':
             $product = $this->productFromCreateEditForm($request);
             $files = $request->getUploadedFiles();
             /** @var UploadedFile $file */
             $file = null;
             if (array_key_exists('image', $files)) {
                 $file = $files['image'];
             }
             $model = new ProductCreationModel();
             $this->validateProduct($product, $file, $model);
             if (!$model->isSuccess()) {
                 $model->setProduct($product);
                 return TwigUtil::render($this->container, $response, 'product/edit_form.twig', $model->jsonSerialize());
             }
             $model->setIsSuccess($this->service->update($product));
             // default: success
             $template = 'product/edit_success.twig';
             if ($model->isSuccess()) {
                 try {
                     // delete previous image
                     if ($file->getSize()) {
                         foreach (glob(getenv('PRODUCT_IMAGES_DIRECTORY') . '*') as $entry) {
                             if (preg_match("/.*\\/{$productId}\\.[a-z]+\$/", $entry)) {
                                 unlink($entry);
                             }
                         }
                     }
                     Utils::writeUploadedFileToPath($file, getenv(PRODUCT_IMAGES_DIRECTORY), $product->getId());
                 } catch (\RuntimeException $ex) {
                     $model->setIsSuccess(false);
                     $message = "Can't write image file to the upload directory";
                     $model->appendMessage("{$message}. Please retry again later.");
                     $logger->error(__FILE__ . "(" . __LINE__ . "): {$message} :: " . $ex->getMessage());
                     $template = 'product/edit_form.twig';
                 }
             } else {
                 // error: back to the form
                 $template = 'product/edit_form.twig';
             }
             return TwigUtil::render($this->container, $response, $template, $model->jsonSerialize());
             $model->setIsSuccess($this->service->create($product));
             return $response->withStatus(501);
             // Not implemented yet
         // Not implemented yet
         default:
             return $response->withStatus(404);
             // Should never happen
     }
 }
Ejemplo n.º 4
0
 public function __construct()
 {
     $this->logger = LoggerSingleton::getInstance()->getLogger();
 }