PHP 7+ Logging class Magic Constants - php

I just wrote a little "logging" class and want to ask a question about the usage of this class, how i could make it easier to use.
For example:
$log = new Log();
$log->Error("You have an error!", __FILE__, __CLASS__, __FUNCTION__, __LINE__);
This is how i write errors to a log file at the moment, but it seems to complex?! Is there a way to get the "MAGIC CONSTANTS" inside the logging class from the "calling" php ?
Here is the class code (any other tips are welcome too):
<?php
class Log
{
private $path;
public function __construct()
{
$config = new Config(); // init. from autoloader
$path = $config->app_log_dir;
if (!is_dir($path) && !is_writable($path))
{
error_log('[ERROR] [Log::__Construct()] -> ' . $path . ' does not exist or is not writeable!',0);
header("HTTP/1.0 500 Internal Server Error");
exit();
}
$this->path = $path;
}
public function Error($message, $file, $class = '', $function = '', $line)
{
$array_data = array($message, $file, $class, $function, $line);
$this->write('ERROR', $array_data);
}
public function TestError($message, $file = __FILE__, $class = __CLASS__, $function = __FUNCTION__, $line = __LINE__)
{
$array_data = array($message, $file, $class, $function, $line);
$this->write('TESTERROR', $array_data);
}
private function write($error_type, $array_data)
{
$date = date("Y-m-d H:i:s");
$dateFile = date("Y-m-d");
$message = "[{$date}] [{$error_type}] [{$array_data[1]}->{$array_data[2]}::{$array_data[3]}:{$array_data[4]}] $array_data[0]".PHP_EOL;
try
{
file_put_contents($this->path.'/'.$dateFile.'.log', $message, FILE_APPEND);
}
catch (Exception $e)
{
error_log('[ERROR] [Log::write()] -> ' . $e, 0);
header("HTTP/1.0 500 Internal Server Error");
exit();
}
}
}

Check out debug_backtrace().
So you can do :
public function Error($message, $debug)
{
$array_data = array($message, $debug);
$this->write('ERROR', $array_data);
}
$log->Error("Oh noo!!", print_r(debug_backtrace(),true) );
A backtrace contains a potentially huge amount of data so I'm not going to example a full one here, but it can contain:
function ; The current function name. See also __FUNCTION__.
line ; The current line number. See also __LINE__.
file ; The current file name. See also __FILE__.
class ; The current class name. See also __CLASS__.
object ; The current object.
type ; The current call type. If a method call, "->" is returned. If a static method call, "::" is returned. If a function
call, nothing is returned.
args ; If inside a function, this lists the functions arguments. If inside an included file, this lists the included file name(s).
debug_backtrace() is a goldmine of information to debug PHP. This covers everything you ask for in your question.

Pass those constants as function parameters instead:
public function Error(
$message,
$file = __FILE__,
$class = __CLASS__,
$function = __FUNCTION__,
$line = __LINE__,
) {
// ...
}
and call as always:
$log->Error('xxx');
If I may, your code smells, why not use PSR-3 compatible logger like Monolog? Or even handle errors like a pro with Whoops.

Related

PHP: opposite of extends (for parent class)

