composer - dynamically set parameters variable - php

I have the following setup:
symfony 2.7 classic structure
composer for dependency management
What I need to do is set a variable in parameters.yml with the timestamp when composer was ran.
For this I tried the following solution:
parameters.yml.dist
[bla bla bla]
ran_timestamp: ~
composer.json
[bla bla bla]
"scripts": {
"pre-install-cmd": [
"export SYMFONY_APP_DATE=$(date +\"%s\")"
],
}
"extra": {
"incenteev-parameters": {
"file": "app/config/parameters.yml",
"env-map": {
"ran_timestamp": "SYMFONY_APP_DATE"
}
}
}
The part where the variable is set inside parameters.yml works fine (the parameter is created with the value from SYMFONY_APP_DATE env variable).
The problem is that the env variable is not updated when composer is ran. Can anyone help me with that pls?
Additional info:
If I run the command from pre-install-cmd in cli by hand it works fine (so command itself I think is ok)
I see the command being run in composer after it starts install so I think it is executed (output below):
$composer install
export SYMFONY_APP_DATE=$(date +"%s")
Loading composer repositories with package information [bla bla bla]
No errors are reported
I'm assuming maybe composer doesn't have rights to set env variables? - nope, it isn't this. It is related with variable scope.

The problem apparently is that you're setting env parameter in child process (which is created for each script), but it's not possible to redefine env parameter for parent process from child (i.e. to set env value for composer itself from one of its scripts)
I think you need to extend \Incenteev\ParameterHandler\ScriptHandler::buildParameters to make it happen.
UPD: I've found a way to make it happen
Define a special block only for build-params in composer.json
"scripts": {
"build-params": [
"Incenteev\\ParameterHandler\\ScriptHandler::buildParameters"
],
and than in post-install-cmd block instead of Incenteev\\ParameterHandler\\ScriptHandler::buildParameters make it
"export SYMFONY_APP_DATE=$(date +\"%s\") && composer run-script build-params"
That will create env var and building parameteres in same process

Related

What does the "key: value" syntax do in composer scripts

From compser documentation you can write scripts like so:
{
"scripts": {
"some-event": [
"command1",
"command2",
"command3"
],
}
}
But in Symfony I found a slightly different syntax that instead of a list is using a key:value pair like this:
{
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
},
}
How does this syntax work exactly?
Does it redirect commands to another one, adds a dependency between 2 commands or what?
How does this syntax work exactly?
This syntax is undefined in Composer.
To learn more, remove it and then check which component(s) break(s). Then consult the support of that/these component(s) to learn more (and/or leave a comment here).
Does it redirect commands to another one, adds a dependency between 2 commands or what?
As written this is undefined. As far as you're concerned about Composer you can check if it is recognized as a script:
composer run-script --list
You'll likely see that auto-scripts is listed as a script, that is, Composer is recognizing at least the auto-scripts keyword.
You can then execute that script to see how Composer executes it (it is highly recommended you do this and the following in an isolated environment):
composer run-script auto-scripts
To see if a plugin would make a difference, you can also execute it with plugins disabled:
composer --no-plugins run-script auto-scripts
Take care.
And with Composer there is always -vvv.
Some notes on internal Composer configuration file handling: JSON Text files (.json, .lock) in Composer are typically parsed into associative arrays (not stdClass::class objects).
Therefore foreach'ing a script member - which can be a string as well, likely post-processed with an (array) cast - would not care about the key (attribute name), just executing the scripts lines.
This syntax is used by symfony/flex plugin and is only valid under auto-scripts key.
The code in flex looks like this:
$json = new JsonFile(Factory::getComposerFile());
$jsonContents = $json->read();
$executor = new ScriptExecutor($this->composer, $this->io, $this->options);
foreach ($jsonContents['scripts']['auto-scripts'] as $cmd => $type) {
$executor->execute($type, $cmd);
}
With execute being handled using this code:
switch ($type) {
case 'symfony-cmd':
return $this->expandSymfonyCmd($cmd);
case 'php-script':
return $this->expandPhpScript($cmd);
case 'script':
return $cmd;
default:
throw new \InvalidArgumentException();
}

