Commit 2bf12619 authored by Adam Brown's avatar Adam Brown

v1.2.0

parent 7ca564b1
......@@ -30,3 +30,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
-
## [1.2.0] - 2018-07-12
### Added
### Changed
- Refactored to stop using the facade
- Can now call statically
- Made most of the traits private and static. Will move some to public again soon
### Removed
-
......@@ -4,7 +4,11 @@
"keywords": [
"techendeavors",
"laravel",
"fileinfo"
"fileinfo",
"hashing",
"size",
"permissions",
"ownership"
],
"homepage": "https://techendeavors.com/packages",
"license": "MIT",
......@@ -36,24 +40,22 @@
"phpunit/phpunit": "^7.0"
},
"suggest": {
"nesbot/carbon": "1.25.*"
"nesbot/carbon": "A simple API extension for DateTime"
},
"autoload": {
"psr-4": {
"Techendeavors\\FileInfo\\": "src"
},
"files": [
"lib/helpers.php"
]
}
},
"autoload-dev": {
"psr-4": {
"Techendeavors\\FileInfo\\Tests\\": "tests"
},
"files": [
"lib/helpers.php"
]
}
},
"support" : {
"source" : "https://gitlab.com/techendeavors/fileinfo",
"issues" : "https://gitlab.com/techendeavors/fileinfo/issues"
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
......@@ -67,7 +69,7 @@
"Techendeavors\\FileInfo\\FileInfoServiceProvider"
],
"aliases": {
"FileInfo": "Techendeavors\\FileInfo\\Facades\\FileInfo"
"FileInfo": "Techendeavors\\FileInfo\\FileInfo"
}
}
}
......
<?php
namespace Techendeavors\FileInfo\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @see \Spatie\Skeleton\SkeletonClass
*/
class FileInfo extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'fileinfo';
}
}
......@@ -23,10 +23,14 @@ class FileInfo
protected $filename;
const DEFAULT_HASH_MAX_LENGTH = (1024 * 1024 * 20);
const DEFAULT_HASH_ALGOS = ['crc32','crc32b','md5','ripemd256','sha1','sha256','sha3-256','sha3-384','sha3-512','sha384','sha512'];
/**
* Create a new instance.
*/
public function __construct(string $filename)
public function __construct(string $filename = null)
{
$this->filename = $filename;
}
......@@ -37,16 +41,16 @@ class FileInfo
* @param string $phrase Phrase to return
* @return string Returns the phrase passed in
*/
public function all()
public static function show($filename)
{
return collect([
"query" => $this->filename,
"tests" => self::getTestResults(),
"permissions" => self::getPermissions(),
"info" => self::getInfo(),
"size" => self::getSize(),
"hashes" => self::getHashes(),
"events" => self::getEvents()
"query" => $filename,
"tests" => static::getTestResults($filename),
"permissions" => static::getPermissions($filename),
"info" => static::getInfo($filename),
"size" => static::getSize($filename),
"hashes" => static::getHashes($filename),
"events" => static::getEvents($filename)
]);
}
}
......@@ -2,7 +2,6 @@
namespace Techendeavors\FileInfo;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;
class FileInfoServiceProvider extends ServiceProvider
......@@ -19,17 +18,9 @@ class FileInfoServiceProvider extends ServiceProvider
*/
public function boot()
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/config.php' => config_path('fileinfo.php'),
], 'config');
// $this->loadViewsFrom(__DIR__.'/../resources/views', 'app-path');
// $this->publishes([
// __DIR__.'/../resources/views' => base_path('resources/views/vendor/techendeavors/app-path'),
// ], 'views');
}
$this->publishes([
__DIR__.'/../config/fileinfo.php' => config_path('fileinfo.php'),
], 'config');
}
/**
......@@ -37,26 +28,9 @@ class FileInfoServiceProvider extends ServiceProvider
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'fileinfo');
$this->app->booting(function() {
$loader = AliasLoader::getInstance();
$loader->alias('FileInfo', FileInfo::class);
});
$this->app->bind('fileinfo', function() {
return new FileInfo;
});
$this->mergeConfigFrom(
__DIR__.'/../config/fileinfo.php', 'fileinfo'
);
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['fileinfo'];
}
}
......@@ -4,74 +4,74 @@ namespace Techendeavors\FileInfo\Traits;
trait Checks
{
public function isThere()
protected static function isThere($filename)
{
return (bool) (file_exists($this->filename));
return (bool) (file_exists($filename));
}
public function isNotThere()
protected static function isNotThere($filename)
{
return (bool) (! self::isThere());
return (bool) (! static::isThere($filename));
}
public function isReadable()
protected static function isReadable($filename)
{
return (bool) (is_readable($this->filename));
return (bool) (is_readable($filename));
}
public function isNotReadable()
protected static function isNotReadable($filename)
{
return (bool) (! self::isReadable());
return (bool) (! static::isReadable($filename));
}
public function isWritable()
protected static function isWritable($filename)
{
return (bool) (is_writable($this->filename));
return (bool) (is_writable($filename));
}
public function isNotWritable()
protected static function isNotWritable($filename)
{
return (bool) (! self::isWritable());
return (bool) (! static::isWritable($filename));
}
public function isDirectory()
protected static function isDirectory($filename)
{
return (bool) (is_dir($this->filename));
return (bool) (is_dir($filename));
}
public function isNotDirectory()
protected static function isNotDirectory($filename)
{
return (bool) (! self::isDirectory());
return (bool) (! static::isDirectory($filename));
}
public function isExecutable()
protected static function isExecutable($filename)
{
return (bool) (is_executable($this->filename));
return (bool) (is_executable($filename));
}
public function isNotExecutable()
protected static function isNotExecutable($filename)
{
return (bool) (! self::isExecutable());
return (bool) (! static::isExecutable($filename));
}
public function isFile()
protected static function isFile($filename)
{
return (bool) (is_file($this->filename));
return (bool) (is_file($filename));
}
public function isNotFile()
protected static function isNotFile($filename)
{
return (bool) (! self::isFile());
return (bool) (! static::isFile($filename));
}
public function isLink()
protected static function isLink($filename)
{
return (bool) (is_link($this->filename));
return (bool) (is_link($filename));
}
public function isNotLink()
protected static function isNotLink($filename)
{
return (bool) (! self::isLink());
return (bool) (! static::isLink($filename));
}
}
......@@ -4,27 +4,27 @@ namespace Techendeavors\FileInfo\Traits;
trait Events
{
public function getModifiedTime()
protected static function getModifiedTime($filename)
{
return (object) self::dateTimeObject(filemtime($this->filename));
return (object) static::dateTimeObject(filemtime($filename));
}
public function getChangedTime()
protected static function getChangedTime($filename)
{
return (object) self::dateTimeObject(filectime($this->filename));
return (object) static::dateTimeObject(filectime($filename));
}
public function getAccessedTime()
protected static function getAccessedTime($filename)
{
return (object) self::dateTimeObject(fileatime($this->filename));
return (object) static::dateTimeObject(fileatime($filename));
}
public function getSystemTimeZone()
protected static function getSystemTimeZone()
{
return (object) (new \DateTimeZone(trim(shell_exec("date +%:z"))));
}
public function dateTimeObject(int $timestamp)
protected static function dateTimeObject(int $timestamp)
{
$timezone = self::GetSystemTimeZone();
......@@ -35,13 +35,13 @@ trait Events
}
}
public function getEvents()
protected static function getEvents($filename)
{
if (self::isThere()) {
if (self::isThere($filename)) {
return array(
"modified" => self::getModifiedTime(),
"changed" => self::getChangedTime(),
"accessed" => self::getAccessedTime(),
"modified" => static::getModifiedTime($filename),
"changed" => static::getChangedTime($filename),
"accessed" => static::getAccessedTime($filename),
);
}
}
......
......@@ -21,7 +21,7 @@ trait Hashes
return $computed." ".$unit[$picker];
}
public function hashAvailable(string $algorithm)
public static function hashAvailable(string $algorithm)
{
return (bool) (in_array(strtolower($algorithm), hash_algos()));
}
......@@ -62,21 +62,25 @@ trait Hashes
return (object) $algos;
}
public function hashEngine(string $algorithm)
private static function hashEngine($filename, string $algorithm)
{
if (self::hashAvailable($algorithm)) {
return (string) hash_file($algorithm, $this->filename);
if (static::hashAvailable($algorithm)) {
return (string) hash_file($algorithm, $filename);
}
}
public function getHashes()
private static function getHashes($filename, $hash_size = self::DEFAULT_HASH_MAX_LENGTH, $hash_algos = self::DEFAULT_HASH_ALGOS)
{
if ((self::isFile($this->filename)) && (self::isReadable($this->filename)) && (self::getFileBytes($this->filename) <= config('fileinfo.hash_max_size'))) {
//$hash_max_size = config('fileinfo.hash_max_size') ?? (1024 * 1024 * 20);
//$hash_algos = config('fileinfo.hash_algos') ?? ['crc32','crc32b','md5','ripemd256','sha1','sha256','sha3-256','sha3-384','sha3-512','sha384','sha512'];
if ((static::isFile($filename)) && (static::isReadable($filename)) && (static::getFileBytes($filename) <= $hash_size)) {
$hashes = collect();
foreach (config('fileinfo.hash_algos') as $algo) {
$hashes->put($algo, self::hashEngine($algo));
foreach ($hash_algos as $algo) {
$hashes->put($algo, static::hashEngine($filename, $algo));
}
return (array) $hashes->toArray();
......
......@@ -5,39 +5,39 @@ namespace Techendeavors\FileInfo\Traits;
trait Info
{
public function getRealPath()
private static function getRealPath($filename)
{
return (string) (realpath($this->filename));
return (string) (realpath($filename));
}
public function getFileName()
private static function getFileName($filename)
{
return (string) (basename($this->filename));
return (string) (basename($filename));
}
public function getFilePath()
private static function getFilePath($filename)
{
return (string) (dirname($this->filename));
return (string) (dirname($filename));
}
public function getType()
private static function getType($filename)
{
return (string) (filetype($this->filename));
return (string) (filetype($filename));
}
public function getMime()
private static function getMime($filename)
{
return (string) (mime_content_type($this->filename));
return (string) (mime_content_type($filename));
}
public function getInfo()
private static function getInfo($filename)
{
if (self::isThere($this->filename)) {
if (static::isThere($filename)) {
return array(
"name" => self::getFileName(),
"path" => self::getFilePath(),
"type" => self::getType(),
"mime" => self::getMime(),
"name" => static::getFileName($filename),
"path" => static::getFilePath($filename),
"type" => static::getType($filename),
"mime" => static::getMime($filename),
);
}
}
......
......@@ -4,14 +4,14 @@ namespace Techendeavors\FileInfo\Traits;
trait Permissions
{
public function getOctalPermissions()
private static function getOctalPermissions($filename)
{
return (string) substr(decoct(fileperms($this->filename)), 2);
return (string) substr(decoct(fileperms($filename)), 2);
}
public function getHumanPermissions()
private static function getHumanPermissions($filename)
{
$perms = fileperms($this->filename);
$perms = fileperms($filename);
$info = "";
......@@ -33,26 +33,26 @@ trait Permissions
return (string) $info;
}
public function getUserName()
private static function getUserName($filename)
{
return (string) (posix_getpwuid(fileowner($this->filename))["name"]);
return (string) (posix_getpwuid(fileowner($filename))["name"]);
}
public function getGroupName()
private static function getGroupName($filename)
{
return (string) (posix_getgrgid(filegroup($this->filename))["name"]);
return (string) (posix_getgrgid(filegroup($filename))["name"]);
}
public function getPermissions()
private static function getPermissions($filename)
{
if (self::isThere()) {
if (self::isThere($filename)) {
return array(
"readable" => self::isReadable(),
"writable" => self::isWritable(),
"user" => self::getUserName(),
"group" => self::getGroupName(),
"octal" => self::getOctalPermissions(),
"human" => self::getHumanPermissions(),
"readable" => static::isReadable($filename),
"writable" => static::isWritable($filename),
"user" => static::getUserName($filename),
"group" => static::getGroupName($filename),
"octal" => static::getOctalPermissions($filename),
"human" => static::getHumanPermissions($filename),
);
}
}
......
......@@ -4,17 +4,17 @@ namespace Techendeavors\FileInfo\Traits;
trait Size
{
public function getFileBytes()
private static function getFileBytes($filename)
{
return (int) filesize($this->filename);
return (int) filesize($filename);
}
public function getDirectoryBytes()
private static function getDirectoryBytes($filename)
{
return (int) array_filter(explode("\t", trim(shell_exec('du -sb0 ' . escapeshellcmd($this->filename) . ' 2>/dev/null'))))[0];
return (int) array_filter(explode("\t", trim(shell_exec('du -sb0 ' . escapeshellcmd($filename) . ' 2>/dev/null'))))[0];
}
public function getHumanSize(int $bytes, int $decimals = 2)
private static function getHumanSize(int $bytes, int $decimals = 2)
{
$factor = (int) floor((strlen($bytes) - 1) / 3);
if ($factor > 0) {
......@@ -25,20 +25,20 @@ trait Size
return (string) sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$unit[$factor - 1] . 'B';
}
public function getSize()
private static function getSize($filename)
{
if (self::isThere()) {
if (self::isFile()) {
$bytes = self::getFileBytes();
if (static::isThere($filename)) {
if (static::isFile($filename)) {
$bytes = static::getFileBytes($filename);
}
if (self::isDirectory()) {
$bytes = self::getDirectoryBytes();
if (static::isDirectory($filename)) {
$bytes = static::getDirectoryBytes($filename);
}
return array(
"bytes" => $bytes,
"human" => self::getHumanSize($bytes),
"human" => static::getHumanSize($bytes),
);
}
......
......@@ -4,14 +4,14 @@ namespace Techendeavors\FileInfo\Traits;
trait TestResults
{
public function getTestResults()
private static function getTestResults($filename)
{
if (self::isThere()) {
if (static::isThere($filename)) {
return array(
"directory" => self::isDirectory(),
"executable" => self::isExecutable(),
"file" => self::isFile(),
"link" => self::isLink(),
"directory" => static::isDirectory($filename),
"executable" => static::isExecutable($filename),
"file" => static::isFile($filename),
"link" => static::isLink($filename),
);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment