Skip to content

Commit

Permalink
feat(core): basic code updates for slim4 migration and possible integ…
Browse files Browse the repository at this point in the history
…ration with php-di
  • Loading branch information
Awilum committed Jul 28, 2021
1 parent 4ac9d32 commit 25ec3cf
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 471 deletions.
15 changes: 8 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@
"atomastic/macroable": "^2.0.0",
"atomastic/csrf": "^1.0.1",

"slim/slim": "^3.12.3",
"slim/slim": "^4.8.1",

"filp/whoops": "^2.12.1",

"league/glide-slim": "^1.0.0",
"league/event": "^2.2.0",
"league/glide": "^1.7.0",

Expand All @@ -48,17 +47,18 @@
"monolog/monolog": "^2.2.0",
"cocur/slugify": "^4.0.0",
"ramsey/uuid": "^4.1.1",
"symfony/yaml": "^5.2.9",
"symfony/yaml": "^5.3.4",
"symfony/finder": "^5.2.9",

"bnf/slim3-psr15": "^1.1.1",

"league/commonmark": "^1.5.8",

"thunderer/shortcode": "^0.7.4",

"composer/semver": "^3.2.5",
"guzzlehttp/guzzle": "7.3.0"
"slim/psr7": "^1.4",
"php-di/php-di": "^6.3",
"php-di/slim-bridge": "^3.1",
"pimple/pimple": "^3.4"
},
"suggest": {
"ext-zend-opcache": "Recommended for better performance",
Expand All @@ -73,7 +73,8 @@
"files": [
"src/flextype/Support/Helpers/FindHelper.php",
"src/flextype/Support/Helpers/FilterHelper.php",
"src/flextype/Foundation/Helpers/FlextypeHelper.php"
"src/flextype/Foundation/Helpers/FlextypeHelper.php",
"src/flextype/Foundation/Helpers/ActionsHelper.php"
]
},
"require-dev": {
Expand Down
191 changes: 191 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,191 @@
use const DIRECTORY_SEPARATOR;
use const PHP_VERSION;


class Debug
{
/**
* Time
*
* @var array
*/
protected static $time = [];

/**
* Memory
*
* @var array
*/
protected static $memory = [];

/**
* Save current time for current point
*
* Debug::elapsedTimeSetPoint('point_name');
*
* @param string $point_name Point name
*/
public static function elapsedTimeSetPoint(string $point_name) : void
{
Debug::$time[$point_name] = microtime(true);
}

/**
* Get elapsed time for current point
*
* echo Debug::elapsedTime('point_name');
*
* @param string $point_name Point name
* @return string
*/
public static function elapsedTime(string $point_name) : string
{
if (isset(Debug::$time[$point_name])) return sprintf("%01.4f", microtime(true) - Debug::$time[$point_name]);
}

/**
* Save current memory for current point
*
* Debug::memoryUsageSetPoint('point_name');
*
* @param string $point_name Point name
*/
public static function memoryUsageSetPoint(string $point_name) : void
{
Debug::$memory[$point_name] = memory_get_usage();
}

/**
* Get memory usage for current point
*
* echo Debug::memoryUsage('point_name');
*
* @param string $point_name Point name
* @return string
*/
public static function memoryUsage(string $point_name) : string
{
if (isset(Debug::$memory[$point_name])) {
$unit = array('B', 'KB', 'MB', 'GB', 'TiB', 'PiB');
$size = memory_get_usage() - Debug::$memory[$point_name];
$memory_usage = @round($size/pow(1024, ($i=floor(log($size, 1024)))), 2).' '.$unit[($i < 0 ? 0 : $i)];
return $memory_usage;
}
}

/**
* Print the variable $data and exit if exit = true
*
* Debug::dump($data);
*
* @param mixed $data Data
* @param bool $exit Exit
*/
public static function dump($data, bool $exit = false) : void
{
echo "<pre>dump \n---------------------- \n\n" . print_r($data, true) . "\n----------------------</pre>";
if ($exit) exit;
}

/**
* Prints a list of all currently declared classes.
*
* Debug::classes();
*
* @access public
* @return string
*/
public static function classes()
{
return Debug::dump(get_declared_classes());
}

/**
* Prints a list of all currently declared interfaces.
*
* Debug::interfaces();
*
* @access public
* @return string
*/
public static function interfaces()
{
return Debug::dump(get_declared_interfaces());
}

/**
* Prints a list of all currently included (or required) files.
*
* Debug::includes();
*
* @access public
* @return string
*/
public static function includes()
{
return Debug::dump(get_included_files());
}

/**
* Prints a list of all currently declared functions.
*
* Debug::functions();
*
* @access public
* @return string
*/
public static function functions()
{
return Debug::dump(get_defined_functions());
}

/**
* Prints a list of all currently declared constants.
*
* Debug::constants();
*
* @access public
* @return string
*/
public static function constants()
{
return Debug::dump(get_defined_constants());
}

/**
* Prints a list of all currently loaded PHP extensions.
*
* Debug::extensions();
*
* @access public
* @return string
*/
public static function extensions()
{
return Debug::dump(get_loaded_extensions());
}

/**
* Prints a list of the configuration settings read from php.ini
*
* Debug::phpini();
*
* @access public
* @return string
*/
public static function phpini()
{
if (!is_readable(get_cfg_var('cfg_file_path'))) {
return false;
}

return Debug::dump(parse_ini_file(get_cfg_var('cfg_file_path'), true));
}
}

Debug::elapsedTimeSetPoint('flextype');
Debug::memoryUsageSetPoint('flextype');

/**
* Define the application minimum supported PHP version.
*/
Expand Down Expand Up @@ -65,3 +250,9 @@
* the responses back to the browser and delight our users.
*/
include __DIR__ . '/src/flextype/flextype.php';


echo "<div style='position:absolute; bottom: 10px; left: 10px;'>";
echo "Time: " . Debug::elapsedTime('flextype');
echo " Memory: " . Debug::memoryUsage('flextype');
echo "</div>";
51 changes: 30 additions & 21 deletions src/flextype/Foundation/Flextype.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,26 @@
namespace Flextype\Foundation;

use Exception;
use Psr\Container\ContainerInterface;
use DI\Bridge\Slim\Bridge;
use DI\Container;
use Slim\App;
use Slim\Http\Environment;
use Slim\Http\Uri;
use Slim\Middleware\ContentLengthMiddleware;
use Slim\Middleware\OutputBufferingMiddleware;
use Slim\Middleware\RoutingMiddleware;
use Slim\Psr7\Factory\StreamFactory;
use Atomastic\Csrf\Csrf;
use Atomastic\Registry\Registry;
use Atomastic\Session\Session;
use Cocur\Slugify\Slugify;
use DateTimeZone;
use Flextype\Foundation\Actions;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Container\ContainerInterface;

use function is_null;

final class Flextype extends App
final class Flextype
{
/**
* Flextype version
Expand All @@ -33,6 +45,9 @@ final class Flextype extends App
*/
private static array $instances = [];

private App $app;
private Container $container;

/**
* Flextype should not be cloneable.
*/
Expand All @@ -51,34 +66,28 @@ public function __wakeup(): void

/**
* Flextype construct
*
* @param ContainerInterface|array $container
*/
protected function __construct($container = [])
protected function __construct(ContainerInterface $container = null)
{
parent::__construct($container);
$this->app = Bridge::create($container);
$this->container = $this->app->getContainer();
}

/**
* Get/Set Dependency Injection Container.
*
* @param string|null $name DI Container name.
*/
public function container(?string $name = null)
public function app()
{
if (is_null($name)) {
return self::getInstance()->getContainer();
}
return $this->app;
}

return self::getInstance()->getContainer()[$name];
public function container()
{
return $this->container;
}


/**
* Returns Flextype Instance
*
* @param ContainerInterface|array $container Container.
*/
public static function getInstance($container = []): Flextype
public static function getInstance(ContainerInterface $container = null): Flextype
{
$cls = static::class;
if (! isset(self::$instances[$cls])) {
Expand Down
11 changes: 3 additions & 8 deletions src/flextype/Foundation/Helpers/FlextypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@

if (! function_exists('flextype')) {
/**
* Get the available Flextype Application instance
* or try to get Dependency Injection Container if $container is not null.
* Get the available Flextype instance.
*/
function flextype($containerName = null, $container = [])
function flextype($container = null)
{
if (is_null($containerName)) {
return Flextype::getInstance($container);
}

return Flextype::getInstance($container)->container($containerName);
return Flextype::getInstance($container);
}
}

0 comments on commit 25ec3cf

Please sign in to comment.