XBackBone/app/Controllers/Controller.php

71 lines
1.3 KiB
PHP
Raw Normal View History

2018-04-28 12:20:07 +00:00
<?php
namespace App\Controllers;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
2018-11-11 16:02:50 +00:00
use Slim\Container;
use Slim\Http\Request;
use Slim\Http\Response;
2018-04-28 12:20:07 +00:00
abstract class Controller
{
2018-11-11 16:02:50 +00:00
/** @var Container */
protected $container;
2018-11-11 16:02:50 +00:00
public function __construct(Container $container)
{
$this->container = $container;
2018-04-28 12:20:07 +00:00
}
/**
2018-11-11 16:02:50 +00:00
* @param $name
* @return mixed|null
* @throws \Interop\Container\Exception\ContainerException
2018-04-28 12:20:07 +00:00
*/
2018-11-11 16:02:50 +00:00
public function __get($name)
2018-04-28 12:20:07 +00:00
{
2018-11-11 16:02:50 +00:00
if ($this->container->has($name)) {
return $this->container->get($name);
2018-04-28 12:20:07 +00:00
}
2018-11-11 16:02:50 +00:00
return null;
2018-04-28 12:20:07 +00:00
}
2018-10-13 16:25:48 +00:00
/**
* Generate a human readable file size
* @param $size
* @param int $precision
* @return string
*/
2018-04-28 12:20:07 +00:00
protected function humanFilesize($size, $precision = 2): string
{
for ($i = 0; ($size / 1024) > 0.9; $i++, $size /= 1024) {
}
return round($size, $precision) . ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'][$i];
}
2018-10-13 16:25:48 +00:00
/**
* Get a filesystem instance
* @return Filesystem
*/
2018-04-28 12:20:07 +00:00
protected function getStorage(): Filesystem
{
2018-11-11 16:02:50 +00:00
return new Filesystem(new Local($this->settings['storage_dir']));
2018-04-28 12:20:07 +00:00
}
2018-11-11 19:20:38 +00:00
/**
* @param $path
*/
public function removeDirectory($path)
{
$files = glob($path . '/*');
foreach ($files as $file) {
is_dir($file) ? $this->removeDirectory($file) : unlink($file);
}
rmdir($path);
return;
}
2018-04-28 12:20:07 +00:00
}