How to get access to $app inside a controller as the Slim 3.3 injects only the ContainerInterface?
Code to illustrate the question:
$app = new \Slim\App;
$app->get('/home', 'HomeController:get');
$app->run();
class HomeController {
private $ci;
public function _construct($ci) {
$this->ci = $ci;
}
public function get($request, $response) {
$this->ci->get(...);
// How to access $app and dependencies like $app->jwt?
}
}
This was a tough one.
Slim 3 heavily uses dependency injection, so you might want to use it too.
First inside your dependencies.php you need to grab the $app and throw it in a container to inject it to the Controller later.
$container['slim'] = function ($c) {
global $app;
return $app;
};
Then you got to inject it:
// Generic Controller
$container['App\Controllers\_Controller'] = function ($c) {
return new _Controller($c->get('slim'));
};
Now on your controller.php:
private $slim;
/**
* #param \Psr\Log\LoggerInterface $logger
* #param \App\DataAccess $dataaccess
* #param \App\$app $slim
*/
public function __construct(LoggerInterface $logger, _DataAccess $dataaccess, $slim)
{
$this->logger = $logger;
$this->dataaccess = $dataaccess;
$this->slim = $slim;
}
Now you just got call it like this:
$this->slim->doSomething();
You can make your own 'singleton' to mimic Slim::getInstance(); ;)
class Anorexic extends \Slim\App {
private static $_instance;
public static function getInstance(){
if(empty(self::$_instance){
self::$_instance = new self();
}
return self::$_instance;
}
}
Then change your initialization like this:
// $app = new \Slim\App;
$app = Anorexic::getInstance();
Now you can get your \Slim\App instance anywhere in your code by calling Anorexic::getInstance(); Ofcourse you should never try this at home :P
I am getting error in below code, cause am not able to access $log in static function Log which gets initialized in _construct.
class Logger extends Singleton{
protected function __construct() {
if(!class_exists("Log")) {
include '/usr/php/Log.php';
}
$MONITORING_LOGFILE = "/var/log/Monitoring.log";
ini_set('error_log', 'syslog');
openlog($MONITORING_LOGFILE, LOG_NDELAY, LOG_LOCAL0);
$log = Log::singleton('syslog', LOG_LOCAL0, $MONITORING_LOGFILE, array('lineFormat' => ' %{message}'), PEAR_LOG_DEBUG);
}
public static function Log($message){
$log->err($message);
}
}
Ok, I modified the above code
class Logger extends Singleton{
private $log;
protected function __construct() {
if(!class_exists("Log")) {
include '/usr/php/Log.php';
}
$MONITORING_LOGFILE = "/var/log/Monitoring.log";
ini_set('error_log', 'syslog');
openlog($MONITORING_LOGFILE, LOG_NDELAY, LOG_LOCAL0);
$this->log = Log::singleton('syslog', LOG_LOCAL0, $MONITORING_LOGFILE, array('lineFormat' => ' %{message}'), PEAR_LOG_DEBUG);
}
public function Log($message){
$this->log->err($message);
}
}
and now its working fine .... just want to confirm if initializng like this is ok in Singleton pattern?
To be able to access the $log variable trough a static function you need to have a reference of it:
class Logger extends Singleton{
private static $log; //static instance of Log::singleton
protected function __construct() {
if(!class_exists("Log")) {
include '/usr/php/Log.php';
}
$MONITORING_LOGFILE = "/var/log/Monitoring.log";
ini_set('error_log', 'syslog');
openlog($MONITORING_LOGFILE, LOG_NDELAY, LOG_LOCAL0);
self::$log = Log::singleton('syslog', LOG_LOCAL0, $MONITORING_LOGFILE, array('lineFormat' => ' %{message}'), PEAR_LOG_DEBUG);
}
//static method
public static function Log($message){
self::$log->err($message);
}
}
To create your instance of the class Logger and access the static Log function you can do the following:
$mylog = new Logger();
$mylog::Log("Your text here");
I am trying to work through getting the PHP client for Google Map to work correctly.
I've downloaded a local copy of the GoogleAPI PHP Client from GitHub:https://github.com/google/google-api-php-client.
I am running PHP v5.4 on IIS8. The GoogleAPI was installed in the PHP Include folder, under GoogleAPI.
PHP works correctly with all my other scripts.
I am trying get the example to work from Maps-Engine Documentation.
<?php
ini_set('display_errors','on');
require('GoogleAPI/autoload.php');
//require_once 'GoogleAPI/src/Google/Client.php';
//require_once 'Google/Service/MapsEngine.php';
$apiKey = "API Key";
$client = new Google_Client();
$client->setApplicationName("Google-PhpMapsEngineSample/1.0");
$client->setDeveloperKey($apiKey);
$service = new Google_Service_MapsEngine($client);
$optParams = array('maxResults' => 500, 'version' => 'published');
$results = $service->tables_features->listTablesFeatures("12421761926155747447-06672618218968397709", $optParams);
print_r($results);
?>
The only changes to the code example were the API Key, load the Google Autoloader and comment out the require_once directives.
The output I receive is:
Fatal error: Class 'Google_Service_MapsEngine_MapItem' not found in C:\Program Files (x86)\PHP\v5.4\includes\GoogleAPI\src\Google\Service\MapsEngine.php on line 4702
MapsEngine:4702 extends the Google_Service_MapsEngine_MapItem class. The Google_Service_MapsEngine_MapItem class extends the Google_Model class defined in Model.php file.
Hi I had the same problem.
There is a bug in the google-api-php-client/src/Google/Service/MapsEngine.php file. The class Google_Service_MapsEngine_MapFolder which exends the Google_Service_MapsEngine_MapItem is declared before the class Google_Service_MapsEngine_MapItem is declared.
I switch the order of the 2 classes in the MapsEngine.php file and that fixed the problem. This shows the correct order for the classes.
class Google_Service_MapsEngine_MapItem extends Google_Model
{
protected $internal_gapi_mappings = array(
);
public $type;
public function setType($type)
{
$this->type = $type;
}
public function getType()
{
return $this->type;
}
}
class Google_Service_MapsEngine_MapFolder extends Google_Service_MapsEngine_MapItem
{
protected $collection_key = 'defaultViewport';
protected $internal_gapi_mappings = array(
);
protected $contentsType = 'Google_Service_MapsEngine_MapItem';
protected $contentsDataType = 'array';
public $defaultViewport;
public $expandable;
public $key;
public $name;
public $visibility;
protected function gapiInit()
{
$this->type = 'folder';
}
public function setContents($contents)
{
$this->contents = $contents;
}
public function getContents()
{
return $this->contents;
}
public function setDefaultViewport($defaultViewport)
{
$this->defaultViewport = $defaultViewport;
}
public function getDefaultViewport()
{
return $this->defaultViewport;
}
public function setExpandable($expandable)
{
$this->expandable = $expandable;
}
public function getExpandable()
{
return $this->expandable;
}
public function setKey($key)
{
$this->key = $key;
}
public function getKey()
{
return $this->key;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function setVisibility($visibility)
{
$this->visibility = $visibility;
}
public function getVisibility()
{
return $this->visibility;
}
}
I am writting a console application with Symfony2 components, and I want to add distinct logging channels for my services, my commands and so on. The problem: to create a new channel requires to create a new instance of Monolog, and I don't really know how to handle this in a generic way, and without needing to pass the stream handler, a channel and the proper code to bind the one and the other inside all services.
I did the trick using debug_backtrace():
public function log($level, $message, array $context = array ())
{
$trace = array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3), 1);
$caller = $trace[0]['class'] !== __CLASS__ ? $trace[0]['class'] : $trace[1]['class'];
if (!array_key_exists($caller, $this->loggers))
{
$monolog = new Monolog($caller);
$monolog->pushHandler($this->stream);
$this->loggers[$caller] = $monolog;
}
$this->loggers[$caller]->log($level, $message, $context);
}
Whatever from where I call my logger, it creates a channel for each class that called it. Looks cool, but as soon as a logger is called tons of time, this is performance-killing.
So here is my question:
Do you know a better generic way to create one distinct monolog channel per class that have a logger property?
The above code packaged for testing:
composer.json
{
"require" : {
"monolog/monolog": "~1.11.0"
}
}
test.php
<?php
require('vendor/autoload.php');
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class Test
{
public function __construct($logger)
{
$logger->info("test!");
}
}
class Hello
{
public function __construct($logger)
{
$logger->log(Monolog\Logger::ALERT, "hello!");
}
}
class LeveragedLogger implements \Psr\Log\LoggerInterface
{
protected $loggers;
protected $stream;
public function __construct($file, $logLevel)
{
$this->loggers = array ();
$this->stream = new StreamHandler($file, $logLevel);
}
public function alert($message, array $context = array ())
{
$this->log(Logger::ALERT, $message, $context);
}
public function critical($message, array $context = array ())
{
$this->log(Logger::CRITICAL, $message, $context);
}
public function debug($message, array $context = array ())
{
$this->log(Logger::DEBUG, $message, $context);
}
public function emergency($message, array $context = array ())
{
$this->log(Logger::EMERGENCY, $message, $context);
}
public function error($message, array $context = array ())
{
$this->log(Logger::ERROR, $message, $context);
}
public function info($message, array $context = array ())
{
$this->log(Logger::INFO, $message, $context);
}
public function log($level, $message, array $context = array ())
{
$trace = array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3), 1);
$caller = $trace[0]['class'] !== __CLASS__ ? $trace[0]['class'] : $trace[1]['class'];
if (!array_key_exists($caller, $this->loggers))
{
$monolog = new Logger($caller);
$monolog->pushHandler($this->stream);
$this->loggers[$caller] = $monolog;
}
$this->loggers[$caller]->log($level, $message, $context);
}
public function notice($message, array $context = array ())
{
$this->log(Logger::NOTICE, $message, $context);
}
public function warning($message, array $context = array ())
{
$this->log(Logger::WARNING, $message, $context);
}
}
$logger = new LeveragedLogger('php://stdout', Logger::DEBUG);
new Test($logger);
new Hello($logger);
Usage
ninsuo:test3 alain$ php test.php
[2014-10-21 08:59:04] Test.INFO: test! [] []
[2014-10-21 08:59:04] Hello.ALERT: hello! [] []
What would you think about making the decision which logger should be used right before the consumers are created? This could be easily accomplished with some kind of DIC or maybe a factory.
<?php
require('vendor/autoload.php');
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Psr\Log\LoggerInterface;
use Monolog\Handler\HandlerInterface;
class Test
{
public function __construct(LoggerInterface $logger)
{
$logger->info("test!");
}
}
class Hello
{
public function __construct(LoggerInterface $logger)
{
$logger->log(Monolog\Logger::ALERT, "hello!");
}
}
class LeveragedLoggerFactory
{
protected $loggers;
protected $stream;
public function __construct(HandlerInterface $streamHandler)
{
$this->loggers = array();
$this->stream = $streamHandler;
}
public function factory($caller)
{
if (!array_key_exists($caller, $this->loggers)) {
$logger = new Logger($caller);
$logger->pushHandler($this->stream);
$this->loggers[$caller] = $logger;
}
return $this->loggers[$caller];
}
}
$loggerFactory = new LeveragedLoggerFactory(new StreamHandler('php://stdout', Logger::DEBUG));
new Test($loggerFactory->factory(Test::class));
new Hello($loggerFactory->factory(Hello::class));
I finally created a MonologContainer class that extends the standard Symfony2 container, and injects a Logger to LoggerAware services. Overloading the get() method of the service container, I can get the service's ID, and use it as a channel for the logger.
<?php
namespace Fuz\Framework\Core;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Monolog\Handler\HandlerInterface;
use Monolog\Logger;
use Psr\Log\LoggerAwareInterface;
class MonologContainer extends ContainerBuilder
{
protected $loggers = array ();
protected $handlers = array ();
protected $processors = array ();
public function __construct(ParameterBagInterface $parameterBag = null)
{
parent::__construct($parameterBag);
}
public function pushHandler(HandlerInterface $handler)
{
foreach (array_keys($this->loggers) as $key)
{
$this->loggers[$key]->pushHandler($handler);
}
array_unshift($this->handlers, $handler);
return $this;
}
public function popHandler()
{
if (count($this->handlers) > 0)
{
foreach (array_keys($this->loggers) as $key)
{
$this->loggers[$key]->popHandler();
}
array_shift($this->handlers);
}
return $this;
}
public function pushProcessor($callback)
{
foreach (array_keys($this->loggers) as $key)
{
$this->loggers[$key]->pushProcessor($callback);
}
array_unshift($this->processors, $callback);
return $this;
}
public function popProcessor()
{
if (count($this->processors) > 0)
{
foreach (array_keys($this->loggers) as $key)
{
$this->loggers[$key]->popProcessor();
}
array_shift($this->processors);
}
return $this;
}
public function getHandlers()
{
return $this->handlers;
}
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
{
$service = parent::get($id, $invalidBehavior);
return $this->setLogger($id, $service);
}
public function setLogger($id, $service)
{
if ($service instanceof LoggerAwareInterface)
{
if (!array_key_exists($id, $this->loggers))
{
$this->loggers[$id] = new Logger($id, $this->handlers, $this->processors);
}
$service->setLogger($this->loggers[$id]);
}
return $service;
}
}
Usage example:
test.php
#!/usr/bin/env php
<?php
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Fuz\Framework\Core\MonologContainer;
if (!include __DIR__ . '/vendor/autoload.php')
{
die('You must set up the project dependencies.');
}
$container = new MonologContainer();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.yml');
$handler = new StreamHandler(__DIR__ ."/test.log", Logger::WARNING);
$container->pushHandler($handler);
$container->get('my.service')->hello();
services.yml
parameters:
my.service.class: Fuz\Runner\MyService
services:
my.service:
class: %my.service.class%
MyService.php
<?php
namespace Fuz\Runner;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
class MyService implements LoggerAwareInterface
{
protected $logger;
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function hello()
{
$this->logger->alert("Hello, world!");
}
}
Demo
ninsuo:runner alain$ php test.php
ninsuo:runner alain$ cat test.log
[2014-11-06 08:18:55] my.service.ALERT: Hello, world! [] []
You can try this
<?php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\FirePHPHandler;
class Loggr{
private static $_logger;
public $_instance;
public $_channel;
private function __construct(){
if(!isset(self::$_logger))
self::$_logger = new Logger('Application Log');
}
// Create the logger
public function logError($error){
self::$_logger->pushHandler(new StreamHandler(LOG_PATH . 'application.'. $this->_channel . '.log', Logger::ERROR));
self::$_logger->addError($error);
}
public function logInfo($info){
self::$_logger->pushHandler(new StreamHandler(LOG_PATH . 'application.'. $this->_channel . '.log', Logger::INFO));
self::$_logger->addInfo($info);
}
public static function getInstance($channel) {
$_instance = new Loggr();
$_instance->_channel = strtolower($channel);
return $_instance;
}
}
and can be consumed as
class LeadReport extends Controller{
public function __construct(){
$this->logger = Loggr::getInstance('cron');
$this->logger->logError('Error generating leads');
}
}
I have an abstract base Controller class and all action controllers are derived from it.
Base Controller class at construction initializes View object. This View object is used by all action controllers. Each action controller have different dependencies (this is solved by using DI container).
The problem is that base Controller class also needs some dependencies (or parameters),
for example, path to view folder. And the question is - where and how to pass parameters to base Controller class?
$dic = new Dic();
// Register core objects: request, response, config, db, ...
class View
{
// Getters and setters
// Render method
}
abstract class Controller
{
private $view;
public function __construct()
{
$this->view = new View;
// FIXME: How / from where to get view path?
// $this->view->setPath();
}
public function getView()
{
return $this->view;
}
}
class Foo_Controller extends Controller
{
private $db;
public function __construct(Db $db)
{
$this->db = $db;
}
public function barAction()
{
$this->getView()->some_var = 'test';
}
}
require_once 'controllers/Foo_Controller.php';
// Creates object with dependencies which are required in __construct()
$ctrl = $dic->create('Foo_Controller');
$ctrl->barAction();
This is just a basic example. Why is the $view private? Is there a good reason?
class View {
protected $path;
protected $data = array();
function setPath($path = 'standard path') {
$this->path = $path;
}
function __set($key, $value) {
$this->data[$key] = $value;
}
function __get($key) {
if(array_key_exists($key, $this->data)) {
return $this->data[$key];
}
}
}
abstract class Controller {
private $view;
public function __construct($path)
{
$this->view = new View;
$this->view->setPath($path);
}
public function getView()
{
return $this->view;
}
}
class Foo_Controller extends Controller {
private $db;
public function __construct(Db $db, $path)
{
// call the parent constructor.
parent::__construct($path);
$this->db = $db;
}
public function barAction()
{
$this->getView()->some_var = 'test';
}
public function getAction() {
return $this->getView()->some_var;
}
}
class DB {
}
$con = new DB;
$ctrl = new Foo_Controller($con, 'main');
$ctrl->barAction();
print $ctrl->getAction();