How to use the decoupled symfony components? - symfony

Any information on how to use symfony's decoupled components?
I'm rereading the docs but there's nothing on the topic besides a general message of "They are very very decoupled" and 1 tutorial that makes use of Request and Response.
There's also one badly ranked answer of Using symfony2 routing component (outside of symfony2)
Also having a look at a tutorial for the standalone Form component doesn't actually excite me how pleasant this is.
I need the routing, yaml, and session.

The first component you should use is ClassLoader. You can also use spl_autoload_register, but you're using Symfony, so why shouldn't you use its own autoloading library? Add the following at the top of the script:
use Symfony\Component\ClassLoader\UniversalClassLoader;
require_once '/path/to/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php';
$loader = new UniversalClassLoader();
$loader->register();
$loader->registerNamespaces(array(
'Symfony' => '/path/to/symfony/src',
));
Using the Yaml component is really easy:
use Symfony\Component\Yaml\Parser;
$data = Parser::parse('yaml string');
For the other components, you'll have to read the API documentation, as there are no tutorials yet.

Interestingly, Fabien Potencier just published a blog post which contains snippets of how to use the most common components. See the second half of this post for details.

I've written a tutorial which might help you, on using decoupled Symfony components in your project.
It shows how to use the console component as an example, but the logic is the same for other components.

Composer is the answer.
This video here http://www.youtube.com/watch?v=QOldVDVYnAE has a simple and straight forward step by step that answers your question.
EDIT on august 26th 2020: The video creator has made it private. Sorry, nothing I can do about it.

Symfony2 example
1) Install the component You need with composer in new folder
composer require symfony/yaml
2) Create the script yaml.php
<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\Yaml\Parser;
$yaml = new Parser();
$value = $yaml->parse('invoice: 3484');
var_dump($value);
3) Run the script
php yaml.php

Related

reading yaml from twig

Preface: in ez4 i remember there was a tpl function to read ini settings, we used to use this to pass specific locations or id's with which we could then render certain content.
In ezplatform I am now doing the same thing but by using the PreContentViewListener (in the PreContentViewListener read a yml file and pass into the view as params), but this doesn't feel like the correct way as the PreContentViewListener doesn't always get triggered, in custom controllers for example.
Question
Is there a native way to read yaml files from within twig templates? After searching the docs and available packagists i cannot find anything :/
If your needs are simple (i.e. reading container parameters), you can also use eZ Publish config resolver component which is available in any Twig template with ezpublish.configResolver.
You can specify a siteaccess aware parameter in format <namespace>.<scope>.<param_name>, like this:
parameters:
app.default.param.name: 'Default param value'
app.eng.param.name: 'English param value'
app.cro.param.name: 'Croatian param value'
where default, eng and cro are different eZ Publish scopes.
You can then use the config resolver to fetch the parameter in current scope with:
{{ ezpublish.configResolver.parameter('param.name', 'app') }}
If you have Legacy Bridge installed, this even falls back to legacy INI settings if no Symfony container parameter exists:
{{ ezpublish.configResolver.parameter('SiteSettings.SiteName', 'site') }}
Disclaimer: Some say that using config resolver is bad practice, but for simpler usecases it is okay, IMO.
Have a look to our CjwPublishToolsBundle.
https://github.com/cjw-network/CjwPublishToolsBundle
https://github.com/cjw-network/CjwPublishToolsBundle/blob/master/Services/TwigConfigFunctionsService.php
Here we have 2 wrapper twig functions
{{cjw_config_resolver_get_parameter ( 'yamlvariablename', 'namespace default ezsettings') }}
=> ezpublish siteaccessmatching
{{cjw_config_get_parameter( 'mailer_transport' )}}
=> core symfony yaml reader without siteaccess
You could do a lot of things in eZ 4 and not always really good for your application design. ezini was able to read the configuration from the template but now in eZ Platform and by extension Symfony you need to respect more common patterns. IMO the view should not be that smart.
Then injecting variables to the view from a listener (PreContentViewListener or your own) is not a bad idea.
You can also use the Twig Globals that could allow you to do 2 global things:
inject variables (1)
inject a service (2)
Look here: https://symfony.com/doc/current/templating/global_variables.html
(2): please don't inject the service container globally it is bad
(1): I don't remember if the Twig Globals are Site Access aware, if not injecting your own service (2) to manage access to the config might be better.
And finally, I think that the use case is not a common one:
we used to use this to pass specific locations or id's with which we could then render certain content.
Most of the time it is a bad idea to pass ids coming from the configuration to render something, it is much better to organize the content structure to let you pull the location you want using the PHP API. (no id in configuration no hassle with dev, stage, preprod and prod architecture)

