..


Sponsored Links

Filtering properties of our application in relation to the environment

Article written by Riccardo Brambilla
Page 1 of 4

We begin by explaining what we mean by dynamic filtering.
With this expression we refer to that mechanism to make available to our classes / scripts filtered common properties but in relation to the environment in which we are working (local, test, production).

It 'a common mechanism in Java but not in PHP for the simple reason that the concept of deployment, as commonly understood, is not associated with our beloved language. And 'in fact quite common test scripts locally and then copy the files via ftp on our test, or (worse) directly on that production.

In the workplace, however, is very handy to have filtered out automatically depending on properties that our application must run in localhost or on other machines (such as frontend, exposed to the outside), so in this article we will create a single framework to implement This kind of logic in a PHP project our hypothetical.
To do this we will not use external packages, or special abstractions we need is a XML, a PHP class and text files.

The idea is to define in the xml file to our common properties, create a file for each environment to enhance them and write some code that puts it all together. The structure of our test application will be:

  1. classes to a folder containing the class PropertyManager.class.php
  2. config with a folder inside the file and folder filters app_properties.xml
  3. the folder containing three files filters: local.filter.properties, test.filter.properties, pro.filter.properties
  4. a PHP file with the code for testing the operation of our solution: index.php

Here's the basic structure:

Structure such as filtering

The xml

Let's see how the file is made up:






 <? Xml version = "1.0" encoding = "utf-8"?>







 <properties>



  



 <property name="__app_version" value="0.0.1" />

	

  



 <property name="messaggio_variabile" value="" />



  



 <property name="url_servizio_esterno" value="" />



  



 <property name="local" value="localhost" />



  



 <property name="test" value="test.qualchedominio.it" />



  



 <property name="pro" value="prod.qualchedominio.it" />







 </ Properties>



Besides the usual XML tags define the document that starts a tag <properties> container into which we insert a set of tags <property>, the first three are the ones who will serve in the example, the last three are related to various host and serve PHP to understand what environment we are operating dynamically.

Here we deine the local environments, testing and production, but nothing prevents you define others, such as dev. For each of the rooms is shown a base URL, which value according to our requirements, application by application.
In our scenario we have a test machine to reach 'test.qualchedominio.it URL and a production well of course prod.qualchedominio.it reached at localhost.

The properties file .*

The next step is to make sure that the properties defined in XML are valued differently depending on the 'environment where we are. We use for this purpose of text files in the folder filters, let's see the contents one by one:

local.filter.properties






 ; [LOCAL] Filter properties







 messaggio_variabile = "We LOCAL"







 url_servizio_esterno = http://dev-servizio.esterno.it



test.filter.properties






 ; [TEST] Filter properties







 messaggio_variabile = "We TEST"







 url_servizio_esterno = http://test-servizio.esterno.it



pro.filter.properties






 ; [PRO] Filter properties







 messaggio_variabile = "We PRODUCTION"







 url_servizio_esterno = http://prod-servizio.esterno.itit



Note that the property "__app_version", defined in XML, it is not redefined in different files, it will be treated for all purposes as a common property among the various environments and unchanging, so will not be filtered.

In the same category ...
E-Learning
Linux (Course) Linux (Course)
Complete guide to open-source system. From 49 €.
MySQL (Course) MySQL (Course)
Management of open-source database. From 39 €.
PHP (Course) PHP (Course)
Full course for creating dynamic Web sites. From 49 €.
Sponsored Links