I was wondering, if it's possible not to repeat your self when using if statements in PHP. The following is the part of my spl_autoload_register function, e.g.:
if (is_file(Core::Config('plugins_path') . '/' . strtolower($required_class) . '.class.php'))
{
require_once (Core::Config('plugins_path') . '/' . strtolower($required_class) . '.class.php');
}
Is there a way to write it familiar to:
if (is_file(Core::Config('plugins_path') . '/' . strtolower($required_class) . '.class.php'))
{
require_once ( this );
}
in order not to duplicate exactly the same line of the code?
The example above is not working.. Is there a way really?
Not exactly as you described. But you can shrink the code with a variable.
$file = Core::Config('plugins_path') . '/' . strtolower($required_class) . '.class.php';
if (is_file($file))
{
require_once ( $file );
}
Based on the idea offered by Pietu, I'd rather shorten it to:
$file = Core::Config('plugins_path') . '/' . strtolower( $required_class ) . '.class.php';
is_file( $file ) && require_once ( $file );
Related
I am trying to create a zip file with folders inside it.
Now the code is :
{
$zipper = new \Chumper\Zipper\Zipper;
$zipper->make(storage_path('app/' . $zipPath));
.
.
Storage::makeDirectory($zipPath . DIRECTORY_SEPARATOR . $user->username , 0777);
$zipper->folder($zipPath . DIRECTORY_SEPARATOR . $user->username);
$currentZipPath = $zipPath . DIRECTORY_SEPARATOR . $user->username;
Storage::makeDirectory($currentZipPath . DIRECTORY_SEPARATOR . $myfolder->name , 0777);
$zipper->folder($currentZipPath . DIRECTORY_SEPARATOR . $myfolder->name);
.
.
$this->addDataToZip($contents, $currentZipPath . DIRECTORY_SEPARATOR . $myfolder->name, $zipper, $user);
Log::info('Zip status : ' . $zipper->getStatus()); //Gives "No error"
$zipper->close();
}
public function addDataToZip($contents, $path, &$zipper, $user)
{
foreach ($contents as $content) {
$filebasepath = storage_path('app/' . $path);
Storage::copy(
$this->model->getActiveStorageBasePath($user)
. DIRECTORY_SEPARATOR . $content->unique_name,
$path . DIRECTORY_SEPARATOR . $content->unique_name
);
$zipper->add(storage_path() . DIRECTORY_SEPARATOR . 'app'
. DIRECTORY_SEPARATOR . $path
. DIRECTORY_SEPARATOR . $content->unique_name);
}
}
Now when I view the folder locally, the entie admin.zip folder is created with complete heirarchy and content.
But on $zipper->close, the zip file is not created and the admin.zip folder remains there.
Also there is no error in the API call or the logs.
Please guide on where I might be making an error
See with example Chumper/Zipper. This is the simple and straightforward way to use.
OR you can place this line "status ::". $zipper->status . "\n" after $zipper->add() to check status.
Right so i'm making a configuration class that will use an array of different file types in 4 main locations. Now I want to make it so that the configuration class will search these locations in order at the moment i'm using the following
if (file_exists(ROOT . DS . 'Application/Config/' . APP_ENV . DS . $file)) {
$this->filePath = ROOT . DS . 'Application/Config/' . APP_ENV . DS . $file;
echo $this->filePath;
} else {
if (file_exists(ROOT . DS . "Application/Config/$file")) {
$this->filePath = ROOT . DS . "Application/Config/$file";
echo $this->filePath;
} else {
if (file_exists(CARBON_PATH . 'Config' . DS . APP_ENV . DS . $file)) {
$this->filePath = CARBON_PATH . 'Config' . DS . APP_ENV . DS . $file;
echo $this->filePath;
} else {
if (file_exists(CARBON_PATH . "Config/$file")) {
$this->filePath = CARBON_PATH . "Config/$file";
echo $this->filePath;
} else {
throw new \Exception("Unable to locate: $file, Please check it exists");
}
}
}
}
pretty messy and not very flexible.
What I want to be able to do is search the locations in the same order BY FILE NAME ONLY after finding the first match It would then return the file with the extension for the configuration class to use the correct method to parse into a php array and so on.
What is the best way to search these locations for a file name
Example
Say we want a database configuration file as you can see there are 2
ConfigLocation1/Dev/
/file.php
/database.json
ConfigLocation1/
/database.ini
/anotherfile.json
I would want to use the function like so
config::findFile('database');
and it return
$result = ConfigLocation1/Dev/database.json
but if it wasnt found here then then
$result = ConfigLocation1/database.ini
Not very good at explaining things so hope the example helps
As you mentioned you need to check for file in 4 locations, so instead of if conditions, create an array of directories and loop through.
and you can use glob, to find a file irrespective of extension. see my example below:-
//Make a array of directory where you want to look for files.
$dirs = array(
ROOT . DS . 'Application/Config/' . APP_ENV . DS,
CARBON_PATH . 'Config' . DS . APP_ENV . DS
);
function findFiles($directory, $filename){
$match = array();
foreach ($directory => $dir) {
$files = glob($dir.$filename);
foreach ($files as $file) {
$match[] = $file;
}
}
return $match;
}
// to find database
$results = findFiles($dirs, 'database.*');
I have a file that auto loads each of my classes.
This is what it contains:
spl_autoload_register(function($class){
require_once 'classes/' . $class . '.php';
});
require_once 'functions/sanitize.php';
require_once 'functions/hash.php';
But when I require_once this file from another php file that is inside my ajax folder, it will try looking for the classes, the function will look from my classes with the path: main_folder/ajax/classes instead of just main_folder/classes.
Does anyone know how to fix this?
FIX:
spl_autoload_register(function($class){
if (file_exists('classes/' . $class . '.php')) {
require_once 'classes/' . $class . '.php';
}
elseif (file_exists('../classes/' . $class . '.php')) {
require_once '../classes/' . $class . '.php';
}
elseif (file_exists('../../classes/' . $class . '.php')) {
require_once '../../classes/' . $class . '.php';
}
You should simple use this function just once - in the main file (usually index.php) and not in another files.
However if it's not possible (but I don't see any reason when could it be not possible) you can change it for example that way:
spl_autoload_register(function($class){
if (file_exists('classes/' . $class . '.php')) {
require_once 'classes/' . $class . '.php';
}
elseif (file_exists( $class . '.php')) {
require_once $class . '.php';
}
});
Here is a proper way, It should universally work.
EDIT: Make sure to specify levels on dirname in order to find the correct path.
spl_autoload_register(function ($classname) {
$file_realpath = dirname(realpath(__FILE__), levels: 1 /* Change that? */) . DIRECTORY_SEPARATOR . dirname($classname);
$classpath = sprintf('%s' . DIRECTORY_SEPARATOR . '%s.php', $file_realpath, basename($classname, '.{php,PHP}'));
if (file_exists($classpath)) {
echo "Loading <b>$classpath</b>...<br>";
require($classpath);
}
});
You need to know the absolute path to the classes directory, then just use a full qualified path to get there.
Make sure your document root is correctly configured with your http server. Furthermore this is usually solved by routing all requests to index.php and deriving the base path (document root) from there. Here is your code modified:
spl_autoload_register(function($class) {
$resolved = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class . '.php';
if(file_exists($resolved)) {
require_once $resolved;
} else {
// make it known to your that a class failed to load somehow
return;
}
})
Here is another implementation that I use for simple projects
I build a function that if you give it an array of paths it will look for a file in each path and if you give it an array of one path it will look in that path only for the file.
My questions, is when you give it an array of paths such as:
$array = array(
'template_view_paths' => array(
'path_name' => some/path/
'path_name_two' => some/path/two/
)
)
It seems to find the file in some/path/ but keeps going on to some/path/two/ and freaks out because the file doesn't exist. What would I have to change so that if it find the file in any path that it stops looking in other paths?
The code - NOT re-factored
public function render_view($template_name){
if(!isset($this->_options['template_view_path'])){
throw new AisisCore_Template_TemplateException('Not view path was set.');
}
if(is_array($this->_options['template_view_path'])){
foreach($this->_options['template_view_path'] as $template=>$path){
require_once($path . $template_name . '.phtml');
}
}else{
if(!file_exists($this->_options['template_view_path'] . $template_name . '.phtml')){
throw new AisisCore_Template_TemplateException('Could not find: ' . $template_name . '.phtml at ' . $path);
}
require_once ($this->_options['template_view_path'] . $template_name . '.phtml');
}
}
Note: the part to focus on is the if(is_array()){} loop.
Shouldn't:
if(is_array($this->_options['template_view_path'])){
foreach($templates as $template=>$path){
require_once($path . $template_name . '.phtml');
}
}else{
be:
if(is_array($this->_options['template_view_path'])){
foreach($this->_options['template_view_path'] as $template=>$path){
if (file_exists($filename = $path . $template_name . '.phtml')) {
require_once($filename);
return;
}
}
throw new AisisCore_Template_TemplateException('Could not find: ' . $template_name . '.phtml in any paths');
}else{
You're never actually looping $this->_options['template_view_path'].
I'm just trying to build my first app on PHP Fog but there's a piece of code that doesn't run properly - works fine on localhost and other regular hosts though.
I use a modified version of TinyMVC, this is the code responsible for setting up autoloading:
/* Set include_path for spl_autoload */
set_include_path(get_include_path()
. PATH_SEPARATOR . FRAMEWORK_BASEDIR . 'core' . DS
. PATH_SEPARATOR . FRAMEWORK_BASEDIR . 'libraries' . DS
. PATH_SEPARATOR . FRAMEWORK_APPLICATION . DS . 'controllers' . DS
. PATH_SEPARATOR . FRAMEWORK_APPLICATION . DS . 'models' . DS
);
/* File extensions to include */
spl_autoload_extensions('.php,.inc');
/* Setup __autoload */
$spl_funcs = spl_autoload_functions();
if($spl_funcs === false)
spl_autoload_register();
elseif(!in_array('spl_autoload',$spl_funcs))
spl_autoload_register('spl_autoload');
Basically, it fails at the first class it should load, which is located in "FRAMEWORK_BASEDIR . 'core' . DS". The class filename is "framework_controller.php" and class name is "Framework_Controller" (tried lowercase as well). If I include the class manually it works but fails with autoload.
Here's the error message that I get:
Fatal error: spl_autoload(): Class Framework_Controller could not be loaded in /var/fog/apps/app7396/claudiu.phpfogapp.com/application/controllers/home.php on line 12
Any ideas as to what could the problem be?
I managed to sort it out:
function framework_autoload($className, $extList='.inc,.php') {
$autoload_paths = array (
FRAMEWORK_BASEDIR . 'core' . DS,
FRAMEWORK_BASEDIR . 'libraries' . DS,
FRAMEWORK_APPLICATION . DS . 'controllers' . DS,
FRAMEWORK_APPLICATION . DS . 'models' . DS
);
$ext = explode(',',$extList);
foreach($ext as $x) {
foreach ($autoload_paths as $v) {
$fname = $v . strtolower($className).$x;
if(#file_exists($fname)) {
require_once($fname);
return true;
}
}
}
return false;
}
spl_autoload_register('framework_autoload');
Thanks to another question here on StackOverflow: spl_autoload problem