LESS php not updating css - php

I am using less for creating css file. I am using this plugin
<?php
require "lessc.inc.php";
$less = new lessc;
$less->setVariables(array(
"base" => "968px"
));
$less->checkedCompile("webform.less", "output.css");
?>
When I first compile this code... the base variable in webform.less is compiled and exact output is created. But when I edit base value and compile, it is not updating. Am I missing anything here in less compilation using php?

The checkedCompile() method compiles only if the input file is a different one or the output file does not already exist. You have to make use of the function compileFile() to compile it again and again..
The following code may do the trick..
<?php
require "lessc.inc.php";
$less = new lessc;
$less->setVariables(array(
"base" => "968px"
));
$less->compileFile("webform.less", "output.css");
?>

Related

Measure code coverage using Xdebug when crawls web application

I build my crawler based on ChromeDriver Selenium , and I want to measure the code coverage of the web application when my automated crawler crawls the application.
So, my question is how I do that using Xdebug (I'm newer on it). I installed Xdebug on my PHP, but I didn't know how to start? Can anyone have an idea to give me steps for that because I didn't find any resource that help me.
I don't have a direct example, but I would approach this in the following way. The code is untested, and will likely require changes to work, take this as a starting point
In any case, you want to do the following things:
Collect code coverage data for each request, and store that to a file
Aggregate the code coverage data for each of these runs, and merge them
Collecting Code Coverage for Each Request
Traditionally code coverage is generated for unit tests, with PHPUnit. PHPUnit uses a separate library, PHP Code Coverage, to collect, merge and generate reports for the per-test collected coverage. You can use this library stand alone.
To collect the data, I would do composer require phpunit/php-code-coverage and then create an auto_prepend file, with something like the following in it:
<?php
require 'vendor/autoload.php';
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlReport;
$filter = new Filter;
$filter->includeDirectory('/path/to/directory');
$coverage = new CodeCoverage(
(new Selector)->forLineCoverage($filter),
$filter
);
$coverage->start($_SERVER['REQUEST_URI']);
function save_coverage()
{
global $coverage;
$coverage->stop();
$data = $coverage->getData();
file_put_contents('/tmp/path/crawler/' . bin2hex(random_bytes(16)), serialize($data) . '.serialized', $data->get );
}
register_shutdown_function('save_coverage');
?>
(I copied most of that from the introduction in the php-code-coverage README.md)
You need to configure this prepend_file with php.ini: auto_prepend_file.
When you now crawl through your web site, you should get a file with code coverage for each request into /tmp/path/crawler/, but make sure that directory exists first.
Merging Code Coverage
For this step, you need to write a script that load each of the generate files (look at glob()), and merge them together.
PHP Code Coverage has a method for this too. It would look something like:
<?php
require 'vendor/autoload.php';
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Report\Html\Facade as HtmlReport;
$filter = new Filter;
$filter->includeDirectory('/path/to/directory');
$coverage = new CodeCoverage(
(new Selector)->forLineCoverage($filter),
$filter
);
foreach ( glob('/tmp/path/crawler/*.serialize') as $file)
{
$data = unserialize( file_get_contents( $file ) );
$fileCoverage = new CodeCoverage(
(new Selector)->forLineCoverage($filter),
$filter
);
$fileCoverage->setData( $data );
$coverage->merge( $fileCoverage );
}
/* now generate the report, as per the README.md again */
(new HtmlReport)->process($coverage, '/tmp/code-coverage-report');
?>
If I find some time, I will do a video on this.

php require_once modify the local variable?

In my web page, I wrote:
<?php
//define('__PUBLIC__', $_SERVER['DOCUMENT_ROOT'].'/public');
$doc_public = $_SERVER['DOCUMENT_ROOT'].'/public';
echo "Before include...<==============>$doc_public";
?>
<?php require_once($doc_public.'/inc/head.php'); ?>
<?php echo "After include...<==============>$doc_public"; ?>
And the page shows:
This firstly happened when I notice the fatal error in the footer, but the head is fine.
Although I can implement define or constant variable to avoid this, I am still curious how it happens.
P.S.: I run this under Apache with a port 8001. This is set in 【apache\conf\extra\httpd-vhosts.conf】. I am running more than one webapp under this site. I just share this information, as I am not sure this has anything to do with this case.
Thanks!
When you require a file, if a variable is modified it affects the original script as well, that's how it's designed. Require doesn't create a secondary environment separated from the including file, it just adds the PHP code in sequence, exactly like if you had written the code in the initial file.
Have a look at the official PHP documentation, the first example is exactly the same as your case
http://php.net/manual/en/function.include.php
(include is the same as require, the latter just throws an error. For more info about differences between include and require http://php.net/manual/en/function.require.php)

Adding to a data array externally PHP

I'm trying to figure out how to add to a PHP data array externally using PHP.
Say if this array, below. Was in a file called Index.php
$data=array("user1"=>array("url"=>"user1.pdf","password"=>"pass1"),
"user2"=>array("url"=>"user2.php","password"=>"pass2"));
and I wanted to add third user using a different Php file taking inputs from somewhere else to insert into the url, password and user namespace.
Thanks.
$data['user3']=array("url"=>"user3.pdf","password"=>"pass3")
I believe include_once() or require_once() should do the trick.
include_once('index.php');
array_push($data,"user3"=>array("url"=>"user3.pdf","pass"=>"123"));
include_once or require_once functions are similar to executing that file once and continuing further. http://in1.php.net/include_once
Alternatively, php now allows for object-oriented programming, so if you are familiar with it you can take a shot at that
If you need it from different file, then the simplest way is to include the other file, which adds users to $data.
index.php
$data = array(
"user1"=>array("url"=>"user1.pdf","password"=>"pass1"),
"user2"=>array("url"=>"user2.php","password"=>"pass2")
);
other_file.php
include "other_file.php";
$data["user3"] = array("url"=>"user3.php","password"=>"pass3");
// or
array_push($data, array("user3" => array("url"=>"user3.php","password"=>"pass3"));
Are you sure you need that other file?

How to read an array from another php page

I have the below mentioned code (actually, configuration) written in a file (config.php) and i want to get the content written in config.php in another file (check.php)
Codes written in config.php:
<?php
$CONFIG = array (
'id' => 'asd5646asdas',
'dbtype' => 'mysql',
'version' => '5.0.12',
);
Code written in check.php to get the content is:
$config_file_path = $_SERVER['DOCUMENT_ROOT'] . 'config.php';
$config = file_get_contents($config_file_path);
Using the above code i am getting the output as a string and i wanted to convert it into an array. To do so, i have tried the below code.
$config = substr($config, 24, -5); // to remove the php starting tag and other un-neccesary things
$config = str_replace("'","",$config);
$config_array = explode("=>", $config);
Using the above code, i am getting an output like:
Array
(
[0] => id
[1] => asd5646asdas,
dbtype
[2] => mysql,
version
[3] => 5.0.12
)
which is incorrect.
Is there any way to convert it into an array. I have tried serialize() as well as mentioned in accessing array from another page in php , but did not succeed.
Any help on this will be appreciated.
You don't need file_get_contents:
require_once 'config.php'
var_dump($CONFIG);
Use include or require to include and execute the file. The difference is just what happens when the file doesn't exist. inlcude will throw a warning, require an error.
To make sure just to load (and execute) the file the first time you can also use include_once, require_one php.net
If you are somehow not able to include the file (what reasons ever) take a look at the eval() function to execute php code from string. But this is not recommendet at all because of security reasons!
require_once('config.php');
include_once('config.php');
require('config.php');
include('config.php');
I'm very confused at your approach, if you just do this:
include(config.php);
Then your $CONFIG variable will be available on other pages.
print_r($CONFIG);
Take a look on the PHP website which documents the include function.
if you are using codeigniter no need to include the config file, you can access using $this, but if you are using plain php, u need to use include() or require() or include_once() or require_once()
simple
include_file "config.php";
How could you fail to read about the require_once directive? :)
$config = file_get_contents($config_file_path);
Must be:
require_once $config_file_path;
// now you can use variables, classes and function from this file
var_dump($config);
If you follow the link to the require_once manual page please read also about include and include_once. As a PHP developer you MUST know about this. It's elementary

Accessing external functions from php class

Hello let's say that I have the following structure in my application .
<?
include('includes/functions.php');
include('includes/classes/login.class.php');
$login = new login();
?>
What I want is inside the login class to call a function that is defined in functions.php . But I can't get it to work.
PHP does not care in which file you have storred what function or class definition. Only namespaces, order or processing and of course where inside classes or functions you have what definitions matter.
What you are doing is correct.
Including php-code just adds the content of that file in the file where you execute the include. If this is not working there is something else wrong with your code.
You should close this question and make a new one. Include the content of the files you are including and the errors that are displayed.

Categories