I'm aware of using extends but i am wondering what's the best practice for doing the opposite:
I'm having a "parent" class called c_film and 2 child classes called c_wikipedia and c_imdb - they need to access the general settings ($aOptions) and functions / error handler from c_film.
here's a simplified version:
$aOptions = array(
"wikidata_id" => "Q63985561"; // the movie is: tenet
"verbose_output" => true,
"logging" => true
);
$o = new c_film( $aOptions );
$aData = $o->load_film(); // scrape wikipedia, scrape imdb, merge data into array
these are the requirements:
c_film has functions for scraping/parsing/error handling for all child classes/logging/misc which can be used from both child classes
c_wikipedia and c_imdb can access options / functions from c_film and trigger errors
here's my current solution (simplified):
class c_film
{
function __construct( $aOptions )
{
$this->aOptions = $aOptions;
}
function load_film()
{
$o = new c_wikipedia( $this );
$this->aWikipedia = $o->get_data();
$o = new c_imdb( $this );
$this->aImdb = $o->get_data();
$aData = $this->get_merged_data();
}
private function get_merged_data()
{
// process data / merge into one array
$aResult = array_merge( $this->aWikipedia, $this->aImdb );
result $aResult;
}
function scrape($url)
{
// scrape data / handle 404 / log errors/ html parsing
// [code here]
return $html;
}
function log($msg, $class, $function)
{
// log to file
}
function error( Throwable $t )
{
// log error into file
}
}
class c_wikipedia
{
function __construct( $oFilm ) // parent class object c_film
{
$this->oFilm = $oFilm;
}
function get_data()
{
try {
// scrape data from wikipedia
$aData = $this->get_data();
$url = $this->get_url_from_wikidata_id();
$html = $oFilm->scrape($url);
} catch(Throwable $t ){
//
$oFilm->error( $t );
}
}
private function get_data()
{
$oFilm = $this->oFilm;
$aOptions = $oFilm->aOptions;
$wikidata_id = $aOptions['wikidata_id'];
$bLog = $oFilm->aOptions['logging'];
$output = $oFilm->aOptions['verbose_output'];
// .. load + parse data
$url = // determine url
$msg = "loading data for " . $wikidata_id;
if($bLog) $oFilm->log($msg, get_class(), __FUNCTION__ ); // log to file including class name and function name
if($output) echo $msg;
$html = $oFilm->scrape($url);
return $aData;
}
}
So - is passing the c_film object to the child classes the best practice or is there a more elegant method?

PHP - ZF2 - render template from string variable

i have problem with rendering template in ZF2, where template is in string in variable. There is simple example:
$template = "<div>easy</div>";
$view = new \Zend\View\Model\ViewModel();
$view->setTemplate($template);
$renderer = new \Zend\View\Renderer\PhpRenderer();
$html = $renderer->render($view);
This code fail on rendering, the renderer think that the template is a path to file. And iam reallz not sure how to tell rendere its a string.
Thx for your time and respond.
You have to extend the PhpRenderer class and override the render method, in such a way that will use the string in the $template as the actual template:
class MyPhpRenderer extends PhpRenderer {
public function render($nameOrModel, $values = null)
{
if ($nameOrModel instanceof Model) {
$model = $nameOrModel;
$nameOrModel = $model->getTemplate();
if (empty($nameOrModel)) {
throw new Exception\DomainException(sprintf(
'%s: received View Model argument, but template is empty',
__METHOD__
));
}
$options = $model->getOptions();
foreach ($options as $setting => $value) {
$method = 'set' . $setting;
if (method_exists($this, $method)) {
$this->$method($value);
}
unset($method, $setting, $value);
}
unset($options);
// Give view model awareness via ViewModel helper
$helper = $this->plugin('view_model');
$helper->setCurrent($model);
$values = $model->getVariables();
unset($model);
}
// find the script file name using the parent private method
$this->addTemplate($nameOrModel);
unset($nameOrModel); // remove $name from local scope
$this->__varsCache[] = $this->vars();
if (null !== $values) {
$this->setVars($values);
}
unset($values);
// extract all assigned vars (pre-escaped), but not 'this'.
// assigns to a double-underscored variable, to prevent naming collisions
$__vars = $this->vars()->getArrayCopy();
if (array_key_exists('this', $__vars)) {
unset($__vars['this']);
}
extract($__vars);
unset($__vars); // remove $__vars from local scope
while ($this->__template = array_pop($this->__templates)) {
$this->__file = $this->resolver($this->__template);
try {
if (!$this->__file) {
$this->__content = $this->__template; // this line does what you need
}else{
ob_start();
$includeReturn = include $this->__file;
$this->__content = ob_get_clean();
}
} catch (\Exception $ex) {
ob_end_clean();
throw $ex;
}
if ($includeReturn === false && empty($this->__content)) {
throw new Exception\UnexpectedValueException(sprintf(
'%s: Unable to render template "%s"; file include failed',
__METHOD__,
$this->__file
));
}
}
$this->setVars(array_pop($this->__varsCache));
if ($this->__filterChain instanceof FilterChain) {
return $this->__filterChain->filter($this->__content); // filter output
}
return $this->__content;
}
}
and then you code should look like:
$template = "<div>easy</div>";
$view = new \Zend\View\Model\ViewModel();
$view->setTemplate($template);
$renderer = new MyPhpRenderer();
$html = $renderer->render($view);
Try by replacing '\' with _ underscore as Zend_View_Renderer_PhpRenderer

