{"id":24019,"date":"2024-04-06T14:06:33","date_gmt":"2024-04-06T13:06:33","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/compiler-passes-en-symfony-optimizando-tu-aplicacion\/"},"modified":"2024-06-03T18:40:57","modified_gmt":"2024-06-03T17:40:57","slug":"compiler-passes-en-symfony-optimizando-tu-aplicacion","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/php\/compiler-passes-in-symfony-optimizing-your-application\/","title":{"rendered":"Compiler Passes in Symfony: Optimizing your Application"},"content":{"rendered":"<p>When developing applications with Symfony, there are various tools and techniques that allow us to optimize the performance and efficiency of our application. One of these techniques is the use of Compiler Passes, which give us the ability to customize and modify the behavior of the Symfony service container during the compilation phase.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/php\/compiler-passes-in-symfony-optimizing-your-application\/#%C2%BFQue_son_los_Compiler_Passes_en_Symfony\" >What are Compiler Passes in Symfony?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/php\/compiler-passes-in-symfony-optimizing-your-application\/#%C2%BFPor_que_utilizar_Compiler_Passes_en_Symfony\" >Why use Compiler Passes in Symfony?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/php\/compiler-passes-in-symfony-optimizing-your-application\/#Implementando_Compiler_Passes_en_Symfony\" >Implementing Compiler Passes in Symfony<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/php\/compiler-passes-in-symfony-optimizing-your-application\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/php\/compiler-passes-in-symfony-optimizing-your-application\/#Preguntas_Frecuentes\" >Frequent questions<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/php\/compiler-passes-in-symfony-optimizing-your-application\/#%C2%BFComo_puedo_utilizar_Compiler_Passes_para_agregar_un_nuevo_servicio_a_mi_aplicacion_Symfony\" >How can I use Compiler Passes to add a new service to my Symfony application?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/php\/compiler-passes-in-symfony-optimizing-your-application\/#%C2%BFPuedo_utilizar_varios_Compiler_Passes_en_mi_aplicacion_Symfony\" >Can I use multiple Compiler Passes in my Symfony application?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/php\/compiler-passes-in-symfony-optimizing-your-application\/#%C2%BFEs_posible_modificar_las_definiciones_de_servicios_existentes_utilizando_Compiler_Passes\" >Is it possible to modify existing service definitions using Compiler Passes?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/php\/compiler-passes-in-symfony-optimizing-your-application\/#%C2%BFQue_sucede_si_tengo_conflicto_entre_dos_Compiler_Passes\" >What happens if I have a conflict between two Compiler Passes?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_son_los_Compiler_Passes_en_Symfony\"><\/span>What are Compiler Passes in Symfony?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Compiler Passes are a Symfony feature that allows us to make modifications to the application&#039;s service container before its compilation is completed. During compilation, Symfony collects all service definitions in memory and optimizes them for better performance. Compiler Passes allow us to intervene in this process and make specific customizations.<\/p>\n<p>A Compiler Pass is implemented by a class that implements the interface <code>CompilerPassInterface<\/code>. This class must define the method <code>process()<\/code>, which will be called by the Symfony service container during the build phase. Within this method, we can access all service definitions and make any modifications we need.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFPor_que_utilizar_Compiler_Passes_en_Symfony\"><\/span>Why use Compiler Passes in Symfony?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Using Compiler Passes in Symfony gives us a series of benefits and advantages. Below I will mention some of the most common use cases:<\/p>\n<p><strong>Modify settings:<\/strong> Compiler Passes allow us to modify configurations in the service container before they are compiled. For example, we can change the value of a parameter or add and delete service definitions according to our needs.<\/p>\n<p><strong>Add features:<\/strong> Using Compiler Passes, we can add new functionality to our application. For example, we can register and configure additional services, subscribe to container-specific events, or even alter the execution flow of the application.<\/p>\n<p><strong>Optimize performance:<\/strong> Compiler Passes also allow us to perform optimizations related to the performance of our application. We can eliminate unnecessary services, replace services with optimized versions or even apply caching techniques to speed up the application&#039;s response time.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Implementando_Compiler_Passes_en_Symfony\"><\/span>Implementing Compiler Passes in Symfony<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To implement a Compiler Pass in Symfony, we must first create a class that implements the interface <code>CompilerPassInterface<\/code>. This class must define the method <code>process()<\/code>, where we will make the desired modifications to the service container.<\/p>\n<pre><code>use SymfonyComponentDependencyInjectionCompilerCompilerPassInterface; use SymfonyComponentDependencyInjectionContainerBuilder; class CustomCompilerPass implements CompilerPassInterface { public function process(ContainerBuilder $container) { \/\/ Here we make the modifications to the service container } }\n<\/code><\/pre>\n<p>Once we have defined our Compiler Pass class, we need to register it in the file <code>services.yaml<\/code> of our application. We will use the key <code>compiler_passes<\/code> to indicate the Compiler Passes that we want to use:<\/p>\n<pre><code>services: _defaults: # ... compiler_passes: - AppCustomCompilerPass\n<\/code><\/pre>\n<p>Finally, when we execute the command <code>php bin\/console cache:clear<\/code>, Symfony will automatically call the method <code>process()<\/code> of all registered Compiler Passes, allowing us to modify the service container before its final compilation.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Compiler Passes in Symfony are a powerful tool that allows us to customize and optimize the service container of our application. Through them, we can modify settings, add new functionalities and optimize the performance of our application. By knowing and using Compiler Passes properly, we can improve the efficiency and quality of our Symfony applications.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Preguntas_Frecuentes\"><\/span>Frequent questions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFComo_puedo_utilizar_Compiler_Passes_para_agregar_un_nuevo_servicio_a_mi_aplicacion_Symfony\"><\/span>How can I use Compiler Passes to add a new service to my Symfony application?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To add a new service to your Symfony application using Compiler Passes, you must first define the configuration of that service in your configuration file (for example, services.yaml). You can then implement a Compiler Pass that registers and configures that service in the service container. Inside the method <code>process()<\/code> from your Compiler Pass, use the object <code>$container<\/code> to add your service definition to the container.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFPuedo_utilizar_varios_Compiler_Passes_en_mi_aplicacion_Symfony\"><\/span>Can I use multiple Compiler Passes in my Symfony application?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes, you can use multiple Compiler Passes in your Symfony application. To do this, simply register all the Compiler Pass classes you want to use in your configuration file. Note that the order in which Compiler Passes will be called depends on the order in which they are registered in the configuration file.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFEs_posible_modificar_las_definiciones_de_servicios_existentes_utilizando_Compiler_Passes\"><\/span>Is it possible to modify existing service definitions using Compiler Passes?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Yes, it is possible to modify existing service definitions using Compiler Passes. Inside the method <code>process()<\/code> from your Compiler Pass, you can access existing service definitions using the object <code>$container<\/code>. Modify the properties or attributes of the definitions as necessary to make the desired modifications.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%C2%BFQue_sucede_si_tengo_conflicto_entre_dos_Compiler_Passes\"><\/span>What happens if I have a conflict between two Compiler Passes?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>If you have a conflict between two Compiler Passes, that is, if they both make modifications to the same service or configuration, unwanted behavior can occur. To avoid these types of conflicts, make sure that the Compiler Passes are designed in a modular way and that they do not directly depend on each other. In case of conflict, you can reorder the Compiler Passes in the configuration file to establish a specific execution order.<\/p>","protected":false},"excerpt":{"rendered":"<p>En el desarrollo de aplicaciones con Symfony, existen diversas herramientas y t\u00e9cnicas que nos permiten optimizar el rendimiento y la eficiencia de nuestra aplicaci\u00f3n. Una de estas t\u00e9cnicas es el uso de Compiler Passes, que nos brindan la capacidad de personalizar y modificar el comportamiento del contenedor de servicios de Symfony durante la fase de [&hellip;]<\/p>","protected":false},"author":1,"featured_media":24020,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2206,420,16],"tags":[874,205,1174,851,1173,15,101,583],"class_list":["post-24019","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-blog","category-programacion","tag-aplicacion","tag-blog","tag-compiler","tag-optimizando","tag-passes","tag-php","tag-programacion","tag-symfony"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/24019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=24019"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/24019\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/24020"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=24019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=24019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=24019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}