nextstage-brasil / ns-util
Metodos e classes de utilidades diversas
1.78.24
2025-03-13 10:42 UTC
Requires
- php: >=7.4
- firebase/php-jwt: ^6.10
- laravel/serializable-closure: ^1.3 || ^2
- league/flysystem: ^1.0 || ^3.0
- league/flysystem-aws-s3-v3: ^1.0 || ^3.0
- nelexa/zip: ^3.3 || ^4.0
- phpmailer/phpmailer: ^6.9
- predis/predis: ^2.3
- sendgrid/sendgrid: ^7
- smottt/wideimage: ^1.1
Requires (Dev)
- dev-main
- 1.78.24
- 1.78.23
- 1.78.22
- 1.78.21
- 1.78.20
- 1.78.19
- 1.78.18
- 1.78.17
- 1.78.16
- 1.78.15
- 1.78.14
- 1.78.13
- 1.78.12
- 1.78.11
- 1.78.10
- 1.78.9
- 1.78.8
- 1.78.7
- 1.78.6
- 1.78.5
- 1.78.4
- 1.78.3
- 1.78.2
- 1.78.1
- 1.78.0
- 1.77.123
- 1.77.122
- 1.77.121
- 1.77.120
- 1.77.119
- 1.77.118
- 1.77.117
- 1.77.116
- 1.77.115
- 1.77.114
- 1.77.113
- 1.77.112
- 1.77.111
- 1.77.110
- 1.77.109
- 1.77.108
- 1.77.107
- 1.77.106
- 1.77.105
- 1.77.104
- 1.77.103
- 1.77.102
- 1.77.101
- 1.77.100
- 1.77.99
- 1.77.98
- 1.77.97
- 1.77.96
- 1.77.95
- 1.77.94
- 1.77.93
- 1.77.92
- 1.77.91
- 1.77.90
- 1.77.89
- 1.77.88
- 1.77.87
- 1.77.86
- 1.77.85
- 1.77.84
- 1.77.83
- 1.77.82
- 1.77.81
- 1.77.80
- 1.77.79
- 1.77.78
- 1.77.77
- 1.77.76
- 1.77.75
- 1.77.74
- 1.77.73
- 1.77.72
- 1.77.71
- 1.77.70
- 1.77.69
- 1.77.68
- 1.77.67
- 1.77.66
- 1.77.65
- 1.77.64
- 1.77.63
- 1.77.62
- 1.77.61
- 1.77.60
- 1.77.59
- 1.77.58
- 1.77.57
- 1.77.56
- 1.77.55
- 1.77.54
- 1.77.53
- 1.77.52
- 1.77.51
- 1.77.50
- 1.77.49
- 1.77.48
- 1.77.47
- 1.77.46
- 1.77.45
- 1.77.44
- 1.77.43
- 1.77.42
- 1.77.41
- 1.77.40
- 1.77.39
- 1.77.38
- 1.77.37
- 1.77.36
- 1.77.35
- 1.77.34
- 1.77.33
- 1.77.32
- 1.77.31
- 1.77.30
- 1.77.29
- 1.77.28
- 1.77.27
- 1.77.26
- 1.77.25
- 1.77.24
- 1.77.23
- 1.77.22
- 1.77.21
- 1.77.20
- 1.77.19
- 1.77.18
- 1.77.17
- 1.77.16
- 1.77.15
- 1.77.14
- 1.77.13
- 1.77.12
- 1.77.11
- 1.77.10
- 1.77.9
- 1.77.8
- 1.77.7
- 1.77.6
- 1.77.5
- 1.77.4
- 1.77.3
- 1.77.2
- 1.77.1
- 1.77.0
- 1.76.6
- 1.76.5
- 1.76.4
- 1.76.3
- 1.76.2
- 1.76.1
- 1.76.0
- 1.75.114
- 1.75.113
- 1.75.112
- 1.75.111
- 1.75.110
- 1.75.109
- 1.75.108
- 1.75.107
- 1.75.106
- 1.75.105
- 1.75.104
- 1.75.103
- 1.75.102
- 1.75.101
- 1.75.100
- 1.75.99
- 1.75.98
- 1.75.97
- 1.75.96
- 1.75.95
- 1.75.94
- 1.75.93
- 1.75.92
- 1.75.91
- 1.75.90
- 1.75.89
- 1.75.88
- 1.75.87
- 1.75.86
- 1.75.85
- 1.75.84
- 1.75.83
- 1.75.82
- 1.75.81
- 1.75.80
- 1.75.79
- 1.75.78
- 1.75.77
- 1.75.76
- 1.75.75
- 1.75.74
- 1.75.73
- 1.75.72
- 1.75.71
- 1.75.70
- 1.75.69
- 1.75.68
- 1.75.67
- 1.75.66
- 1.75.65
- 1.75.64
- 1.75.63
- 1.75.62
- 1.75.61
- 1.75.60
- 1.75.59
- 1.75.58
- 1.75.57
- 1.75.56
- 1.75.55
- 1.75.54
- 1.75.53
- 1.75.52
- 1.75.51
- 1.75.50
- 1.75.49
- 1.75.48
- 1.75.47
- 1.75.46
- 1.75.45
- 1.75.44
- 1.75.43
- 1.75.42
- 1.75.41
- 1.75.40
- 1.75.39
- 1.75.38
- 1.75.37
- 1.75.36
- 1.75.35
- 1.75.34
- 1.75.33
- 1.75.32
- 1.75.31
- 1.75.30
- 1.75.29
- 1.75.28
- 1.75.27
- 1.75.26
- 1.75.25
- 1.75.24
- 1.75.23
- 1.75.22
- 1.75.21
- 1.75.20
- 1.75.19
- 1.75.18
- 1.75.17
- 1.75.16
- 1.75.15
- 1.75.14
- 1.75.13
- 1.75.12
- 1.75.11
- 1.75.10
- 1.75.9
- 1.75.8
- 1.75.7
- 1.75.6
- 1.75.5
- 1.75.4
- 1.75.3
- 1.75.2
- 1.75.1
- 1.75.0
- 1.74.11
- 1.74.10
- 1.74.9
- 1.74.8
- 1.74.7
- 1.74.6
- 1.74.5
- 1.74.4
- 1.74.3
- 1.74.2
- 1.74.1
- 1.74.0
- 1.73.0
- 1.72.34
- 1.72.33
- 1.72.32
- 1.72.31
- 1.72.30
- 1.72.29
- 1.72.28
- 1.72.27
- 1.72.26
- 1.72.25
- 1.72.24
- 1.72.23
- 1.72.22
- 1.72.21
- 1.72.20
- 1.72.19
- 1.72.18
- 1.72.17
- 1.72.16
- 1.72.15
- 1.72.14
- 1.72.13
- 1.72.12
- 1.72.11
- 1.72.10
- 1.72.9
- 1.72.8
- 1.72.7
- 1.72.6
- 1.72.5
- 1.72.4
- 1.72.3
- 1.72.2
- 1.72.1
- 1.72.0
- 1.71.26
- 1.71.25
- 1.71.24
- 1.71.23
- 1.71.22
- 1.71.21
- 1.71.20
- 1.71.19
- 1.71.18
- 1.71.17
- 1.71.16
- 1.71.15
- 1.71.14
- 1.71.13
- 1.71.12
- 1.71.11
- 1.71.10
- 1.71.9
- 1.71.8
- 1.71.7
- 1.71.6
- 1.71.5
- 1.71.4
- 1.71.3
- 1.71.2
- 1.71.1
- 1.71.0
- 1.70.15
- 1.70.14
- 1.70.13
- 1.70.12
- 1.70.11
- 1.70.10
- 1.70.9
- 1.70.8
- 1.70.7
- 1.70.6
- 1.70.5
- 1.70.4
- 1.70.3
- 1.70.2
- 1.70.1
- 1.70.0
- 1.69.11
- 1.69.10
- 1.69.9
- 1.69.8
- 1.69.7
- 1.69.6
- 1.69.5
- 1.69.4
- 1.69.3
- 1.69.2
- 1.69.1
- 1.69.0
- 1.68.11
- 1.68.10
- 1.68.9
- 1.68.8
- 1.68.7
- 1.68.6
- 1.68.5
- 1.68.4
- 1.68.3
- 1.68.2
- 1.68.1
- 1.68.0
- 1.67.3
- 1.67.2
- 1.67.1
- 1.67.0
- 1.66.4
- 1.66.3
- 1.66.2
- 1.66.1
- 1.66.0
- 1.65.23
- 1.65.22
- 1.65.21
- 1.65.20
- 1.65.19
- 1.65.18
- 1.65.17
- 1.65.16
- 1.65.15
- 1.65.14
- 1.65.13
- 1.65.12
- 1.65.11
- 1.65.10
- 1.65.9
- 1.65.8
- 1.65.7
- 1.65.6
- 1.65.5
- 1.65.4
- 1.65.3
- 1.65.2
- 1.65.1
- 1.65.0
- 1.64.15
- 1.64.14
- 1.64.13
- 1.64.12
- 1.64.11
- 1.64.10
- 1.64.9
- 1.64.8
- 1.64.7
- 1.64.6
- 1.64.5
- 1.64.4
- 1.64.3
- 1.64.2
- 1.64.1
- 1.64.0
- 1.63.11
- 1.63.10
- 1.63.9
- 1.63.8
- 1.63.7
- 1.63.6
- 1.63.5
- 1.63.4
- 1.63.3
- 1.63.2
- 1.63.1
- 1.63.0
- 1.62.2
- 1.62.1
- 1.62.0
- 1.61.5
- 1.61.4
- 1.61.3
- 1.61.2
- 1.61.1
- 1.61.0
- 1.60.13
- 1.60.12
- 1.60.11
- 1.60.10
- 1.60.9
- 1.60.8
- 1.60.7
- 1.60.6
- 1.60.5
- 1.60.4
- 1.60.3
- 1.60.2
- 1.60.1
- 1.60.0
- 1.59.12
- 1.59.11
- 1.59.10
- 1.59.9
- 1.59.8
- 1.59.7
- 1.59.6
- 1.59.5
- 1.59.4
- 1.59.3
- 1.59.2
- 1.59.1
- 1.59.0
- 1.58.5
- 1.58.4
- 1.58.3
- 1.58.2
- 1.58.1
- 1.58.0
- 1.57.2
- 1.57.1
- 1.57.0
- 1.56.1
- 1.56.0
- 1.55.5
- 1.55.4
- 1.55.3
- 1.55.2
- 1.55.1
- 1.55.0
- 1.54.2
- 1.54.1
- 1.54.0
- 1.53.7
- 1.53.6
- 1.53.5
- 1.53.4
- 1.53.3
- 1.53.2
- 1.53.1
- 1.53.0
- 1.52.0
- 1.51.0
- 1.50.3
- 1.50.2
- 1.50.1
- 1.50.0
- 1.49.5
- 1.49.4
- 1.49.3
- 1.49.2
- 1.49.1
- 1.49.0
- 1.48.0
- 1.47.13
- 1.47.12
- 1.47.11
- 1.47.10
- 1.47.9
- 1.47.8
- 1.47.7
- 1.47.6
- 1.47.5
- 1.47.3
- 1.47.2
- 1.47.1
- 1.47.0
- 1.46.0
- 1.45.1
- 1.45.0
- 1.44.0
- 1.43.3
- 1.43.2
- 1.43.1
- 1.43.0
- 1.42.1
- 1.42.0
- 1.41.2
- 1.41.1
- 1.41.0
- 1.40.0
- 1.39.6
- 1.39.5
- 1.39.4
- 1.39.3
- 1.39.2
- 1.39.1
- 1.39.0
- 1.38.2
- 1.38.1
- 1.38.0
- 1.37.0
- 1.36.0
- 1.35.0
- 1.34.7
- 1.34.6
- 1.34.5
- 1.34.4
- 1.34.3
- 1.34.2
- 1.34.1
- 1.34.0
- 1.33.11
- 1.33.10
- 1.33.9
- 1.33.8
- 1.33.7
- 1.33.3
- 1.33.2
- 1.33.1
- 1.33.0
- 1.32.30
- 1.32.29
- 1.32.28
- 1.32.27
- 1.32.26
- 1.32.25
- 1.32.24
- 1.32.23
- 1.32.22
- 1.32.21
- 1.32.20
- 1.32.19
- 1.32.18
- 1.32.17
- 1.32.16
- 1.32.15
- 1.32.14
- 1.32.13
- 1.32.12
- 1.32.11
- 1.32.10
- 1.32.9
- 1.32.8
- 1.32.7
- 1.32.6
- 1.32.5
- 1.32.4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
- 1.1.55
- 1.1.41
- 1.1.40
- 1.1.39
- 1.1.38
- 1.1.37
- 1.1.36
- 1.1.35
- 1.1.34
- 1.1.33
- 1.1.32
- 1.1.31
- 1.1.30
- 1.1.29
- 1.1.28
- 1.1.27
- 1.1.26
- 1.1.25
- 1.1.24
- 1.1.23
- 1.1.22
- 1.1.21
- 1.1.20
- 1.1.19
- 1.1.18
- 1.1.17
- 1.1.16
- 1.1.15
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.62
- 1.0.61
- 1.0.60
- 1.0.59
- 1.0.58
- 1.0.57
- 1.0.56
- 1.0.55
- 1.0.54
- 1.0.53
- 1.0.52
- 1.0.51
- 1.0.50
- 1.0.49
- 1.0.48
- 1.0.47
- 1.0.46
- 1.0.45
- 1.0.44
- 1.0.43
- 1.0.42
- 1.0.41
- 1.0.40
- 1.0.39
- 1.0.38
- 1.0.37
- 1.0.36
- 1.0.35
- 1.0.34
- 1.0.33
- 1.0.32
- 1.0.31
- 1.0.30
- 1.0.29
- 1.0.28
- 1.0.27
- 1.0.25
- 1.0.24
- 1.0.23
- 1.0.22
- 1.0.21
- 1.0.20
- 1.0.19
- 1.0.18
- 1.0.17
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2025-03-13 09:43:52 UTC
README
Examples
API
- Atention: onError and onSuccess order on middlewares is important
<?php
use AppLibrary\App;
use JDrel\NsLibrary\Entities\Logs;
use NsUtil\Api;
use NsUtil\Helper;
use NsUtil\Services\RateLimiter;
if (is_dir(__DIR__ . '/../../_build/')) {
error_reporting(E_ALL);
}
include __DIR__ . '/../../vendor/autoload.php';
App::init();
App::setDevelopeMode();
$namespace = Helper::getPsr4Name() . '\\Routers';
// Api
$api = new Api();
$headers = $api->getHeaders(true);
$resource = Helper::name2CamelCase($api->getConfigData()['rest']['resource']);
$apikey = $api->getHeaders(true)['apikey'] ?? $api->getConfigData()['ParamsRouter'][3] ?? '-1';
// Rotas liberadas sem precisar de autenticação de usuário
$freeRoutes = ['version', 'webhooks', 'cpuAgent'];
// log de uso
$hit = (new Logs([
'appnameLog' => 'Api Consume',
'textLog' => $apikey,
'extrasLog' => [
'route' => $resource,
'headers' => $headers,
'payload' => $api->getBody()
]
]))->save();
// Rate, Middlewares and Resolver
$dados = [];
$api
->onError(fn ($param1, $param2) => $hit->setExtrasLog(array_merge($hit->getExtrasLog('array'), ['response' => ['data' => $param1, 'code' => $param2]]))->save())
->onSuccess(fn ($param1, $param2) => $hit->setExtrasLog(array_merge($hit->getExtrasLog('array'), ['response' => ['code' => $param2]]))->save())
->rateLimit(3, 60)
->middleware('Invalid APIKEY', 403, fn () => in_array($resource, $freeRoutes) ? true : App::validaApiKey($apikey))
->rest($namespace);
Rate Limit
// to clear:
// php nsutil rate:clear {appkey_or_IP}
try {
// rate 120 calls by ip per minute
RateLimiter::byIP(120, RateLimiter::PER_MINUTE);
// rate 100 calls per apikey per day
RateLimiter::byKey('my-app-key-1', 100, RateLimiter::PER_DAY);
// Exception on error to connect on redis
} catch (RedisConnectionException $exc) {
http_response_code($exc->getCode() ?? 400);
echo "REDIS-ERROR: " . $exc->getMessage() . PHP_EOL;
// Too many requests (429)
} catch (TooManyRequestException $exc) {
http_response_code($exc->getCode());
echo $exc->getMessage() . PHP_EOL;
// Others exceptions
} catch (Exception $exc) {
http_response_code($exc->getCode() ?? 400);
echo "ERROR: " . $exc->getMessage() . PHP_EOL;
}
Composer local
Configure a .env com o parametro LOCAL_COMPOSER listando os paths absoutos a serem atualizados localmente
Ex.: LOCAL_COMPOSER="/home/user/apps/app1;/home/user/apps/app2"
Dentro deste projeto será atualizado a pasta /vendor/nextstage-brasil/ns-util/src com a cópia do conteudo deste projeto.
Para executar: sudo php nsutil composer:local