Skip to content

Commit

Permalink
Fix an error, added methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rezen committed Oct 29, 2020
1 parent 771b980 commit 64f78af
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 12 deletions.
28 changes: 27 additions & 1 deletion example.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,31 @@

require "src/function.php";

\Becho\Becho::getInstance()
->useDefaultOptions(['color' => BECHO_YELLOW]);


$GLOBALS['BECHO_ECHOER'] = new class extends \Becho\StandardEchoer
{
function echo($text, $options=[])
{
echo $text;
}
};

// Basic example
becho("Some colored text", ['color' => BECHO_YELLOW]);
becho("What color do you think the text will be?\n", ['color' => BECHO_GREEN]);

// Override the echoer with a global
$GLOBALS['BECHO_ECHOER'] = new class extends \Becho\StandardEchoer
{
function echo($text, $options=[])
{
parent::echo(__FILE__ . $text, $options);
}
};

?>
<input type="text" value="<?php becho('">\'Trying to break out"', BECHO_ESC_ATTR); ?>" />


59 changes: 50 additions & 9 deletions src/Becho/Becho.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,39 @@ class Becho
{
private static $instance = null;

private $driver = "vanilla";
private $driver;

private $echoers = [];

private $defaultEchoer = VanillaEchoer::class;
private $options = [];

private $ctx;

private $defaultEchoer = StandardEchoer::class;


function getEchoer(): Echoer
{
$globals = $this->getGlobals();
// Make it super easy to override an echoer
if (isset($globals['BECHO_ECHOER'])) {
return $globals['BECHO_ECHOER'];
// Make it super easy to override an echoer
if (isset($globals['BECHO_ECHOER']) && is_object($globals['BECHO_ECHOER'])) {
$interfaces = class_implements($globals['BECHO_ECHOER']);

// Verify the BECHO_ECHOER implements the interface
if($interfaces && in_array(Echoer::class, $interfaces)) {
return $globals['BECHO_ECHOER'];
}
}
$driver = $this->getDriver();
return $this->echoers[$driver] ?? new $this->defaultEchoer;
}

/**
* Get name of echoer being used
*/
function getDriver(): string
{
$globals = $this->getGlobals();
return isset($globals['BECHO_DRIVER']) ? $globals['BECHO_DRIVER'] : $this->driver;
}

Expand All @@ -35,23 +49,48 @@ function getGlobals()
return $GLOBALS;
}

function useDriver(string $name): Becho
/**
* Switch to using a different driver
* @param string $name
*/
function useEchoer(string $name): Becho
{
$this->driver = $name;
return $this;
}


/**
* Add an Echoer with a given name
* @param string $name
* @param Echoer $echoer
*/
function addEchoer(string $name, Echoer $echoer): Becho
{
$this->echoers[$name] = $echoer;
return this;
}

function echo($text, $ctx=\BECHO_RAW, $options=[])
function useDefaultOptions(array $options=[]): Becho
{
$this->options = $options;
return $this;
}

function useDefaultContext($ctx)
{
$this->ctx = $ctx;
}

function echo($text, $ctx=null, $options=[])
{
$options = is_array($ctx) ? $ctx : $options;
$ctx = is_array($ctx) ? \BECHO_RAW : $ctx;
$ctx = php_sapi_name() === 'cli' && $ctx === \BECHO_RAW ? \BECHO_CLI : $ctx;
$options = array_merge($this->options, $options);
$ctx = is_array($ctx) ? $this->ctx : $ctx;
$ctx = php_sapi_name() === 'cli' && is_null($ctx) ? \BECHO_CLI : $ctx;
$ctx = is_null($ctx) ? $this->ctx : $ctx;

$options['$ctx'] = $ctx;

$echoer = $this->getEchoer();
if ($ctx === \BECHO_ESC_ATTR) {
Expand All @@ -60,13 +99,15 @@ function echo($text, $ctx=\BECHO_RAW, $options=[])
$text = $echoer->escapeHtml($text);
}


if ($ctx === \BECHO_CLI) {
$text = $echoer->inCliContext($text, $options);
}

$echoer->echo($text, $options);
}


public static function getInstance()
{
if (self::$instance == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Becho;

class VanillaEchoer implements Echoer
class StandardEchoer implements Echoer
{
function echo($text, $options=[])
{
Expand Down
2 changes: 1 addition & 1 deletion src/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function ensureBechoClasses()
{
if (!class_exists("\\Becho\\Becho")) {
require 'Becho/Echoer.php';
require 'Becho/VanillaEchoer.php';
require 'Becho/StandardEchoer.php';
require 'Becho/Becho.php';
}
}
Expand Down

0 comments on commit 64f78af

Please sign in to comment.