Completed
Pull Request — master (#26)
by Théo
02:11
created

formatted_filesize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box;
16
17
use Assert\Assertion;
18
use Phar;
19
use Symfony\Component\Filesystem\Filesystem;
20
use Webmozart\PathUtil\Path;
21
22
function canonicalize(string $path): string
23
{
24
    $lastChar = substr($path, -1);
25
26
    $canonical = Path::canonicalize($path);
27
28
    return '/' === $lastChar ? $canonical.$lastChar : $canonical;
29
}
30
31
function is_absolute(string $path): bool
32
{
33
    static $fileSystem;
34
35
    if (null === $fileSystem) {
36
        $fileSystem = new Filesystem();
37
    }
38
39
    return $fileSystem->isAbsolutePath($path);
40
}
41
42
/**
43
 * TODO: this function should be pushed down to the PHAR extension.
44
 */
45
function get_phar_compression_algorithms(): array
46
{
47
    static $algorithms = [
48
        'GZ' => Phar::GZ,
49
        'BZ2' => Phar::BZ2,
50
        'NONE' => Phar::NONE,
51
    ];
52
53
    return $algorithms;
54
}
55
56
function formatted_filesize(string $path)
57
{
58
    Assertion::file($path);
59
60
    $size = filesize($path);
61
    $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
62
    $power = $size > 0 ? floor(log($size, 1024)) : 0;
63
64
    return sprintf(
65
        '%s%s',
66
        number_format(
67
            $size / (1024 ** $power),
68
            2,
69
            '.',
70
            ','
71
        ),
72
        $units[$power]
73
    );
74
}
75
76
function register_aliases(): void
77
{
78
    if (false === class_exists(\Herrera\Box\Compactor\Javascript::class, false)) {
79
        class_alias(\KevinGH\Box\Compactor\Javascript::class, \Herrera\Box\Compactor\Javascript::class);
80
    }
81
82
    if (false === class_exists(\Herrera\Box\Compactor\Json::class, false)) {
83
        class_alias(\KevinGH\Box\Compactor\Json::class, \Herrera\Box\Compactor\Json::class);
84
    }
85
86
    if (false === class_exists(\Herrera\Box\Compactor\Php::class, false)) {
87
        class_alias(\KevinGH\Box\Compactor\Php::class, \Herrera\Box\Compactor\Php::class);
88
    }
89
}
90