Testing a class that uses the storage facade?

I am trying to unit test a function that calls another function that uses the storage facade. When I run the function in phpunit, it returns the error, "Error: Class 'Storage' not found." How can I run the function either without an error or defaulting the second function to return true?
Part of the code I am trying to test.
public function newMaterial($fileType, $groupId, $categoryId, $fileName, $file, $open, $close) {
$material = new Mat;
if (!$this->save($fileType, $file, $material)) {
return false;
}
}
This is the section of code that is causing the error.
protected function save($fileType, $file, Mat $material) {
//generate random name
do {
$key = str_random(32);
//check if exist
} while (Storage::exists($path . $key . '.' . $ext));
return true;
}
This is not all of my code, just the parts that are causing an issue.
In my test I am only calling the newMaterial Function and it returns the error.

Why is include('../myscript.php') causing an error?

I've been trying to debug my PHP script and I've narrowed down the problem to the line
include "../classes.php";
at the top of my file team_manager.php which is where you see it below.
themes
my_theme
js
management
team_manager.php
project_manager.php
classes.php
footer.php
functions.php
Am I not doing the path correctly? Or could it be something wrong with the contents of classes.php? If it could be a problem with the file being included, below is the file, and let me know if anything immediately stands out as wrong.
<?php
final class MySqlInfo
{
const DBNAME = 'somedb';
const USER = 'someuser';
const PSSWD = 'somepassword';
const TEAMTABLENAME = 'sometablename';
public function getUser ( )
{
return self::USER;
}
public function getPassword ( )
{
return self::PSSWD;
}
}
final class MethodResult
{
public $succeeded;
public $message;
public MethodResult ( $succeededInit = NULL, $messageInit = NULL )
{
this->$succeeded = $succeededInit;
this->$message = $messageInit;
}
}
final class MySite
{
const ROOTURL = 'http://asite.com/subsite';
function getRootUrl()
{
return self::ROOTURL;
}
}
final class TeamManager
{
private $dbcon;
public function TeamManager ( )
{
$dbcon = mysqli_connect('localhost', MySqlInfo.getUser(), MySqlInfo::getPassword());
$dbcon->select_db(MySqlInfo::DBNAME);
// need to add error handling here
}
final public class TeamMember
{
public $name; // team member name
public $title; // team member title
public $bio; // team member bio
public $sord; // team member sort order
public $picfn; // team member profile picture file name
}
public function addMember ( TeamMember $M )
{
if ($this->$dbcon->connect_error)
{
return new MethodResult(false, 'Not connected to database');
}
$q = "INSERT INTO " . MySqlInfo::TEAMTABLENAME . " (" . implode( ',' array($M->name, $M->title, $M->bio, $M->sord, $M->picfn) ) . ") VALUES ('" . implode('\',\'', array($_POST['fullname'], $_POST['title'], $_POST['bio'], $_POST['sord'], $targetFileName)) . "')";
// ^ query for inserting member M to the database
if (!mysqli_query(this->$dbcon, $q))
{
return new MethodResult(false, 'Query to insert new team member failed');
}
return new MethodResult(true, 'Successfully added new member' . $M->name);
}
}
?>
include() might cause an error in case it can't find a file to be included. It's that simple. So you have to make sure that file exists before you include it. Also, never use relative paths like ../script.php, as they introduce a number of issues. And one major issue is that, some hosting providers don't allow relative paths due to security reasons.
So to make sure the file can be included, simply do the check for its existence:
<?php
// dirname(__FILE__) returns an absolute path of the current script
// which is being executed
$file = dirname(__FILE__) . '/script.php';
if (is_file($file)) {
include($file);
} else {
echo 'File does not exist';
}
Also, I see that you write code as an old-school. You might want to take a look at PSR-FIG standards.

Error When Calling Function

