Download the PHP package popphp/pop-audit without Composer
On this page you can find all versions of the php package popphp/pop-audit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download popphp/pop-audit
More information about popphp/pop-audit
Files in popphp/pop-audit
Package pop-audit
Short Description Pop Audit Component for Pop PHP Framework
License BSD-3-Clause
Homepage http://github.com/popphp/pop-audit
Informations about the package pop-audit
pop-audit
- Overview
- Install
- Quickstart
- Storing Changes
- Retrieving Changes
- Diffing
- Using Files
- Using a Database
- Using HTTP
Overview
Pop Audit is an auditing component of the Pop PHP Framework. It allows you to track and recall changes in model states, which is useful for rolling back mistakes or recovering lost data. It provides different adapters to achieve this, all of which are interchangeable using the same interface:
- File
- Database
- HTTP
pop-audit
is a component of the Pop PHP Framework.
Top
Install
Install pop-audit
using Composer.
composer require popphp/pop-audit
Or, require it in your composer.json file
"require": {
"popphp/pop-audit" : "^2.0.1"
}
Top
Quickstart
With the audit component, you can store model state data changes and recall them at a later date.
Storing Changes
To store the model data, there are two required data points - the model name and model ID. After that, optional data points such as user data or the domain can be stored. First we create the auditor and set the data points:
Then, we look at the changed model data. In this example, the model state contains 4 data points,
2 of which have changed: username
and phone
. Once passed to the auditor's send()
method,
it will "diff" the two states and record the differences, as well as snapshot of the final
changed state:
Top
Retrieving Changes
Interacting with the auditor's adapter, the previously stored model states can be retrieved:
List all stored states
List stored states for a particular model and model ID
Other methods are available to help refine your search for previous states:
getStateById()
getStateByTimestamp()
getStateByDate()
The state structure will look like:
The storing of the full state is on by default, can be turned off by passing a false
boolean
to the send()
method:
Top
Diffing
In the above examples, the pop-audit
component automatically handles "diffing" for you. If you have
another resource that evaluates the differences, you can pass those directly into the auditor as well:
An example of this is the Pop\Db\Record
class from the pop-db
component. It automatically tracks the
"dirty" values that have been changed while working with a record object. You can then used the getDirty()
method of the Pop\Db\Record
class to return an array with the keys old
and new
and pass them off to
the auditor.
Top
Using Files
With the file adapter, you set the folder you want to save the audit record to, and save the model state changes like this:
In this case, the variable $logFile
would contain the name of the audit log file, for example
pop-audit-aed112d5d6de258762c03aa597a47f9b-653ec767ee591-1698613095.log
in case it needs to be
referenced again. That file will contain the JSON-encoded data that tracks the difference between
the model states, as well as a snapshot of the full state (if provided):
Top
Using a Database
Using a database connection requires the use of the pop-db
component and a database table class
that extends the Pop\Db\Record
class. Consider a database and table class set up in your
application like this:
Then you can use the table adapter like this:
If needed, the variable $row
contains the newly created record in the audit table.
Top
Using HTTP
You can also send your audit data to an HTTP service like this:
If needed, the variable $response
contains the HTTP response returned by the HTTP request.
Top
All versions of pop-audit with dependencies
popphp/popphp Version ^4.3.6
popphp/pop-db Version ^6.6.0
popphp/pop-http Version ^5.3.0