Add new environment variables to .env when installing a package

In our company, several internal projects rely on the same copied code parts to handle connections to the same APIs (like: Google Suite, JIRA,...). To avoid copying the same code over and over again for new projects, I want to create Symfony packages that collect these API classes.
The tricky part: I'm looking for a way to add the neccessary env variables automatically to .env, just like Symfony's recipe structure does it. But as these projects should only be used internally, pushing their recipe configuration to a public repository is a no-go for me. Adding a custom recipe server (like the one by moay) looks interesting to me, but needs additional configuration in each projects composer.json.
Is there any better way to resolve this, such that I could simply define the needed variables solely in my project, such that they get added to .env without any additional magic?
NB: anything that requires symfony/flex is fine, as this should be part of all new projects in our company
These are solutions I want to avoid:
add configuration to bundles / packages itself, such that these configuration values are put under version control
add configuration through any other command that is run manually after installing
You can use composer events for this process. After the package is installed, you add it to the .env file with a symfony command.
https://getcomposer.org/doc/articles/scripts.md
Composer unable to run post install script
There is a sample in the symfony composer.json file.
...
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"#auto-scripts"
],
"post-update-cmd": [
"#auto-scripts"
]
},
...
After each update or installation, this command is run "post-update-cmd, post-install-cmd".
In Symfony, the .env file is now committed into the repository. So It's not a good practice to put sensitive data on it.
A better solution is to create a file name .env.local. This file is not committed and it overrides all environnement value in .env
so you could have in .env
MY_SENSITIVE_DATA=mypersonalkey
and in your .env.local
MY_SENSITIVE_DATA=the_real_sensitive_data
source: https://symfony.com/doc/current/configuration.html#overriding-environment-values-via-env-local

How write a Symfony Flex recipe for a new bundle?

