PHPackages                             vinogradsoft/scanner - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. vinogradsoft/scanner

ActiveLibrary

vinogradsoft/scanner
====================

Vinograd is a powerful library for processing hierarchical data. It provides an intuitive API for traversing trees in breadth-first and depth-first orders. Developers can also implement their own visitor patterns to process specific nodes or leaves.

2.0.0(2y ago)3773MITPHPPHP &gt;=8.0

Since Oct 6Pushed 2y ago1 watchersCompare

[ Source](https://github.com/vinogradsoft/scanner)[ Packagist](https://packagist.org/packages/vinogradsoft/scanner)[ RSS](/packages/vinogradsoft-scanner/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (3)

[![Simple Files logo](banner.svg)](banner.svg)

[![codecov](https://camo.githubusercontent.com/3e6f9c0231672a16d7fcf02d0a0917fafef036b57a6ef9cc2c32a30f86df803c/68747470733a2f2f636f6465636f762e696f2f67682f76696e6f67726164736f66742f7363616e6e65722f67726170682f62616467652e7376673f746f6b656e3d394b4133533256584251)](https://codecov.io/gh/vinogradsoft/scanner)[![](https://camo.githubusercontent.com/231e6bea9f265fd6379e656eec06716656fd3d6439631b6be4316db060d0c253/68747470733a2f2f62616467656e2e6e65742f7374617469632f6c6963656e73652f4d49542f677265656e)](https://camo.githubusercontent.com/231e6bea9f265fd6379e656eec06716656fd3d6439631b6be4316db060d0c253/68747470733a2f2f62616467656e2e6e65742f7374617469632f6c6963656e73652f4d49542f677265656e)

What is Scanner?
================

[](#what-is-scanner)

> 👉 Scanner is a skeleton for building systems for searching and processing data in hierarchical structures. It offers two approaches to data analysis: the first, breadth-first analysis, looks at all levels of the tree in one pass, and the second, depth-first analysis, processes each level of the tree in turn, starting from the root. The main purpose of this tool is to enable developers to focus on the logic of the application rather than how to traverse trees. Scanner can be useful for programmers working with hierarchical data and seeking to automate the process of processing such data.

Features
--------

[](#features)

- 💪 Support for different drivers for different use cases (for example, [file driver](https://github.com/vinogradsoft/files-driver) for directory traversal or ArrayDriver for working with arrays).
- 👍 Ability to search and process certain elements in tree structures.
- 🚧 Filtering elements at the time of crawling.
- 🤚 Stop tree traversal at any location based on condition.
- ⚗️ Flexibility of use thanks to its own configurations and parameters.

Install
-------

[](#install)

To install with composer:

```
php composer require vinogradsoft/scanner "^2.0"

```

General Information
-------------------

[](#general-information)

The main object in the library is `Vinograd\Scanner\Scanner`. This object accumulates all the traversal settings and starts traversing the tree.

Bypass algorithms are placed in separate classes, so-called strategies, which can be changed depending on the task. There are two such strategies implemented in the library: breadth-first (`Vinograd\Scanner\BreadthStrategy`) and depth-first (`Vinograd\Scanner\SingleStrategy`).

The `Vinograd\Scanner\Visitor` interface is used to process and collect data. There is no implementation for it in the library; its implementation is carried out by the developer using this library. There are 4 useful methods in this interface:

- `scanStarted` - called when scanning starts;
- `scanCompleted` - called when the strategy has completed its work;
- `visitLeaf` - called when the strategy visited a leaf of the tree;
- `visitNode` - called when the strategy visited a tree node;

The depth-first traversal algorithm is achieved by the `Vinograd\Scanner\SingleStrategy` strategy. Its algorithm is quite simple. It receives the children of the node passed to it and exits. The idea is to put a `Scanner` in a `Visitor`and run the scan repeatedly for each child node in the `visitNode` method. The result is a controlled depth-first recursive traversal.

In the `Vinograd\Scanner\BreadthStrategy` strategy there is no need to do this; it ends when the last element of the tree is reached.

Among other things, the `\Vinograd\Scanner\Verifier` object is used during the crawl. Its purpose is to ensure that the child element meets the requirements and the `visitLeaf` and `visitNode` methods of the `Visitor` object should be called on the element. In other words, you can provide it with some rules and filter out the elements of the tree. For the `Vinograd\Scanner\BreadthStrategy` strategy, this does not mean that if a node is filtered, the strategy will not bypass its child nodes. This means that the `visitLeaf` and `visitNode` methods will not be called on failed elements. This way, you can configure the bypass to only perform processing on the target nodes. For the `Vinograd\Scanner\SingleStrategy` strategy this will mean that child nodes will not be scanned, since the `visitNode` method will not be called and you will not be able to run a scan for it. You can get around this by relaxing the rules in the `Verifier` object and creating a `Visitor` proxy, which runs a scan for all nodes, but does not call the `visitNode` method on the proxied object.

The driver allows you to select the type of tree objects that need to be traversed. The library implements a driver for traversing arrays. The class is called `Vinograd\Scanner\ArrayDriver`. Another external implementation [files-driver](https://github.com/vinogradsoft/files-driver) allows you to traverse directories in the file system. Both of these drivers implement the `Vinograd\Scanner\Driver` interface.

Example
-------

[](#example)

Let's look at a conceptual use case.

> 📢 To make the example clearer, it does not include checks that are typically performed in code. Instead, the example focuses on demonstrating the capabilities of the system. The example includes a look at the classes that are needed to understand how the system works. You can run the example by cloning this [repository](https://github.com/vinogradsoft/example-for-scanner).

### Formulation of the problem

[](#formulation-of-the-problem)

You need to make a console command that triggers a series of commands to be executed in a certain sequence, based on the configuration. In the configuration, you need to bypass the nodes, starting with `tasks`, and ignore the `other` node.

The configuration looks like this:

```
