Last active
July 19, 2018 16:12
-
-
Save jonathansanchez/10bd017aebb6f570a8e71cf8f0c58db8 to your computer and use it in GitHub Desktop.
Passing Request object to Infrastructure Service on Laravel 5.4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class InfrastructureServiceProvider extends ServiceProvider { | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
// | |
} | |
/** | |
* Register any application services. | |
* | |
* This service provider is a great spot to register your various container | |
* bindings with the application. As you can see, we are registering our | |
* "Registrar" implementation here. You can add your own bindings too! | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
$this->registerLaravelLogger(); | |
} | |
private function registerLaravelLogger() | |
{ | |
$this->app->bind( | |
'App\Infrastructure\Logging\Logger', | |
'App\Infrastructure\Logging\Laravel\LaravelLogger', function() { | |
$request = app(\Illuminate\Http\Request::class); | |
return app(LaravelLogger::class, [$request]); //or another var eg.:$request->foo | |
} | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Infrastructure\Logging\Laravel; | |
use App\Infrastructure\Logging\Logger; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Log; | |
final class LaravelLogger implements Logger | |
{ | |
/** @var Request */ | |
private $request; | |
private $sessionToken; | |
public function __construct(Request $request) //Here my Dependency Illuminate\Http\Request | |
{ | |
$this->request = $request; | |
$this->sessionToken = $request->cookie('SESSION_CUSTOM'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function emergency($message, array $context = []) | |
{ | |
Log::info('SESSION: ' . $this->sessionToken . ' | ' . $message, $context); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function alert($message, array $context = []) | |
{ | |
Log::alert('SESSION: ' . $this->sessionToken . ' | ' . $message, $context); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function critical($message, array $context = []) | |
{ | |
Log::critical('SESSION: ' . $this->sessionToken . ' | ' . $message, $context); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function error($message, array $context = []) | |
{ | |
Log::error('SESSION: ' . $this->sessionToken . ' | ' . $message, $context); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function warning($message, array $context = []) | |
{ | |
Log::warning('SESSION: ' . $this->sessionToken . ' | ' . $message, $context); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function notice($message, array $context = []) | |
{ | |
Log::notice('SESSION: ' . $this->sessionToken . ' | ' . $message, $context); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function info($message, array $context = []) | |
{ | |
Log::info('SESSION: ' . $this->sessionToken . ' | ' . $message, $context); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function debug($message, array $context = []) | |
{ | |
Log::debug('SESSION: ' . $this->sessionToken . ' | ' . $message, $context); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function log($level, $message, array $context = []) | |
{ | |
Log::log($level, 'SESSION: ' . $this->sessionToken . ' | ' . $message, $context); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Infrastructure\Logging; | |
/** | |
* Describes a logger instance. | |
*/ | |
interface Logger | |
{ | |
/** | |
* System is unusable. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return void | |
*/ | |
public function emergency($message, array $context = array()); | |
/** | |
* Action must be taken immediately. | |
* | |
* Example: Entire website down, database unavailable, etc. This should | |
* trigger the SMS alerts and wake you up. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return void | |
*/ | |
public function alert($message, array $context = array()); | |
/** | |
* Critical conditions. | |
* | |
* Example: Application component unavailable, unexpected exception. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return void | |
*/ | |
public function critical($message, array $context = array()); | |
/** | |
* Runtime errors that do not require immediate action but should typically | |
* be logged and monitored. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return void | |
*/ | |
public function error($message, array $context = array()); | |
/** | |
* Exceptional occurrences that are not errors. | |
* | |
* Example: Use of deprecated APIs, poor use of an API, undesirable things | |
* that are not necessarily wrong. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return void | |
*/ | |
public function warning($message, array $context = array()); | |
/** | |
* Normal but significant events. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return void | |
*/ | |
public function notice($message, array $context = array()); | |
/** | |
* Interesting events. | |
* | |
* Example: User logs in, SQL logs. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return void | |
*/ | |
public function info($message, array $context = array()); | |
/** | |
* Detailed debug information. | |
* | |
* @param string $message | |
* @param array $context | |
* | |
* @return void | |
*/ | |
public function debug($message, array $context = array()); | |
/** | |
* Logs with an arbitrary level. | |
* | |
* @param mixed $level | |
* @param string $message | |
* @param array $context | |
* | |
* @return void | |
*/ | |
public function log($level, $message, array $context = array()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment