NotionCommotion Posted April 22, 2017 Share Posted April 22, 2017 How can I get rid of the Factory class and instead put in in closure, and shorten $this->get('Factory')->get('Accounts')->read()) to something like $this->factory('Accounts')->read())? Also, please provide constructive criticism of the below code other than my preliminary user authentication and logoff. Thank you <?php namespace Administrator; session_start(); use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response; spl_autoload_register(function ($classname) { $parts = explode('\\', $classname); require __DIR__."/../src/classes/".end($parts).".php"; }); require __DIR__.'/../vendor/autoload.php'; $app = new \Slim\App([ 'settings' => [ 'displayErrorDetails' => true, // set to false in production 'addContentLengthHeader' => false, // Allow the web server to send the content-length header 'determineRouteBeforeAppMiddleware' => true, 'config' => parse_ini_file(__DIR__.'/../config.ini',true), ], ]); $c = $app->getContainer(); $c['pdo'] = function ($c) { $db = $c->get('settings')['config']['db']; return new \PDO("mysql:host={$db['host']};dbname={$db['dbname']};charset={$db['charset']}",$db['username'],$db['password'],array(\PDO::ATTR_EMULATE_PREPARES=>false,\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY=>true,\PDO::ATTR_ERRMODE=>\PDO::ERRMODE_EXCEPTION,\PDO::ATTR_DEFAULT_FETCH_MODE=>\PDO::FETCH_OBJ)); }; $c['logger'] = function ($c) { $l = $c->get('settings')['config']['logger']; $logger = new \Monolog\Logger($l['name']); $logger->pushProcessor(new \Monolog\Processor\UidProcessor()); $logger->pushHandler(new \Monolog\Handler\StreamHandler($l['path'], \Monolog\Logger::DEBUG)); return $logger; }; $c['view'] = function ($c) { $view = new \Slim\Views\Twig(__DIR__.'/../src/templates', [ //'cache' => 'path/to/cache' // See auto_reload option 'debug' => true, 'strict_variables'=> true ]); $view->addExtension(new \Slim\Views\TwigExtension( $c['router'], $c['request']->getUri() )); $view->addExtension(new \Twig_Extension_Debug()); return $view; }; $c['Factory'] = function($c) { return new Factory($c->get('pdo'),$c->get('logger')); }; $app->get('/accounts', function (Request $request, Response $response) { return $this->view->render($response, 'accounts.html', $this->get('Factory')->get('Accounts')->read()); }); $app->get('/', function (Request $request, Response $response) { return $this->view->render($response, 'accounts.html', $this->get('Factory')->get('Accounts')->read()); }); $app->get('/logoff', function (Request $request, Response $response) { unset($_SESSION['auth']); return $this->view->render($response, 'logon.html'); }); $app->add(function(Request $request, Response $response, $next) { if(isset($_SESSION['auth'])) { return $next($request, $response); } else { //Obviously improve $post=$request->getParsedBody(); $db=[ 'Michael'=>'michael', 'nextUser'=>'nextPassword' ]; if(isset($post['username'],$post['password'],$db[$post['username']]) && $db[$post['username']]==$post['password']) { $_SESSION['auth']=true; return $this->view->render($response, 'accounts.html', $this->get('Factory')->get('Accounts')->read()); } else { return $this->view->render($response, 'logon.html'); } } }); $app->run(); <?php namespace Administrator; class Factory { protected $pdo, $logger; public function __construct($pdo, $logger) { $this->pdo=$pdo; $this->logger=$logger; } public function get($class) { $class='\Administrator\\'.$class; return new $class($this->pdo, $this->logger); } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.