Giter Site home page Giter Site logo

namshiutilitybundle's Introduction

NAMSHI | UtilityBundle

This bundle provides some basic, silly utilities that we use across our Symfony2 applications.

Installation

The bundle can be easily installed via composer ("namshi/utility-bundle": "dev-master").

More informations available on Packagist.

Then enable it in the AppKernel.php:

new Namshi\UtilityBundle\NamshiUtilityBundle(),

Doctrine CommaSeparatedList type

If you want to persist an array into the DB as a list of comma-separated strings (ie. to be able to execute FIND_IN_SET) you can simply use the Namshi\UtilityBundle\ORM\Type\CommaSeparatedList type.

Remember to register it inside Symfony2's config.yml:

doctrine:
    dbal:
        types:
            comma_separated_list: Namshi\UtilityBundle\ORM\Type\CommaSeparatedList

and then use it on your entities:

/**
 * @var array
 *
 * @ORM\Column(name="tags", type="comma_separated_list", nullable=true)
 */
protected $tags = array();

File serving with authentication

One of the utilities that you can take advantage of is file serving, mixing it with authentication.

Suppose you have a file, protected.txt, in /path/to/symfony2/data/protected.txt and you want only some users to be able to access it.

You just have to enable a route that tells the bundle which file to serve:

protected_file:
    pattern:  /protected.txt
    defaults: { _controller: NamshiUtilityBundle:Default:serveFile, file: protected.txt }

and define the file path in the parameters.yml:

namshi_utility.files.protected.txt: /path/to/symfony2/data/protected.txt

At that point you will be able to access the content of the file (it uses file_get_contents(), so don't try using this utility as a download manager).

How to restrict access to the file then? Simply use the built-in ACL system that you have in Symfony2: configure your security.yml for the path ^/protected.txt and you're done.

Currency conversion

Totally unrelated to Symfony2, but included here for (our) conveniency, there is a currency converter that accepts conversion rates and can be used to easily convert an amount from a currency to another one.

<?php

use Namshi\UtilityBundle\Currency\Converter;
use Namshi\UtilityBundle\Currency\Currency;
use Namshi\UtilityBundle\Exception\CurrencyNotFound;

$conversionRates = array(
  'EUR' => array(
    'USD' => 1.3,
  ),
  'USD' => array(
    'AED' => 4,
    'EUR' => 0.7,
  ),
);

$converter = new Converter($conversionRates);

try {
    echo $converter->convert(12, Currency::UNITED_STATES_DOLLAR, Currency::EURO)
} catch (CurrencyNotFound $e) {
    echo "Yo boss, can ya provide conversion rates here?";
}

namshiutilitybundle's People

Contributors

odino avatar

Watchers

James Cloos avatar Mohamad Hallal 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.