I searched forever trying to find an answer, but was ultimately stumped. I've been writing code to allow multiple bots to connect to a chat box. I wrote all the main code and checked it over to make sure it was all okay. Then when I got to calling the function needed to make it work, it gave me an error saying:
Notice: Undefined variable: ip in C:\wamp\www\BotRaid.php on line 40
And also an error saying:
Fatal Error: Cannot access empty property in C:\wamp\www\BotRaid.php
on line 40
( Also a screenshot here: http://prntscr.com/ckz55 )
<?php
date_default_timezone_set("UCT");
declare(ticks=1);
set_time_limit(0);
class BotRaid
{
public $ip="174.36.242.26";
public $port=10038;
public $soc = null;
public $packet = array();
##############################
# You can edit below this #
##############################
public $roomid="155470742";
public $userid = "606657406";
public $k = "2485599605";
public $name="";
public $avatar=;
public $homepage="";
##############################
# Stop editing #
##############################
public function retry()
{
$this->connect($this->$ip,$this->$port); //Line 40, where I'm getting the error now.
$this->join($this->$roomid);
while($this->read()!="DIED");
}
public function connect($ip, $port)
{
if($this->$soc!=null) socket_close($this->$soc);
$soc = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if(!$this->$soc)$this->port();
if(!socket_connect($this->$soc,$this->$ip,$this->$port))$this->port();
}
public function port()
{
$this->$port++;
if($this->$port>10038) $this->$port=10038;
$this->retry();
}
public function join($roomid)
{
$this->send('<y m="1" />');
$this->read();
$this->send('<j2 q="1" y="'.$this->$packet['y']['i'].'" k="'.$this->$k.'" k3="0" z="12" p="0" c"'.$roomid.'" f="0" u="'.$this->$userid.'" d0="0" n="'.$this->$name.'" a="'.$this->$avatar.'" h="'.$this->$homepage.'" v="0" />');
$this->port();
$this->$roomid;
}
public function send($msg)
{
echo "\n Successfully connected.";
socket_write($this->$soc, $this->$msg."\0", strlen($this->$msg)+1);
}
public function read($parse=true)
{
$res = rtrim(socket_read($this->$soc, 4096));
echo "\nSuccessfully connected.";
if(strpos(strtolower($res), "Failed"))$this->port();
if(!$res) return "DIED";
$this->lastPacket = $res;
if($res{strlen($res)-1}!='>') {$res.=$this->read(false);}
if($parse)$this->parse($res);
return $res;
}
public function parse($packer)
{
$packet=str_replace('+','#più#',str_replace(' ="',' #=#"',$packet));
if(substr_count($packet,'>')>1) $packet = explode('/>',$packet);
foreach((Array)$packet as $p) {
$p = trim($p);
if(strlen($p)<5) return;
$type = trim(strtolower(substr($p,1,strpos($p.' ',' '))));
$p = trim(str_replace("<$type",'',str_replace('/>','',$p)));
parse_str(str_replace('"','',str_replace('" ','&',str_replace('="','=',str_replace('&','__38',$p)))),$this->packet[$type]);
foreach($this->packet[$type] as $k=>$v) {
$this->packet[$type][$k] = str_replace('#più#','+',str_replace('#=#','=',str_replace('__38','&',$v)));
}
}
}
}
$bot = new BotRaid; //This is where I had the error originally
$bot->retry();
?>
Line 40 is below the "Stop Editing" line. Anyone have any suggestions? Or perhaps need me to clear some things up?
You are accessing the properties of the class incorrectly.
The line:
$this->connect($this->$ip,$this->$port);
Should be:
$this->connect($this->ip, $this->port);
Since there was no local variable called $ip, your expression was evaluating to $this-> when trying to access the property since PHP lets you access properties and functions using variables.
For example, this would work:
$ip = 'ip';
$theIp = $this->$ip; // evaluates to $this->ip
// or a function call
$method = 'someFunction';
$value = $this->$method(); // evaluates to $this->someFunction();
You will have to change all the occurrences of $this->$foo with $this->foo since you used that notation throughout the class.
As noted in the comment by #Aatch, see the docs on variable variables for further explanation. But that is what you were running into accidentally.

Categories