I tried to find any documentation about using Symfony Flex but so far no luck.
Almost all docs point to installing a bundle that uses symfony Flex, not how to create a bundle that is using it.
I even tried to reverse engineer some of the packages but again, no luck.
My goal is to generate a default configuration file for my bundle in config/packages/my_bundle.yaml.
What I need to know is where do I need to put it and what env variables (if any) will I have available?
What is a Flex Recipe?
Keep in mind that the flex recipe is a separate repository from your package repository, that needs to be hosted separately from the Bundle package.
In the most likely scenario that your is a public bundle/recipe, you'll have to submit your recipe to the "contrib" repository, get it approved and merged, so it's available as a community recipe.
Additionally, it's important to remember that most users will not have the contrib repository enabled by default. So if this is important for installing this bundle, you should tell your users how to do so before they install your recipe (e.g. in your bundle's readme file).
Private Recipes
The other option would be having a private Flex recipe, as described here. The easiest way to generate a private recipe is to follow the same steps that Symfony does. Check this question and its answers for more details: How to generate a private recipe JSON from the contents of a recipe directory?
With that out of the way: Basically, a Flex recipe is a repository with a manifest.json file with specific keys to enable certain "configurators".
The available manifest.json configurators are:
Bundles
Which bundles should be enabled on bundles.php. These are added when the recipe is installed, and removed when the recipe is uninstalled.
{
"bundles": {
"Symfony\\Bundle\\DebugBundle\\DebugBundle": ["dev", "test"],
"Symfony\\Bundle\\MonologBundle\\MonologBundle": ["all"]
}
}
Configuration
The "configuration" configurator deals with two keys: copy-from-recipe and copy-from-package. The first one can copy files from the recipe repository, the second one copies files from the package repository.
{
"copy-from-package": {
"bin/check.php": "%BIN_DIR%/check.php"
},
"copy-from-recipe": {
"config/": "%CONFIG_DIR%/",
"src/": "%SRC_DIR%/"
}
}
In this example, a file bin/check.php in the package will be copied to the projects %BIN_DIR%, and the contents of config and src on the recipe package will be copied the corresponding directory.
This is the typical use case to provide default configuration files, for example. From what you ask, this is your stated purpose for wanting to create a flex recipe.
Env Vars
This configurator simply adds the appropriate environment variable values to the project's .env and .env.dist. (Again, these would be removed if you uninstalled the recipe)
{
"env": {
"APP_ENV": "dev",
"APP_DEBUG": "1"
}
}
Composer Scripts
This configurator adds tasks to the scripts:auto-scripts array from the project's composer.json. The auto-scripts are tasks that are executed every time composer update or composer install are executed in the project.
{
"composer-scripts": {
"vendor/bin/security-checker security:check": "php-script",
"make cache-warmup": "script",
"assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
}
}
The second part on each line specifies what kind of command it is: a regular PHP script (php-script), a shell script (script), or a Symfony command (symfony-cmd, executed via bin/console).
Gitignore
This will add entries to the project's .gitignore file.
{
"gitignore": [
"/phpunit.xml"
]
}
A complete example of a manifest.json (lifted from here, as most other examples on this post):
{
"bundles": {
"Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle": ["all"]
},
"copy-from-recipe": {
"config/": "%CONFIG_DIR%/",
"public/": "%PUBLIC_DIR%/",
"src/": "%SRC_DIR%/"
},
"composer-scripts": {
"make cache-warmup": "script",
"assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
},
"env": {
"APP_ENV": "dev",
"APP_DEBUG": "1",
"APP_SECRET": "%generate(secret)%"
},
"gitignore": [
".env",
"/public/bundles/",
"/var/",
"/vendor/"
]
}
Additional configurators
There are two configurators which do not rely on the manifest.json file:
Post-install output.
If a file named post-install.txt exists in the recipe's package, its content is displayed when installation is complete. You can even use styles as defined here, for additional prettiness/obnoxiousness.
Example:
<bg=green;fg=white> </>
<bg=green;fg=white> Much success!! </>
<bg=green;fg=white> </>
* <fg=yellow>Next steps:</>
1. Foo
2. <comment>bar</>;
3. Baz <comment>https://example.com/</>.
This will be presented to the user after the installation is complete.
Makefile
If a file named Makefile exists in the recipe's repository, the tasks defined here would be added to the project's Makefile (creating the Makefile if it didn't exist).
cache-clear:
#test -f bin/console && bin/console cache:clear --no-warmup || rm -rf var/cache/*
.PHONY: cache-clear
Simple as that. I guess than most packages would not need a makefile command, so this would have much less use than other configurators.
You can read the full documentation here.

How to force Composer script to load local class instead of global class

undefined method
(Relevant files linked at the bottom of my question.)
I let Composer run some post-install-cmd and post-update-cmd scripts. In my script I want to make use of the readlink() function from symfony/filesystem. Inside my projects /vendor folder there is the 3.4 version of the filesystem package, fine.
I use Symfony\Component\Filesystem\Filesystem; at the top of my file.
But whenever I run:
$fs = new Filesystem();
$path = '/path/to/some/symlink';
if ($fs->readlink($path)) {
// code
}
I get the following error which tells me I'm calling an undefined method:
PHP Fatal error: Uncaught Error: Call to undefined method
Symfony\Component\Filesystem\Filesystem::readlink() in
/Users/leymannx/Sites/blog/scripts/composer/ScriptHandler.php:160
OK, so I double-checked the class inside my project's /vendor folder. This method is there. My IDE points me there. But when I run:
$fs = new Filesystem();
get_class_methods($fs);
this method is not listed.
Which file is it trying to load the method from?
OK, so I tried to check which file it's loading this class from:
$fs = new Filesystem();
$a = new \ReflectionClass($fs);
echo $a->getFileName();
and that returns me phar:///usr/local/Cellar/composer/1.7.2/bin/composer/vendor/symfony/filesystem/Filesystem.php – But why? Why is it taking the package from my Mac's Cellar? That's odd.
But OK, so I thought that's a Homebrew issue, and uninstalled the Homebrew Composer $ brew uninstall --force composer and installed it again as PHAR like documented on https://getcomposer.org/doc/00-intro.md#globally.
But now it's the same.
$fs = new Filesystem();
$a = new \ReflectionClass($fs);
echo $a->getFileName();
returns me phar:///usr/local/bin/composer/vendor/symfony/filesystem/Filesystem.php.
But why? Why does it pick up the (outdated) package from my global Composer installation? How can I force my script to use the project's local class and not the one from my global Composer installation?
What else?
Initially my $PATH contained /Users/leymannx/.composer/vendor/bin /usr/local/bin /usr/bin /bin /usr/sbin /sbin. I removed /Users/leymannx/.composer/vendor/bin to only return /usr/local/bin /usr/bin /bin /usr/sbin /sbin. Still the same.
I also tried setting the following in my composer.json. Still the same:
"optimize-autoloader": true,
"classmap-authoritative": true,
"vendor-dir": "vendor/",
I finally created an issue on GitHub: https://github.com/composer/composer/issues/7708
https://github.com/leymannx/wordpress-project/blob/master/composer.json
https://github.com/leymannx/wordpress-project/blob/master/scripts/composer/ScriptHandler.php
This is matter of context where your code is run. If you're executing some method directly in post-install-cmd it will be executed inside of Composer's process. It means that it will share all code bundled inside of composer.phar. Since you can't have two classes with the same FQN, you can't include another Symfony\Component\Filesystem\Filesystem in this context.
You can bypass this by running your script inside of separate process. You may create post-install-cmd.php file where you do all bootstrapping (like require vendor/autoload.php) and call these methods. Then run this file in your post-install-cmd hook:
"scripts": {
"post-install-cmd": [
"php post-install-cmd.php"
]
},

Propel and Composer in PHP: no connection

I've problem in configuring Propel with Composer in my php project.
this is how appears my tree directory:
project/
|--/public_html/index.php
|--/app/
| |--data/
| | |--propel.json
| | |--schema.xml
| |--vendor/
| |--composer.json
In /data/ folder I would store all my propel files, that is generated-classes/ , generated-conf/ and generated-sql/ .
To realize this purpose, with a terminal in /data/ folder, I put the commands in the following sequence:
$ propel sql:build
$ propel model:build
$ propel config:convert
and all go right.
To make more suitable work, in composer.json I've added this extra feature:
"autoload": {
"classmap": ["./data/generated-classes/"]
}
so that, almost in theory, putting
require '../app/vendor/autoload.php';
inside index.php should be enough. Unfortunately, when I try to use one propel classes inside this page, returns the error
Type: Propel\Runtime\Exception\RuntimeException
Message: No connection defined for database "my_api". Did you forget to define a connection or is it wrong written?
File: 'C:\pathToMyProject'\project\app\vendor\propel\propel\src\Propel\Runtime\ServiceContainer\StandardServiceContainer.php
Line: 279
I thought that propel doesn't find the propel.json file stored in /data/folder.
As extra, if in index.php I simply add
require_once '../app/data/generated-conf/config.php';
all goes right.
There's a trick to autoload propel without use this last require_once? (obviously keep the tree as is).
Thanks for reading.
The order of CLI commands is important:
composer install or update to fetch propel
then the commands to generate the models with propel
then re-scan / re-generate the autoloading files with composer dump-autoload --optimize
You could include the configuration file in the bootstrap process of your application - like you already have.
Or you could use the files directive in Composers autoload section
to define file(s), which should be included on every request.
Referencing: https://getcomposer.org/doc/04-schema.md#files
"autoload": {
"files": ["./data/generated-conf/config.php"],
"classmap": ["./data/generated-classes/"]
}

Categories