Active Model Serializer not render my models

I am trying to integrate Active Model Serializer to render JSON elements with relations.
I follow the documentation on this address: http://rubydoc.info/gems/active_model_serializers
I am not sure if I am doing something wrong but it looks like serializers are not working. Do I need to make more steps?
I install the gem, generate the serializer and add relation.
Can you guide me, please??
My project is in this repo:
https://github.com/dwdsolutions/argo
Best Regards
You're using the 0.9.0 version. Try to change it to 0.8.0
Alex is right, I tried using v0.9.0 earlier and was unable to get it working per the existing documentation.
The main github repo does state to use v0.8.0 if you are familiar with the gem (https://github.com/rails-api/active_model_serializers/tree/master#maintenance-please-read). At any rate, once you get it working, you shouldn't need to manually specify your serializers since they're named properly compared to your models and controllers.
i.e. you can remove the trailing option of this line:
render json: #travel, serializer: TravelSerializer
And make it just:
render json: #travel

How to enable route security when using AngularFire with Angular ui-router?

Is it possible to use theAngularFire routeSecurity module with angular UI-ROUTER instead of the standard ng-route provider? Is there a version of routeSecurity that would work with ui-router?
#mattvv Gave me this gist while I was talking on him in the angular irc channel. So essentially you would just need to replace the routesecurity.js file in angularfire directory assuming that you used yeoman to scaffold your application.
A neat thing to do is just to create another file named routesecurity-ui-router.js instead of replacing the content of the routesecurity.js.
So to give a little bit of information about the gist, basically mattvv just modified the routes term and use state instead.

Best approach To include 3rd party files with Symfony2

I would like to know What is the best way to include 3rd party php files in symfony2. I am using a different php - ajax package for uploading files in my symfony2 application. The package offers me some php oops code which i need to use in my symfony controller. I am creating objects of that code in my controller. So i would like to know where i can put that third party code or file and how can i include or create objects of that code in my symfony2 controller. Do we use require or include in symfony2 as well. If So is that the only approach.
I'm not so sure about trying to add namespaces to a third party library. Twig, for example, does not use name spaces. And there really is no need. Consider for example a case where you want to use the PDF component from the Zend_Framework 1 library.
In your app/autoload.php file you would do something like:
$loader->registerPrefixes(array(
'Twig_Extensions_' => $ws . 'Symfony/vendor/twig-extensions/lib',
'Twig_' => $ws . 'Symfony/vendor/twig/lib',
'Zend_' => $ws . 'ZendFramework-1.0.0/library',
));
// And since Zend internally uses require/include we need to set an include path
ini_set('include_path','.' .
PATH_SEPARATOR . $ws . 'ZendFramework-1.0.0/library'
);
At this point we should be able to create 3rd part objects inside of controllers while letting the autoload system take care of finding and including the classes:
$page = new \Zend_Pdf_Page(\Zend_Pdf_Page::SIZE_A4);
$doc->pages[] = $page;
$font1 = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_HELVETICA);
$font2 = \Zend_Pdf_Font::fontWithName(\Zend_Pdf_Font::FONT_COURIER_BOLD);
You do have to use the \ to get around the lack of namespacing.
This answer does assume that your 3rd part library follows the more or less standard class naming convention. If it has it's own auto loading functionality then just call it from autolaod.php as well. And if you don't want to use autoloading at all then just set the include path and include away.
The documentation explains the directory structure in detail.
Basically, you can put them wherever you want, but for the sake of consistency and following best-practices, you should put your third-party libraries in vendor/ directory.
Than you can include the relevant files with namespaces.

Wordpress widget creation

%wordpress I've created to tool to turn php functions into Wordpress widgets. I use the register_sidebar_widget function but it seems like wp_register_sidebar_widget would give me more options. Is that legal?
P.S. I also generate widgets in the new 2.8 format.
register_sidebar_widget is deprecated--though it can still be used, but you could use wp_register_sidebar_widget. If you do use register_sidebar_widget might want to bracket it in an if ( function_exists('register_sidebar_widget') ) in case that function gets removed in subsequent versions.
Also, there is a plugin, Otto's PHP Code Widget, that allows PHP code in a widget.
http://codex.wordpress.org/WordPress_Widgets_Api/register_sidebar_widget
http://codex.wordpress.org/WordPress_Widgets_Api/wp_register_sidebar_widget
http://wordpress.org/extend/plugins/php-code-widget
Legal? Sure. WordPress is GPL, you can do anything you like as long as you follow the requirements of the license.

Resources