Updating version number in php file using grunt - php

I'm using grunt to compile, minify etc css and javascript which is all working as it should. But as i'm integrating it into an existing php site, for me to see the changes I need to update the version number in the php to clear the cache and the updated CSS to be shown.eg:
$nVersion = 20150807;
I've looked around but can't find a npm package which will allow me to specify the file and update certainly elements of the file.
Does anyone know of a package like this exists or know another way for me to achieve this?

For anyone else interested I've used grunt-text-replace npm package with the following grunt task:
replace: {
version: {
src: ['./version.php'],
dest: './version.php',
replacements: [{
from: /^\$nVersion = ([0-9])+/igm, // eg $nVersion = 20150807;
to: '$nVersion = ' + Date.now() // $nVersion = 1439212970400;
}]
}
}
And have attached the task to the watcher for any changes to the SCSS, JS files then it updates the version number

Related

Vite & Vue3: Adding JS and CSS bundle to Wordpress plugin

I have a small Vue app and I want to use it as a plugin in Wordpress. I got this working without problems. The only challenge I have is to get Vite to use the PHP plugin file as the entry point instead of the usual index.html.
This is problematic because the build gives the bundles a unique file name by adding a hash, like index-eb5dc3f1.js. The index.html is generated during the build and automatically links to the unique files, but I need to manually change the PHP file after every build. I want to keep this mechanism of unique bundle names.
As a workaround I have changed my vite.config.js so vite no longer adds the hash:
build: {
rollupOptions: {
output: {
entryFileNames: `assets/[name].js`,
chunkFileNames: `assets/[name].js`,
assetFileNames: `assets/[name].[ext]`
}
}
}
And in the WordPress plugin I am adding it like:
wp_register_script('the-plugin', plugin_dir_url( __FILE__ ).'assets/index.js', true );
This works, but I rather would keep the hashes in the file name. Is there a possibility to let Vite alter the PHP file?
I have tried adding input: 'index.php', to the rollupOptions, but it is choaking on the PHP:
error during build:
RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)

Yeoman - php project

I'm starting a new php project and I'm trying to learn new techniques (for me). Grunt, yeoman etc.
Now I want to start a php project and found this example from Bradleycorn => generator-php. As far as I think, I have everything working, but I can't rund my development local site using grunt server. I'm getting a error:
Running "php:server" (php) task
Invalid address: http://localhost/myproject.nl:80
I have this folder structure:
\Applications\XAMPP\xamppfiles\htdocs\myproject.nl\
Beneath this folder:
myproject\
|--.sass-cache\
|--.tmp\
|--app\
|--dist\
|--node_modules\
|--bower.json
|--Grunftile.js
|--package.jason
|--router-dist.php
|--router.php
In the Gruntfile.js this code is placed (part of the code with the dev URL):
module.exports = function (grunt) {
// load all grunt tasks
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);
// CONFIGURABLE PATHS
// The yeomanConfig object contains file paths and other "constants" that are used throughout
// The rest of this Gruntfile. Basically, any value that is used in multiple places should be
// put here for ease of maintenance. Update the value here, and all other places are updated
// automagically.
var yeomanConfig = {
app: 'app',
dist: 'dist',
siteURL: 'www.myproject.nl',
devURL: 'http://localhost/myproject.nl',
devPort: 80
};
I also tried this devURL http://localhost/myproject.nl/myproject/dist:, but also not working. What should the devURL be in this case?
PS: I also want to run a local database, I'm using XAMPP for that.

Nix Composable Derivation Options

I'm trying to understand what options are available for me in my configuration.nix for particular programs, by searching the pkgs sub-folder of nixpkgs's source tree, picking out the module's definitions to understand the available options. However, I'm running into a troublesome case for PHP - it's a special derivation, a composable derivation. I'm not able to see what options I have available with PHP - something that would be tremendously helpful for enabling special modules like mcrypt, gd, etc. Any help with this would be greatly appreciated!
It took me a while to figure this out but the right way to use composeDerivation for setting the php package build features is this:
# config.nix
{
packageOverrides = pkgs: rec {
php = pkgs.php.merge {
cfg = {
imapSupport = false;
intlSupport = false;
fpmSupport = false;
};
};
};
}
This overrides the default values in cfg specified in php/default.nix (imapSupport, intlSupport and fpmSupport get turned off). You can either place that file in ~/.nixpkgs/config.nix to be active system-wide or use it in another nix file like so to customize the global nixpkgs:
pkgs = import <nixpkgs> { config = (import ./config.nix); };
Try tracking the file interactions from configuration.nix and also try to understand all those flags at first, the PHP file is an "startup engine" of some kind I doesn't seem to have any possible configuration options it is just used as a run switch of some type and read rows 234-236(PHP) it says it needs config
also the http://nixos.org/nixos/manual/sec-configuration-syntax.html#sec-nix-syntax-summary is showing very clearly the possibilities. I say again I'm not into that engine but I think everything further to configure is done with the NixOS admin commands, it will be easier for me to help you if you explain what exactly you need done.

