rudak/js-injector

Injection de constantes PHP dans un JS - TEST

Installs: 21

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 0

Language:JavaScript

pkg:composer/rudak/js-injector

v1.6.6.4 2019-01-05 00:33 UTC

This package is auto-updated.

Last update: 2025-12-05 08:59:50 UTC


README

Bundle Symfony pour injecter des constantes PHP dans un fichier JavaScript.

Installation

PHP (Composer)

composer require rudak/js-injector

JavaScript (npm)

npm install js-injector

Utilisation rapide

1. Créer un provider de valeurs

Créez un service qui implémente HarvesterInterface :

use Rudak\JsInjector\Harvester\HarvesterInterface;

class MyValuesProvider implements HarvesterInterface
{
    public function getValues(): array
    {
        return [
            'API_URL' => 'https://api.example.com',
            'DEBUG' => true,
        ];
    }
}

2. Taguer le service

services:
    App\Service\MyValuesProvider:
        tags: ['rudak.injector']

3. Générer le fichier JS

php bin/console rudak:generate:js

Le fichier injection.js sera créé dans /public/bundles/rudakInjection/.

4. Inclure le script dans vos templates

<script src="/bundles/rudakInjection/injection.js"></script>
<script>
    console.log(API_URL); // 'https://api.example.com'
    console.log(DEBUG);   // true
</script>

API JavaScript

import { inject, injectFromJson, generateInjectionCode } from 'js-injector';

// Injection directe
inject({ API_URL: 'https://api.example.com', DEBUG: true });

// Avec namespace
inject({ timeout: 5000 }, { namespace: 'MyApp' });
console.log(window.MyApp.timeout); // 5000

// Depuis JSON
injectFromJson('{"foo": 1, "bar": "hello"}');

Scripts npm

npm test           # Lancer les tests
npm run lint       # Vérifier le code
npm run format     # Formater le code

Licence

Apache License 2.0