Documentation and Examples

Learn how to use mindpowered packages

Changelog

Getting Started

Advanced Topics

Persistence

What is Persistence?

Persistence is everything you need to organize how to store and retrieve your program’s data. This can be in any format you like. It is also possible to use your own.

MagLev

What is MagLev?

MagLev is a bus that allows your code to communicate with Mindpowered’s repository of packages.

How to use MagLev

Your code can access the Mindpowered packages through MagLev using four methods,

  • register
  • call
  • listen
  • emit

register lets you register a function to use later with the call command. The first parameter is the function name, the second parameter is the function. You can either pass the register command a function as a parameter or you can give it the function’s code as a parameter:

class A {
    function myfunc($args) {
        return 'Hello, World!';
    }
}

$a = new A();
$maglev->register('example.myfunc', [$a, 'myfunc']);
$maglev->register('example.myanonfunc', function($args) {
    return 'Hello, ' . $args[0] . '!';
});

call lets you call the methods you have created with register.

echo $maglev->call('example.myfunc', []) . "\n";
Hello, World!
echo $maglev->call('example.myanonfunc', ['Friend']) . "\n";
Hello, Friend!

listen lets your MagLev instance know that your code is interested in a specific event name and allows you to execute a callback function if that event happens. The first parameter is the eventname and the second parameter is the function you want called should the event happen. The function that is called can make use of the argument specified by the emit function.

emit emits a specific event name and lets any listeners know that that event is happening. Will trigger the callback function of any listeners. You also specify an argument that the callback function specified in your listen commands can use.

// function
$myhandler = function ($event, $args) {
    print("My Event!\n");
    print_r($args);
};
// add a listener
$maglev->listen('example.myevent', $myhandler);
// emit event
$maglev->emit('example.myevent', [1]);
My Event!
Array
(
    [0] => 1
)
// add another listener
$maglev->listen('example.myevent', $myhandler);
// emit event
$maglev->emit('example.myevent', [2]);
My Event!
Array
(
    [0] => 2
)
My Event!
Array
(
    [0] => 2
)

PHP

Getting Started

You can get MagLev using Composer. Composer manages dependencies for packages from Packagist and you can get it here: https://getcomposer.org/ .

Create a composer.json file in the top-most directory of your project and add mindpowered/maglev to the requirements:

{
        "require": {
                "mindpowered/maglev" : "*"
        }
}

Now you can run the command to update composer: composer update. In order for composer to install the package dependencies you will need an archive manager such as zip installed. Composer requires use of the commands zip and unzip.

MagLev also requires that you use the php extension “mbstring”. Make sure it is installed before running any code using the MagLev bus. https://www.php.net/manual/en/mbstring.installation.php First, make sure you have the correct requirements and you are using the maglev\MagLevPhp package:

require __DIR__ . '/vendor/autoload.php';
use maglev\MagLevPhp;

Then you can create an instance of the maglev bus:

$maglev = MagLevPhp::getInstance('default');
How to Use MagLev (PHP)

Below is an an example of how to use MagLev in PHP:

<?php
require __DIR__ . '/vendor/autoload.php';
use maglev\MagLevPhp;
$maglev = MagLevPhp::getInstance('default');
// register class method
class A {
    // named function
    function myfunc($args) {
        return 'Hello, World!';
    }
}
$a = new A();
$maglev->register('example.myfunc', [$a, 'myfunc']);
// call method
echo $maglev->call('example.myfunc', []) . "\n";
// register anonymous function
$maglev->register('example.myanonfunc', function($args) {
    return 'Hello, ' . $args[0] . '!'; // the $args[0] refers to the first argument
});
// call method
echo $maglev->call('example.myanonfunc', ['Friend']) . "\n"; // the . combines strings
// add a listener
$myhandler = function ($event, $args) {
    print("My Event!\n");
    print_r($args);
};
$maglev->listen('example.myevent', $myhandler);
// emit event
$maglev->emit('example.myevent', [1]);
// add another listener
$maglev->listen('example.myevent', $myhandler);
// emit event
$maglev->emit('example.myevent', [2]);
My Event!
Array
(
    [0] => 2
)
My Event!
Array
(
    [0] => 2
)

JavaScript

Getting Started

You can retrieve the MagLev package using npm https://www.npmjs.com/. After initializing your packages.json file in your project’s directory with npm init, you can install the MagLev package with npm i @mindpowered/maglev --save hint: the --save option will add the newly installed dependency in your packages.json file. Once installed, you will need to import maglev in your code before using it. One way of importing is below:

const maglev = require('@mindpowered/maglev');

let bus = maglev.maglev.MagLevJs.getInstance("default");
How to Use MagLev (JavaScript)

Below is an example of how to use MagLev in JavaScript:

const maglev = require('@mindpowered/maglev');


let bus = maglev.maglev.MagLevJs.getInstance("default");


bus.register("myfunc", function(args) {

  return "Hello, " + args[0] + "!"

});


console.log(bus.call("myfunc", ["World"]));


bus.listen("myevent", function(event, args) {

  console.log("Hello from " + args[0]);

});

bus.emit("myevent", ["a friend"]);
Hello, World!
Hello from a friend

Python

Getting Started

You can retrieve the MagLev package from the Python Package Index https://pypi.org/project/mindpowered-maglev/ using pip. First make sure you have python3 and python3-pip installed. Then you can start by making a requirements.txt file in your working directory with the MagLev requirement in it. You will add any other Mindpowered packages to your requirements here, each as a separate line. requirements.txt:

mindpowered-maglev>0

Now you can use pip to install the MagLev package: python3 -m pip install -r requirements.txt

How to Use MagLev(Python)

Below is an example of how to use MagLev in Python:

import maglev


bus = maglev.maglev_MagLevPy.getInstance("default")


def myfunc(args):

	return "Hello, " + args[0] + "!"


bus.register("myfunc", myfunc);


print(bus.call("myfunc", ["World"]))


def myhandler(ev, args):

	print("Hello from " + args[0])


bus.listen("myevent", myhandler)

bus.emit("myevent", ["a friend"])
Hello, World!
Hello from a friend

Special Notes

You may only pass primitive data types as functions e.g. null, string, ?object(map/hash)?, array, number.

List of Packages

Package Name Hosted Language Description
maglev - all Provides internal communication between libraries
mongodb-persistence no js Use mongodb for persistent data
sqlalchemy-persistence no python Use SQLite, Postgresql, MySQL, Oracle, MS-SQL, Firebird, Sybase and others for persistent data
sql-persistence no php Use MySQL, MSSQL, SQLite, MariaDB, PostgreSQL, Sybase, Oracle and more for persistent data
sqlite-persistence no c++ Use SQLite for persistent data.
english-auction yes all A system for holding auctions - English auctions are the most common form of auction
ip-tools yes all Tools for dealing with IP addresses and Subnets
car-loan-calculator yes all Calculator for car loans including taxes and trade-in value
color-converter yes all Convert between various color systems
invoices yes all Logic for dealing with invoices
reserved-seating yes all Logic for Seating Reservations