{"id":36371970,"url":"https://github.com/redbitcz/utils","last_synced_at":"2026-01-11T14:01:49.756Z","repository":{"id":62534172,"uuid":"364521299","full_name":"redbitcz/utils","owner":"redbitcz","description":"Common utils from Redbit projects","archived":false,"fork":false,"pushed_at":"2022-05-26T06:51:05.000Z","size":74,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-13T23:37:37.030Z","etag":null,"topics":["bitwise","bitwise-operations","logger","pcntl","pcntl-signals","php7","process-control"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/redbitcz/utils","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/redbitcz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-05T09:14:58.000Z","updated_at":"2022-02-12T13:38:37.000Z","dependencies_parsed_at":"2022-11-02T14:46:54.406Z","dependency_job_id":null,"html_url":"https://github.com/redbitcz/utils","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/redbitcz/utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redbitcz%2Futils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redbitcz%2Futils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redbitcz%2Futils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redbitcz%2Futils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redbitcz","download_url":"https://codeload.github.com/redbitcz/utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redbitcz%2Futils/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28306985,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-11T11:18:18.743Z","status":"ssl_error","status_checked_at":"2026-01-11T11:07:56.842Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bitwise","bitwise-operations","logger","pcntl","pcntl-signals","php7","process-control"],"created_at":"2026-01-11T14:01:25.601Z","updated_at":"2026-01-11T14:01:49.738Z","avatar_url":"https://github.com/redbitcz.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Redbit Utils\n\nLightweight utilities for logging, IO, and Unix-like process signal\n\n## Installation\n\nInstall via Composer:\n\n```shell\ncomposer install redbitcz/utils\n```\n\n## Requirements \nPackage requires PHP version 7.3 and above.\n\nFor handling Unix-like process signals requires the `pcntl` and `posix` PHP extensions. Without that support\nrelated method call will be siletly ignored.\n\n## Usage\n\n### `Locker`\n\nThe `\\Redbitcz\\Utils\\Lock\\Locker` class is simple implementation of lock/semaphor based of filelock. It's optimized for\nLinux architecture. \n\nLocker support two modes:\n\n - **Blocking mode** – Blocking mode is create semaphor for locked space, all concurrent locks will **wait to release\n    previous lock**. Be careful, it may cause to deadlock of PHP processes, because lock at filesystem is not subject of\n    [`max_execution_time`](https://www.php.net/manual/en/info.configuration.php#ini.max-execution-time) limit!\n - **Non blocking mode** – Non blocking mode is create lock which is prevent access concurrent processes to locked stage. \n    All concurent locks **will imediatelly fails** with `LockObtainException` Exception.\n\nExample non-blocking lock: \n\n```php\n    $locker = new Locker(__DIR__, 'example', Locker::NON_BLOCKING);\n    \n    try {\n        $locker-\u003elock();\n        \n        // ... exclusive operation\n        \n        $locker-\u003eunlock();\n    }\n    catch (LockObtainException $e) {\n        die('Error: Another process is alreasy processing that stuff');\n    }\n```\n\nSee [Non-blocking `Locker` example](examples/lock/non-blocking-locker.php).\n\nExample blocking lock:\n\n```php\n    $locker = new Locker(__DIR__, 'example', Locker::BLOCKING);\n    \n    $locker-\u003elock(); // concurrent process will be wait here to release previous lock\n    \n    // ... exclusive operation\n    \n    $locker-\u003eunlock();\n```\n\nSee [Blocking `Locker` example](examples/lock/blocking-locker.php).\n\n### `Logger`\n\nThe `\\Redbitcz\\Utils\\Log\\Logger` class is implementation of PSR-3 logger interface and it decorates each\nlogger record with time and log severity name.\n\nExample:\n```\n[2021-05-05 11:49:36] INFO: Logged message 1\n[2021-05-05 11:49:38] DEBUG: Another logged message\n```\n\nLogger requires Writer `\\Redbitcz\\Utils\\IO\\IOutStream` instance. Package contains few several types\nof Writer implementations which are different by the log target (console, general output, standard output, HTML output,\nor file).\n\nLogger also support sectionalization for long-processing operations:\n\nExample:\n\n```php\n$logger-\u003einfo(\"Processing message: $messageId\");\n\n$messageLogger = $logger-\u003esection($messageId);\n$messageLogger-\u003einfo('Open');\n\nfunction parse(LoggerInterface $parserLogger) {\n    $parserLogger-\u003einfo('Parsing...');\n    // ...\n    $parserLogger-\u003einfo('Parsing OK');\n}\n\nparse($messageLogger-\u003esection('parser'));\n\n$messageLogger-\u003einfo('Save');\n\n$logger-\u003einfo('Done');\n```\n\nSends to output:\n```\n[2021-05-05 11:49:36] INFO: Processing message: 123456789\n[2021-05-05 11:49:37] INFO: {123456789} Open\n[2021-05-05 11:49:38] INFO: {123456789/parser} Parsing...\n[2021-05-05 11:49:38] INFO: {123456789/parser} Parsing OK\n[2021-05-05 11:49:38] INFO: {123456789} Save\n[2021-05-05 11:49:36] INFO: Done\n```\n\nSection is useful to provide logger to another service which is requested to process single entity.\n\nSee [`Logger` example](examples/log/output-logger.php).\n\n### `Progress`\nThe `\\Redbitcz\\Utils\\Log\\Progress` class is simple generator of progress status to reporting progress of operations.\nIn additive is added the time spent is each step and whole operation.\n\nExample: \n```shell\n[2021-05-05 10:40:06] DEBUG: [ 0.000s/ 0.000] step 1/9: Logged step 1\n[2021-05-05 10:40:06] DEBUG: [ 0.000s/ 0.000] step 2/9: Another logged message\n[2021-05-05 10:40:06] DEBUG: [ 0.371s/ 0.371] step 3/9: Foo\n[2021-05-05 10:40:10] DEBUG: [ 3.900s/ 4.271] step 4/9: Bar\n[2021-05-05 10:40:10] DEBUG: [ 0.000s/ 4.271] step 5/9: Foo Bar\n[2021-05-05 10:40:10] DEBUG: [ 0.000s/ 4.271] step 6/9: Foo Baz\n[2021-05-05 10:40:10] DEBUG: [ 0.000s/ 4.271] step 7/9: Foo comes to the Bar\n[2021-05-05 10:40:11] DEBUG: [ 0.212s/ 4.483] step 8/9: Hey Donald, get off that chandelier! \n[2021-05-05 10:40:11] DEBUG: [ 0.001s/ 4.484] step 9/9: All Done\n```\nSee [`Progress` example](examples/log/progress.php).\n\n### `ProcessTerminationLock`\n\nThe `\\Redbitcz\\Utils\\Process\\ProcessTerminationLock` class is simple mechanism how to prevent (rspt. delay) unexpected\nexit of PHP process during operation processing. It's recommended to workers to prevent break during processing a job\nand similar usage in processes managed by a Process Control system (`systemd`, `supervisor`, etc.).\n\nExample:\n\n```php\nwhile(true) {\n    $job = $worker-\u003ewaitToJob();\n    \n    ProcessTerminationLock::lock(); // Lock termination to prevent break job processing\n    \n    //... long job processing  \n\n    ProcessTerminationLock::unlock(); // Unlock\n}\n```\nSee [`ProcessTerminationLock` example](examples/process/termination-lock.php).\n\n### `BitwiseVariator`\nClasses in `\\Redbitcz\\Utils\\Bitwise` namespace provides filtered bit variations generator over\n[Bitwise values](https://en.wikipedia.org/wiki/Bitwise_operation).\n\nThat mean, when you have bits `1011`, variator generates all bits variations.\n\n```php\n$variations = BitwiseVariator::create(0b1011)-\u003evariate();\n```\n\n| Variation for bits `1011` |\n|--------------------------:|\n|                    `0000` |\n|                    `0001` |\n|                    `0010` |\n|                    `0011` |\n|                    `1000` |\n|                    `1001` |\n|                    `1010` |\n|                    `1011` |\n\n#### Filters\n\n`BitwiseVariator` class provide filter to select variations with(out) some bits only.\n\n```php\n$variations = BitwiseVariator::create(0b1011)-\u003emust(0b0010)-\u003evariate();\n```\n\n| Variation for bits `1011` with bite `0010` |\n|-------------------------------------------:|\n|                                     `0010` |\n|                                     `0011` |\n|                                     `1010` |\n|                                     `1011` |\n\n\n```php\n$variations = BitwiseVariator::create(0b1011)-\u003emustNot(0b0010)-\u003evariate();\n```\n\n| Variation for bits `1011` without bite `0010` |\n|----------------------------------------------:|\n|                                        `0000` |\n|                                        `0001` |\n|                                        `1000` |\n|                                        `1001` |\n\nBe aware to use more than 8 variated bits, because it proceed huge of variants:\n\n![Table with count of variants for every variated bits](https://user-images.githubusercontent.com/1657322/153865836-174cbe67-3216-4e47-954b-bec50e8d2c26.png)\n\n(source: [Spreadseed Bitwise Variator counts](https://drive.google.com/open?id=1J4M0PyoQFTDgKk84fVjzhtil_Af0_gVZX0BdPlD5uFg))\n\n## License\nThe MIT License (MIT). Please see [License File](LICENSE) for more information.\n\n## Contact\nRedbit s.r.o. - @redbitcz - info@redbit.cz\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredbitcz%2Futils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredbitcz%2Futils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredbitcz%2Futils/lists"}