Structure for PHP project and use of the Eval() function

Asked

Viewed 362 times

4

I need to develop an application that fits several projects with common functionalities, however each has several specific rules.

What I need is a structure that allows me to easily change these rules, in case I chose to save the rules in the database and use the function eval() to execute the code in the application, however, I don’t think this would be the best option to get the result I need (nor the most secure, even if the rules are encrypted).

Structure I’m using now is:

app
    controller [controllers]
    model [models] [aqui são aplicados as regras do banco de dados]
    entity [entidades do banco de dados]
    repository [model <=> entity]
    helper [helpers]
    view [views]
    theme [themes]
config
    [configurações da aplicação]
data
    logs [app logs]
    cache [view cache]
public 
    [módulos angular, assets, index.php e .htaccess]
vendor
    [libraries, app-core]

The rules would be applied more or less like this:

// Random key (config)
define('PRIVATE_KEY', 'SECURE_RANDOM_KEY');

// antes

foreach ( regras as regra )
    parse(decode(regra, PRIVATE_KEY));

// continua execução

Any idea how I could improve this?

  • 1

    Run of eval or preg_replace modifier e. Not a good idea anyway

  • the structure is nice. I just advise to include a folder "lib" or "Libraries" inside the folder "app". You have "Ibraries" for "vendor", but you need to have your Ibraries for the app engine. Another one that I think is important is "override". Inside this folder would have the same structure as the folders "app" and "config". Override is to allow the programmer to customize native functions without having to modify the original files. Roughly speaking, override, is an organized "gambiarra".

1 answer

1

Why don’t you use classes? For example:

interface Regras {
    public function execute();
}

class RegrasApp implements Regras {
    public function execute() {
        // suas regras aqui
    }
}

$regras = new RegrasApp();
$regras->execute();
  • Where would these classes be? This structure should be able to support multiple projects (10+), as I could manage the rules of each project?

  • Where are the classes? Where do you find them most suitable.. Each project would have its own class containing the business rules that would be executed by the method execute where you just need to instantiate the correct object.

  • Would it be interesting if I used __invoke() invocable classes and called the rules one by one using a Splstack?

  • I wouldn’t advise using the __invoke. It’s much simpler for someone who doesn’t know your code to understand how it works from calling a method.

  • One would use something like this: $filter->Register('filters.product', Function()ː }); as callback.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.