Symfony2: Extend php-sdk kit to custom bundle - php

Using Symfony2, Twig, and Prismic:
I have looked at the following resources, but I am still unclear how to extend certain methods in Symfony2
http://symfony.com/doc/current/cookbook/bundles/inheritance.html
http://symfony.com/doc/current/cookbook/bundles/override.html
There is a prismic folder in my vendors directory, which includes the following dirs:
vendors/prismic
php-sdk
prismic-bundle
For my application, I duplicated the prismic-bundle directory and moved it here:
src/VAP/Bundle/PrismicBundle
then changed the AppKernal.php to include this bundle:
// new Prismic\Bundle\PrismicBundle\PrismicBundle() ..removed the connection to the vendor dir
new VAP\Bundle\PrismicBundle\PrismicBundle() ..use this custom directory
which works fine.
However, there are methods in the php-sdk directory that are called from my custom PrismicBundle, which I need to extend or override. For instance, a twig template may call
var.getStructuredText('blog.body').asHtml(ctx.linkResolver)
which is located here:
vendor/prismic/php-sdk/src/Prismic/Fragment/StructuredText.php
How/where would I create a file that would extend/overwrite the above file?
I am also confused if php-sdk is a bundle, or is it part of the original PrismicBundle from the vendor directory?

First, you say that you duplicated the PrismicBundle in your src folder, why did you not use the bundle inheritence process ?
public function getParent()
{
return 'PrismicBundle';
}
For you question, you have to change the behavior of the prismic php-sdk, possible reasons:
1) This sdk is not well built
2) You do not use it correctly
3) You want a very custom behavior not supported by prismic
IMO, possible solutions are:
1) Fork the sdk to add compatibility to your need
2) Think about a different way to reach your need
3) Create a new service in your custom PrismicBundle that call prismic api as you want.
Faor your final question, afaik prismic/php-sdk is the api implementation in raw php, and prismic/prismic-bundle is a bridge between this raw php SDK and Symfony, implementing services and all comodities provided by Symfony.

Related

unable to find template, after generating bundle

ok this might be a very strange thing.
it's not the first time I work a symfony project but:
I used the symfony generate:bundle command and after that I created a bundle. Lets call it "CrimeBundle".
I saw it made a folder inside the src/
It also made automatically a DefaultController and an index.html.twig file.
Now whenever I use:
return $this->render('CrimeBundle:Default:index.html.twig');
it doesn't work: I get the error:
Unable to find template "CrimeBundle:Default:index.html.twig" (looked into: /Users/admin/sites/solve/app/Resources/views, /Users/admin/sites/solve/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form).
however it works whenever I use the namespaced twig path like:
return $this->render('#Crime/Default/index.html.twig');
I like the first option, because on my other projects I use it too. None of them are the same version, currently I use: 3.4.1
Again the file is there, because it works with namespaced twig paths.
I can't understand why return $this->render('CrimeBundle:Default:index.html.twig'); wouldn't work as symfony generated this code.
According to this - https://symfony.com/doc/3.4/templating.html#referencing-templates-in-a-bundle
That's the only reference type the support for bundle templates
#BundleName/directory/filename.html.twig
If you go to docs for symfony 3.1 you'll see that was the last version that supported old reference
AcmeBlogBundle:Blog:index.html.twig
Maybe you can create a pull request for this file
SensioGeneratorBundle/bundle/DefaultController.php.twig

Access to the base_path of a particular Asset directory from a controller