How do I create ICU resource files for use in PHP?

I'm following the instructions from BryanH's answer here: gettext() equivalent in Intl library? and trying to implement localization (translation) with php-intl, but I keep getting the same problem this person had: ResourceBundle returns NULL without any errors being raised
He mentions he created the dat files with a tool (which I cannot figure out how to work) while the person in the former answer simply appears to be using txt files with a .res extension.
How do I properly implement localization with php-intl and ResourceBundle, and what am I doing wrong?
The goal is to have various data files with different languages so I can do something similar to
$t = new Translator();
$t->setResource(new \ResourceBundle('es', 'locales_folder/'));
$t->echo("somestring"); // "el stringo"
..much like the person in the first answer had. Also, the aim is to have easily editable files, so I can give them to translators for fixes, updates, and so on. I realize I could easily do this with custom solution through a simple text file which gets parsed and saved into memcache on first request, where it then persists and gets served from without having to re-read the .dat files, but I'd rather take the suggested route here.
Edit: Just to get it out there - I implemented the same thing with gettext successfully and it was dead easy - save for one bug that persists across linux systems (http://www.php.net/manual/en/book.gettext.php#91187) - but I'd like to rely on the more modern and all-inclusive intl extension if possible.
I can provide a solution to your question on how to create and use .res files for the intl PHP extension, however I have no experience with speed and use in production systems, you will have to see for yourself if this can be a replacement for gettext.
In my opinion a great benefit of gettext are additional tools such as xgettext which can extract strings to be translated. Still, using resources might be more useful in your use-case.
To generate a .res file you need to use the program genrb which is bundled with ICU. (for example when installing ICU on OSX using brew install icu4c (see this tutorial) you can find the tool at /usr/local/Cellar/icu4c/*/bin/genrb (replace * with the version number))
Next you prepare a file for each locale. Let's do this for two languages in this example, German and Spanish.
$ cat de.txt
de {
hello { "Hallo" }
}
$ cat es.txt
es {
hello { "Hola" }
}
$ genrb *.txt
genrb number of files: 2
You now have 2 additional files de.res and es.res, which you can now access in PHP
<?php
foreach (array("de", "es") as $locale) {
$r = ResourceBundle::create($locale, __DIR__);
echo $r["hello"], "\n";
}
which will output
Hallo
Hola
So in essence, you can hand those *.txt files to your translaters and prepare them for PHP using genrb.
Finally, a small example for the class you were trying to write:
<?php
class Translator {
private $resourceBundle = null;
public function __construct(\ResourceBundle $r) {
$this->resourceBundle = $r;
}
public function __get($string) {
return $this->resourceBundle[$string];
}
}
$t = new Translator(new \ResourceBundle('es', __DIR__));
echo $t->hello;

Compressing CSS with no spaces via PHP?

Wondering how to reach a css file like this one from css-tricks.com
http://cdn.css-tricks.com/wp-content/themes/CSS-Tricks-9/style.css?v=9.5
Not sure if he is using php to accomplish this or not. I've been reading countless articles with no luck.
Also, is it something automated that spits out the version number after the .css? Been seeing it around and wondered how to achieve a clean css file.
Any help is appreciated! Thanks.
It's simple enough to use an editor with Search/Replace and strip out all the unnecessary spaces. For instance, when I write CSS I only use spaces to separate keywords - I use newlines and tabs to format it legibly. So I could just replace all tabs and newlines with the empty string and the result is "minified" CSS like the one above.
The version number is a fairly common cache trick. It doesn't affect anything server-side, but the browser sees it as a new file, and caches it as such. This makes it easy to purge the cache of all users when an update is made. Personally, though, I use a PHP function to append "?t=".filemtime($file) (in other words, the timestamp that the file was modified) automatically, which saves me the trouble of manually updating version numbers.
Here is the exact code I use to automatically append modification time to JS and CSS files:
<?php
ob_start(function($data) {
chdir($_SERVER['DOCUMENT_ROOT']);
return preg_replace_callback(
"(/(?:js|css)/.*?\.(?:js|css))",
// all the relevant files are in /js and /css folders of the root
function($m) {
if( file_exists(substr($m[0],1)))
return $m[0]."?t=".filemtime(substr($m[0],1));
else return $m[0];
},
$data
);
});
?>
I would avoid to do it manually because you may corrupt your css.
There are good tools available which will solve such problems for you without to be tricky.
An excellent solution is Assetic which is an assets manager and allow you to filter (minify, compress) using various tools (yuicompressor, google closure, etc..).
It is currently bundle by default with Symfony2 but may be used standalone in any PHP Project.
I've successfully implemented it in a Zend Framework project.

Categories