PHPixie / Docs

BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Absent documentation about configuration #3

Open SmiSoft opened 6 years ago

SmiSoft commented 6 years ago

As for me, configuration retreiving is the hardest thing in PHPixie to understand. Documentation about configuration absent, so any analysis can be performed only by source code. Examples:

Local config

I call "local", because this configuration settings are specific to bungle. All example pieces of code ececuted in action (HTTPProcessor). For example, this data will be read from bundles\app\assets\config\pony.php:

$value=$this->builder->bundleConfig()->get('pony.princess')

Or better way to receive two or more values:

$cfg=$this->builder->bundleConfig()->slice('pony');
$value=$cfg->get('princess');

Ok, but this method is not described in documentation. I wanted to use it as i18n tool, retreiving translation strings.

Global config

I call "global", because this configuration settings are common for all bundles. Again, all pieces of code are running in scope of action (HTTPProcessor). For example, this data will be read from assets\config\pony.php

$value=$this->builder->frameworkbuilder()->configuration()->config()->get('pony.princess');

Or better way to receive two or more values

$cfg=$this->builder->frameworkbuilder()->configuration()->config()->slice('pony');
$value=$cfg->get('princess');

But this code is ugly! This $this->builder->frameworkbuilder()->configuration()->config() of anomalous length? It was better to create shortcut, $this->builder->appConfig()->get(...). For myself, I, certainly, did it, here is a solution:

// bundles/app/src/HTTP/Processor.php
    public function __get($name){
        if($name==='bundleConfig')
            return $this->builder->bundleConfig();
        if($name==='appConfig')
            return $this->builder->frameworkbuilder()->configuration()->config();
        if($name==='frameworkBuilder')
            return $this->builder->frameworkBuilder();
        $components=$this->builder->components();
        if(method_exists($components,$name))
            return call_user_func(array($components, $name)); 
        $trace = debug_backtrace();
        trigger_error(
            'Неопределенное свойство в '.get_class($this).': ' . $name .
            ' в файле ' . $trace[0]['file'] .
            ' на строке ' . $trace[0]['line'],
            E_USER_NOTICE
        );
        return null;
    } 

After this trick, I can get configuration and components like this:

$orm=$this->orm;
$bundleValue=$this->bundleConfig->get('pony.princess');
$appValue=$this->appConfig->get('pony.princess');

So, the pony solution is more beautiful, than pixie, isn't it?

Override configuration

Another mess with configuration in PHPixie. Is there a shortcut method to get hierarchy config data? For example: I wish to create a forum bundle. I put content of this bundle into bundles/forum folder, add link to it into src/Project/Framework/Bundles.php and wish to configure it. A forum has local config, located in bundles/forum/assets/config/forum.php, it contains "default" values, like default page title, default banner and so on. A user may wants to override some of data, like default banner (maybe, depending on selected language or moon's phase). This is like an overlay, but target bundle (forum) shouldn't know about it. This behaviour was default in Kohana framework: core-specific values can be overridden by module-specific values (the same as bundle) and both of them can be overridden by application-specific values. This works for i18n too. But the great disappointment was that Kohana reads all configuration files at startup and it wasn't very fast. But there were one great advantage: you may see EVERY configuration option in file, copy them into application/config folder and change everything you want (or delete values, that you didn't plan to change, they will be read automatically). Has PHPixie such convinient instrument? Configuration overlay is slightly different, they mush be merged manually, not automatic.

Assets

Some strange things happens. As I thought, assets is parts of the project, which are not a code (images, css, templates, config and so on). So, the location of such files is very important. But the documentation has many lacks. For example, in bundles/app/assets/config/templateLocator.php, there is a line:

'directory' => 'templates',

But there are no instruction, why I should use EXACTLY this type of code? Why this:

'directory' => realpath(__DIR__.'../templates'),

will lead to error, because engine cannot find template? This is absolute path to template folder, so it should work perfect! OK, return to our assets. A code:

$path=$this->builder->assetsRoot()->path();

is not described in documentation, gives us full path to bundles\app/assets/ - good. But if I want to receive global assets, the same code lead to a fail:

$path=$this->builder->frameworkbuilder()->assets()->frameworkAssetsRoot()->path('');

This retreive invalid path: vendor\phpixie\framework\assets/. I wish simple path to /assets.

dracony commented 6 years ago

did you see this?: https://habrahabr.ru/post/301152/

It should answer some questions. As for what "assets" is in code it's just the location of the /assets folder which is non-code part of your project (e.g. config). TemplateLocator is configured rlative to the assets folder so that's why you are getting an error. To override the entire path you would need to extend the Configuration class of the framework itself.

frameworkAssetsRoot is the assets folder of the framework bundle. It's not global assets, just the assets specific to the "framework" package

SmiSoft commented 6 years ago

did you see this?:

Yes, I do. More, I've read Updates to configuration..., but it was even more confusing. The main pitfalls:

Hmm, maybe, it's worth to test path? Something like IsAbsolutePath?

frameworkAssetsRoot is the assets folder of the framework bundle

Ok, how to get path to /assets folder "right way"? And why in this case "$this->builder->frameworkbuilder()->configuration()" gets application-specific configuration, but "$this->builder->frameworkbuilder()->assets()" gets framework-specific assets? By the way, the PHPixie 2.0 architecture was very like to another PHP framework, Kohana. Modules, hierarcial configuration and file system, even facades Cache, DB and so on - are the same. Most likely, this is an accident. For me, such HMVC architecture is very convinient, but quite slow.