📜 ⬆️ ⬇️

Puli: Resource Management in php applications

image Puli is a tool that allows you to universally manage resources in php applications. The main goal of the project is to standardize such concepts as bundle, plugin, module for different libraries and frameworks into one common independent solution.

What is the problem?

Sometimes, you might have gotten a file using the constant __DIR__:
')
echo file_get_contents(__DIR__.'/../../res/views/index.html.twig'); 

However, as a rule, when finalizing a project, the path to the file changes, and this code stops working. But many frameworks use their own solution with a pre-installed root directory where your resources lie.

 // res/views/index.html.twig echo load_file('views/index/html.twig'); 

This solution works well. And what if you want to load resources from another directory, let's say from the library installed through the composer. The frameworks solve this problem by supporting aliases for resources.

 // vendor/acme/blog/res/views/index.html.twig echo load_file('acme-blog:views/index/html.twig'); 

Unfortunately, each framework has its own directory naming convention.

How Puli Works

The component for managing resources through repositories takes a path, just like in a Unix file system.

 // res/views/index.html.twig echo $repo->get('/app/views/index.html.twig')->getBody(); 

Puli finds files using puli.json at the root of your project and in all packages installed by the compositor. You can add a new path using the command:

 $ php puli.phar map /app res 

In this example, we have added the / app prefix for the res directory of your application. Now Puli knows that all files with the / app prefix must be found in the res directory.

image

Puli collects path data from your project puli.json.

URL generator

When you write html, css, javasript code, you often need other resources. For example, a link to a picture in the html img tag:

 <img src="/images/logo.png" /> 

Maybe everything will work, but if you go to store your images on another server? If your code is part of the composer package, you force users to publish your resources with accuracy, as in your html file.

Puli solves this problem by automatically generating a URL.

 <img src="{{ resource_url("/batman/blog/public/images/logo.png") }}" /> 

Relative path:

 <img src="{{ resource_url("../public/images/logo.png") }}" /> 

Users of your package need a little Puli setup.

image

Now the user must specify at least one web server (name and URL format). For example, use localhost as the name and examle.com/%s as the URL format for our web server. We set up our puli so that the path / batman / blog / public corresponded to the root directory / blog / of our web server. Get the URL:

 <img src="https://example.com/blog/images/logo.png" /> 

Puli can automatically host your resource for a web server. Having specified your web server and how your resources should be published (symlink, copy, rsync), you can install them using the cli command:

 $ php puli.phar publish --install Installing /batman/blog/public into public_html/blog via symlink... 

I think Puli would be a great solution for many module developers. Lead the project is one of the developers Symfony - Bernhard Schussek. At the moment, the version is puli 1.0.0-beta8. Now available:

Composer plugin
Symfony bidge
Twig extension
Symfony bundle
gulp-plugin

Sources:
docs.puli.io
github.com/puli
DrupalCon Barcelona 2015: Puli: PHP's Next Package Revolution

Source: https://habr.com/ru/post/269081/


All Articles