I've got the following file structure:
includes
class.Klasse.php
src
autoload.php
tests
KlasseTest.php
This structure is within a project folder. On the linux shell, being in this folder, I type the following command line:
phpunit --bootstrap src/autoload.php tests/KlasseTest.php
The command line is showing me this:
PHPUnit 4.1.4 by Sebastian Bergmann.
PHP Fatal error: Class 'Klasse' not found in /home/doug/workspace/PHPunit/tests/KlasseTest.php on line 7
My autload.php:
<?php
function __autoload($class_name) {
include 'includes/class.' . $class_name . '.php';
}
My KlasseTest.php:
<?php
class KlasseTest extends PHPUnit_Framework_TestCase {
public function testWertvergleich() {
$o = new Klasse();
}
}
My class.Klasse.php:
<?php
class Klasse {
public function __construct() {
}
}
I don't know why I am getting the message above.
In src/autoload.php, use:
function __autoload($class_name) {
include __DIR__ . '/../includes/class.' . $class_name . '.php';
}
The autoloader searches the classes in directories relative to the location of the autoload.php file. __DIR__ ensures that the starting point of the file inclusion is always the directory of autoload.php - from there, you move one directory up (/..), and then inside the includes directory.
Related
The issue is that if watching multiple directories via the Finder class.
If one does not exist, the Finder will throw an exception and the fixer will die.
$finder = Finder::create()
->in([
__DIR__ . '/web/app/mu-plugins/ys-*',
__DIR__ . '/web/app/plugins/ys-*'
])
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
Is there a possibility of adding an option to "skip if directory does not exist" so it doesn't kill the fixer if one of the directories does not exist?
I would use an array_filter beforehand with a simple glob() function to check if the directories/files actually do exist. Glob will return an array of files/folders if any are found.
If directories/files do exist, pass them to the Finder:
$directories = [
__DIR__ . '/web/app/mu-plugins/ys-*',
__DIR__ . '/web/app/plugins/ys-*'
];
$checkDirs = array_filter($directories, static function ($dir){
return !empty(glob($dir));
});
$finder = Finder::create()
->in($checkDirs)
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
I'm having significant amount of troubles trying figure out how to install and use DOMPDF with Composer and Symfony. It is a project written a few years ago and I am completely new to Composer, Symfony and DOMPDF...somebody else told me we were using Composer and Symfony.
I've installed DOMPDF using the puTTy command line interface by:
1. going to the folder where my composer.json is (vendor)
2. running the command "Install dompdf/dompdf" It completed successfully with no errors.
3. Then running the command composer "require dompdf/dompdf" which also completed successfully with no errors.
Then I get lost/confused...
I see instructions that say to edit composer.json with :
{
"require": {
"squizlabs/php_codesniffer": "2.0.*",
}
So here is the new contents of composer.json
{
"require": {
"spipu/html2pdf": "^5.0",
"dompdf/dompdf": "^0.8.1"
}
I've tried using DOMPDF by putting the following line in my PHP code and all I get is a blank page
use Dompdf\Dompdf;
There is also an autoload.php which looks like this:
//autoload.php #generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit********************************::getLoader();
Following the rabbit hole, autoload_real.php looks like this:
// autoload_real.php #generated by Composer
class ComposerAutoloaderInit987ec9019a1b2f978bf00ce76684ede0
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit987ec9019a1b2f978bf00ce76684ede0', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
spl_autoload_unregister(array('ComposerAutoloaderInit987ec9019a1b2f978bf00ce76684ede0', 'loadClassLoader'));
$useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
if ($useStaticLoader) {
require_once __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit987ec9019a1b2f978bf00ce76684ede0::getInitializer($loader));
} else {
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->set($namespace, $path);
}
$map = require __DIR__ . '/autoload_psr4.php';
foreach ($map as $namespace => $path) {
$loader->setPsr4($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
}
$loader->register(true);
if ($useStaticLoader) {
$includeFiles = Composer\Autoload\ComposerStaticInit987ec9019a1b2f978bf00ce76684ede0::$files;
} else {
$includeFiles = require __DIR__ . '/autoload_files.php';
}
foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire987ec9019a1b2f978bf00ce76684ede0($fileIdentifier, $file);
}
return $loader;
}}function composerRequire987ec9019a1b2f978bf00ce76684ede0($fileIdentifier, $file){if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
require $file;$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;}}
Additionally, I have 14 folders in the vendor file, which I assume are all packages used with Composer, but I don't see them being required in the composer.json file, and, after installing DOMPDF, I don't see a vendor folder for it. I'd like to remove them, but I don't know what is and is not being used.
I've read the "getting started guides" and I still don't know what I've done wrong.
I'm sure I will have a thousand more questions...thank you for your patience.
a) your composer.json file should be in the root of your project directory and not in the vendor directory.
b) just typing the command
composer require dompdf/dompdf
in your project directory should be enough to install the libraries in the vendor directory AND to add the line
"dompdf/dompdf": "^0.8.1"
in your composer.json.
c) Follow instructions on https://github.com/dompdf/dompdf how to use dompdf.
d) In general when working with composer you have to include the vendor/autoload.php file but in the Symfony framework that will already happen in the app/autoload.php.
require 'vendor/autoload.php';
e) dompdf does use namespaces. Little example:
require "../vendor/autoload.php"; // change path if you need to
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
f) Lot of packages that can be installed with composer use other packages too. If you take a look at the composer.json from dompdf you will see that it requires some other packages which composer will automaticly install for you.
[edit]
Your composer.json is in the root directory of your project. Same for your vendor directory. In your vendor directory there will be a composer directory, that is normal. But execute any commands from your root directory. structure:
- [project root] (execute commands here)
|
|- composer.json
|- [vendor]
|
|- [composer]
If you got stuck just delete the whole vendor directory and composer.json file and again use the command
composer require dompdf/dompdf
I've been having this issue for a few days now and though I have made some progress I still have not gotten anywhere. The main issue is finding the classes and autoloading them dynamically as needed. The end result is the outputted error you see in the title of this post.
File structure is as follows
input.php
core/
classes/
init.php
loader.php
Below is the corresponding code:
login.php (Line 21)
if (input::exists()) {
init.php
if (!defined('BASE_PATH')) {
define('BASE_PATH', dirname(__FILE__) . 'classes/');
require 'loader.php';
Loader::Register();
}
loader.php
class Loader {
public static function Register() {
return spl_autoload_register(array('Loader', 'Load'));
}
public static function Load($strObjectName) {
$strObjectFilePath = BASE_PATH . $strObjectName . '.php';
if ((file_exists($strObjectFilePath) === false) || (is_readable($strObjectFilePath) === false)) {
echo "there is a problem!";return false;
}
else {
require ($strObjectFilePath);
}
}
}
error
Fatal Error Class 'input' not found in /home/adminl0gin/public_html/login.php on line 21
Current php version is 5.4.24 on a live hosted server GoDaddy
Many thanks in advance!
Have you added init.php in your login.php file?
In a directory I have two files
oneTest.php
<?php
class oneTest extends PHPUnit_Framework_TestCase {
public function testSomethingOne()
{
echo 'ONE TEST';
$this->assertEquals(1, 1);
}
}
twoTest.php
<?php
class twoTest extends PHPUnit_Framework_TestCase {
public function testSomethingTwo()
{
echo 'TWO TEST';
$this->assertEquals(2, 2);
}
}
From within the directory I can run both tests fine
phpunit oneTest.php
phpunit twoTest.php
And I get the expected output on both.
If I try and run all tests with
phpunit *
It only runs the first test.
I'm running phpunit 3.6.12 on Ubuntu 12.04.
Any ideas why this is happening?
Thanks
This is simply a limitation of phpunit, it is not programmed to support multiple files on the command line. You can, however, pass a directory name to phpunit. If you want to run the tests in the current directory, use
phpunit .
Edit: alternatively, you can specify a testsuite in a XML configuration file.
I want to make a local config file, config_local.yml, that allows each development environment to be configured correctly without screwing up other people's dev environments. I want it to be a separate file so that I can "gitignore" it and know that nothing essential is missing from the project, while simultaneously not having the issue of git constantly telling me that config_dev.yml has new changes (and running the risk of someone committing those changes).
Right now, I have config_dev.yml doing
imports:
- { resource: config_local.yml }
which is great, unless the file doesn't exist (i.e. for a new clone of the repository).
My question is: Is there any way to make this include optional? I.e., If the file exists then import it, otherwise ignore it.
Edit: I was hoping for a syntax like:
imports:
- { resource: config.yml }
? { resource: config_local.yml }
I know this is a really old question, and I do think the approved solution is better I thought I would give a simpler solution which has the benefit of not changing any code
You can use the ignore_errors option, which won't display any errors if the file doesn't exist
imports:
- { resource: config_local.yml, ignore_errors: true }
Warning, if you DO have a syntax error in the file, it will also be ignored, so if you have unexpected results, check to make sure there is no syntax error or other error in the file.
There is another option.
on app/appKernel.php change the registerContainerConfiguration method to this :
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
$extrafiles = array (
__DIR__.'/config/config_local.yml',
);
foreach ($extrafiles as $filename) {
if (file_exists($filename) && is_readable($filename)) {
$loader->load($filename);
}
}
}
this way you have a global config_local.yml file that overwrites the config_env.yml files
A solution is to create a separate environment, which is explained in the Symfony2 cookbook. If you do not wish to create one, there is another way involving the creation of an extension.
// src/Acme/Bundle/AcmeDemo/DepencendyInjection/AcmeDemoExtension.php
namespace Acme\DemoBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
class AcmeDemoExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
// All following files will be loaded from the configuration directory
// of your bundle. You may change the location to /app/ of course.
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
try
{
$loader->load('config_local.yml');
}
catch(\InvalidArgumentException $e)
{
// File was not found
}
}
}
Some digging in the Symfony code revealed me that YamlFileLoader::load() FileLocator::locate() will throw \InvalidArgumentException, if a file is not found. It is invoked by YamlFileLoader::load().
If you use the naming conventions, the extension will be automatically executed. For a more thorough explanation, visit this blog.
I tried both above answers but none did work for me.
i made a new environment: "local" that imports "dev", but as you can read here: There is no extension able to load the configuration for "web_profiler" you also had to hack the AppKernel class.
Further you couldnt set config_local.yml to .gitignore because the file is necessary in local env.
Since i had to hack the AppKernel anyway i tried the approach with the $extrafiles but that resulted in "ForbiddenOverwriteException"
So now what worked for me was a modification of the $extrafiles approach:
replace in app/AppKernel.php
$loader->load(__DIR__ . '/config/config_' . $this->getEnvironment() . '.yml');
with
if ($this->getEnvironment() == 'dev') {
$extrafiles = array(
__DIR__ . '/config/config_local.yml',
);
foreach ($extrafiles as $filename) {
if (file_exists($filename) && is_readable($filename)) {
$loader->load($filename);
}
}
} else {
$loader->load(__DIR__ . '/config/config_' . $this->getEnvironment() . '.yml');
}