I want to access the base_path (base_url registered) of a specific Asset component directory from a controller in order to store my reports to a specific path preconfigured in config.yml.
I started changing my configuration, after upgrading to Symfony 2.7, like the following:
app/config/config.yml
framework:
assets:
version: 'v5'
version_format: '%%s?version=%%s'
base_path: ~
base_urls: ['http://cdn.example.com', 'https://secure.example.com']
packages:
reports:
base_path: bundles/mybundle
So, when I request a specific route, with the correct request parameters my controller generate the HTML from a particular Twig template and, at the end, it will be converted to PDF using KnpSnappyBundle.
At last, my purpose is to build a list of generated PDF reports accessible from a public assets directory.
$kernel->locateResource()
However, I can access the complete path using a workaround like the following:
$this->container->get('kernel')->locateResource('#MyBundle/Resources/public/reports')
Using parameters.yml
I have also asked for some hints and it seems legit to use the parameters.yml in order to manage the Asset component configuration. So, from the controller, they would be accessed using $this->getParameter() and, at the same time, as a configuration value for Asset.
The simplest way to deal with that is to define it as a parameter in parameters.yml, as you suggested yourself.
It's really easy to get it and it totally makes sense.
Update
I wanted to provide a bit more reasoning for my answer, so I will cite http://symfony.com/doc/current/best_practices/configuration.html as a reference.
Reading there, it seems that you should put into "parameters.yml" all infrastructure parameters which do not really change your application behaviours. I think this applies to your case as well: your application does not change its behaviour according to assets paths, it only needs to know where they are.
So, again, I'd say that putting them in parameters.yml not only provides you an easy solution but also it's a "good practice".

Symfony 1.4 - Doctrine - Custom Plugin - How do I get plugin model files to reside in plugin directory?

I am creating a custom plugin, and am trying to keep all related model files contained within the plugin directory structure. However, when I build the model, some files get dropped into lib/model/doctrine/... and others in plugins/userPlugin/lib/model/... . According to Doctrine docs I can add a "package" option to the schema.yml file, and generated model files will be created in the location as defined by my dot-notation entry, for example:
# plugins/userPlugin/config/doctrine/schema.yml
connection: store-rw-user
options:
# Fully expect resulting model files to be dropped in this directory (vs the main model dir)
package: userPlugin.lib.model.doctrine
....
As mentioned, this config setup still results in model files being dropped into the main lib/model/doctrine directory. I even tried this, to no avail:
# plugins/userPlugin/config/doctrine/schema.yml
connection: store-rw-user
options:
package: userPlugin
package_custom_path: /tmp/userPlugin
....
Just wanted to see if the files were dropped in the /tmp directory, but they were not.
Before I start tearing apart the source code, I figured I would ask first, to see if there is something I am missing.
It's perfectly normal to get model files in your project directory after building. The purpose of this is to let you customize the plugin model on per-project basis, because the classes inside these files inherit from the classes defined in the plugin's files. I use plugins too, and most of the time, all the code I write resides in the plugin's model files.

Symfony function available in all models

I want to use a function in all models class (in project folder and in plugins folder).
Where should I declare it?
Depending on what your function does, you can create a file in the lib folder and then call it from every where in your app. This is useful in a Symfony project to define common functions (like a toolbox).
For example, in the Jobeet tutorial, they define a method called slugify in /lib/Jobeet.class.php (be sure to name the file with .class.php at the end so Symfony will automatically load it). Then, you can call Jobeet::slugify() every where in your app/model/plugin/view.
This solution works with Symfony 1.4:
You create a new file in which you declare the function you want to be available everywhere.
You load that file with the auto prepend file settingin the php.ini file.
If done correctly, that function is available in all your scripts, regardless of model, plugin or something else from your project.

Using ProcessBuilder in a Silex Project

I am wanting to use the Symfony\Component\Process\ProcessBuilder class and can see that it is included as part of the Silex codebase within the vendors folder. I am using the Silex phar file and assume that because I can readily instantiate other Symfony components like Request, Response and so on that it will correctly locate the file to include when I use the full namespace.
$foo = new Symfony\Component\HttpFoundation\Request(); //works fine
However, when I try and create and instance of it using:
$foo = new Symfony\Component\Process\ProcessBuilder(); //class not found
It gives me a class not found error. Does anyone know why this is and how I can use this class from the Silex phar without including the component seperately within my project?
It looks like the Process Symfony component is not included in the compiled Silex phar file.

Categories