Code Monkey home page Code Monkey logo

fortune-php's Introduction

Reader for fortune files

Implementation of a reader for fortune files in PHP.

Usage

Retrieve random fortune
use vitoni\Fortunes;

$fortunes = Fortunes::from($path);

echo $fortunes->getRandom();
Use Fortunes as (read-only) array
use vitoni\Fortunes;

$fortunes = Fortunes::from($path);

$randOffset = $fortunes->getRandomOffset();

echo $fortunes[$randOffset];
Access using an Iterator
use vitoni\Fortunes;

$fortunes = Fortunes::read($path);

foreach ($fortunes as $fortune) {
    ...
}

Over-engineered example

Re-reading the fortune file / directory to find all fortunes on each call might be not the best idea. In case of a MOTD this might work but for a web page this approach might not fit.

One can use the indexer example to create a static index of all fortunes. This index can be used as long as the files don’t change (it could be used even then when one only appends to existing files with the downside of missing out on new fortunes).

Outputs a ready to use PHP script with Fortunes set up with a static index
php examples/indexer.php tests/_files
Static reader demo
php examples/indexer.php tests/_files \
  | cat - <(echo 'echo $fortunes->getRandom() . "\\n";') \
  | php

What the snippet does:

  • Creates a static index and prepares a Fortunes instance

  • Uses output from indexer and appends echo $fortunes→getRandom() . "\n";

  • Pipes everything to php to execute the created script to retrieve a random fortune

Output
This is fortune 4

Other approaches

One could created a static PHP readable version with the fortunes included, or something totally different.
But where would the fun be?

The original fortunes uses .dat files which do basically the same but have a per file index.

Development

This project uses composer for dependency management.

Install dependencies and create autoloader script
composer install
Start minimal web server on 127.0.0.1:8000
composer run-script dev
Execute tests (indirectly with composer)
composer run-script test
Execute tests (directly with phpunit)
./vendor/bin/phpunit

fortune-php's People

Contributors

vitoni avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.