Flushing Magento cache - php

I have a problem:
When I'm trying to flush Magento cache programmatically like this:
$types=array('config','layout','block_html','translate','collections','eav','config_api','config_api2');
foreach($types as $type) {
$c = Mage::app()->getCacheInstance()->cleanType($type);
Mage::dispatchEvent('adminhtml_cache_refresh_type', array('type' => $type));
}
[source]
I have already read this article about Magento cache and it's flushing, but I'm still missing something.
The above method should do the same as does the "Flush Magento Cache" button, but it doesn't in my case. When I'm running a script I'm saving a new controller, but it doesn't work after cache flushing programmatically with the way described above (and many others I have already tried).
But as soon as I perform the same from admin panel manually in the middle of the script's job- it starts working correctly.
Any idea? Does it help, if I will put here scripts code?
Thanks in advance!

Your code seems to be clearing only the block caches, for new controllers/classes, you will need to remove the cache folder, you can do it programmatically like this
/**
* Remove cache folders:
*/
public function cleanFiles() {
try {
flush();
//cache
$dir = Mage::getBaseDir('cache');
$items = array_diff(scandir($dir), array('..', '.'));
foreach ($items as $item) {
$path = $dir . DIRECTORY_SEPARATOR . $item;
is_dir($path) ? $this->_rrmdir($path) : unlink($path);
}
} catch (Exception $e) {
die("[ERROR:" . $e->getMessage() . "]" . PHP_EOL);
}
}
/**
* Removes a directory and all elements contained
* #param string $dir directory to remove
*/
private function _rrmdir($dir) {
if (is_dir($dir)) {
$objects = array_diff(scandir($dir), array('..', '.'));
foreach ($objects as $object) {
$path = $dir . DIRECTORY_SEPARATOR . $object;
is_dir($path) ? $this->_rrmdir($path) : unlink($path);
}
reset($objects);
rmdir($dir);
}
}

Related

Saving files locally with Laravel

I'm saving files locally in Laravel, however I'm having issues getting the right URL and accessing the files.
I've setup a symlink with Artisan:
php artisan storage:link
When saving, I add public/ to the name, so the files are placed in the /storage/app/public/ directory, which works.
if ($request->hasFile('files')) {
$files = array();
foreach ($request->file('files') as $file) {
if ($file->isValid()) {
$name = time() . str_random(5) . '.' . $file->getClientOriginalExtension();
Storage::disk('public')->put($name, $file);
$files[] = $name;
}
}
if (count($files) > 0) {
$response->assets = json_encode($files);
}
}
The name is stored in the database
["1524042807kdvws.pdf"]
Then the assets are returned as part of a JSON object via my API for Vue
if (count($response->assets) > 0) {
$assets = array();
foreach (json_decode($response->assets, true) as $asset) {
$assets[] = asset($asset);
}
$responses[$key]->assets = $assets;
}
Which returns http://127.0.0.1:8000/1524042807kdvws.pdf but that 404s. I've gotten myself a little confused I think, so any pointers or help would be appreciated.
So I found my answer on another post
I needed to wrap the $file in file_get_contents();
Storage::disk('public')->put($name, $file);
and instead of asset() I used:
Storage::disk('public')->url($asset['file']);
Check this docs. https://laravel.com/docs/5.5/filesystem#the-public-disk
As it shows there instead of
$name = 'public/' . time() . str_random(5) . '.' . $file->getClientOriginalExtension();
you can just have
$name = 'time() . str_random(5) . '.' . $file->getClientOriginalExtension();
Storage::disk("public")->put($name, $file); // assuming you have the public disk that comes by default
Then when you want to get an url, you can use the asset function
asset('storage/foo.txt');

PHP unlink() Error: "Directory not empty"

I have the following recursive method to delete a directory and all its sub-directories and files:
protected function _rrmdir($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != '.' && $object != '..') {
if (filetype($dir . '/' . $object) == 'dir') {
_rrmdir($dir . '/' . $object);
} else {
unlink($dir . '/' . $object);
}
}
}
reset($objects);
rmdir($dir);
}
}
On occasion, the get a Warning, "Directory not empty".
The directory is actually created as a temporary holder for files. The files are downloaded from the Internet using the following snippet:
file_put_contents($filename, file_get_contents($file))
After they are downloaded (a write operation), they are then uploaded to a website (a read operation). Once done uploading, the temporary folder and its files are then deleted.
The odd thing is that when I look inside the temporary folder, there are no files there. It's as if the code tried to delete the folder while the last file was in the process of being deleted?
Any ideas what might be wrong and how to resolve it? I need this code to run on Windows and *nix, so a *nix only solution is not an option.
The constant DIRECTORY_SEPARATOR might help you with Windows/Unix compatibility.
For the folder not empty, try this:
protected function _rrmdir($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != '.' && $object != '..') {
if (is_dir($dir . DIRECTORY_SEPARATOR . $object)) {
_rrmdir($dir . DIRECTORY_SEPARATOR . $object);
} else {
if( is_file($dir . DIRECTORY_SEPARATOR . $object) ) {
if(!unlink($dir . DIRECTORY_SEPARATOR . $object)) {
// code in case the file was not removed
}
// wait a bit here?
} else {
// code for debug file permission issues
}
}
}
}
reset($objects);
rmdir($dir);
}
}
It might happen that you try to remove a file which permissions are not at php exec level.
The is_file() method will return FALSE only if no read permissions, mind that write permissions are needed by the execution owner to delete files.

Want to take backup of code in PHP where files which are to be taken as backup is read from another specified (patch)folder.?

I need to take backup of code for my Project, but the condition is :-
I generate a Patch folder through SVN, which has files structured in the same way as it is in the main Project folder (the patch has lesser files than the main project). I want to write a code which reads the main project and picks only those files which are present in the patch and saves it in a new backup folder.
The code which i wrote is :-
/*Patch Folder */
$frmFoldr = 'patch_test/';
$basepath = '/var/www/html/TestingBackupCron/';
/*The Folder where files are to be backed up it would use basepath and toFoldr folder name. */
$toFoldr = 'axisdirectcheck/';
/* Read/Copy File from this folder. */
$prodMainFolder = '';
$prodBasePath = '/var/www/html/TestingBackupCron/sijucheckfiles/';
$dir_iterator = new RecursiveDirectoryIterator($basepath . $frmFoldr);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
$readDir = strstr($file->getPathname(), $frmFoldr);
$tofileminpath = str_replace($frmFoldr, "", $readDir);
$fromExPath = $prodBasePath . $tofileminpath;
$toExPath = $basepath . $toFoldr . $tofileminpath;
if (strpos($fromExPath, '.php') || strpos($fromExPath, '.js') || strpos($fromExPath, '.css')) {
if (file_exists($fromExPath)) {
copy($fromExPath, $toExPath);
print_r('Files copied to ' . $toExPath . ".\n");
}else{
print_r($fromExPath." ::: Read path not found.\n");
}
}
}
The above code gives me the error "failed to open stream: No such file or directory". I think copy doesn't create folders. Please help guys.
Found a solution. Below code would create folders first and then on the next loop it would write the copied files onto those folders.
public function run($args) {
/*Patch Folder */
$frmFoldr = 'patch_test/';
$basepath = '/var/www/html/TestingBackupCron/';
/*The Folder where files are to be backed up it would use basepath and toFoldr folder name. */
$toFoldr = 'axisdirectcheck/';
/* Read/Copy File from this folder. */
$prodMainFolder = '';
$prodBasePath = '/var/www/html/TestingBackupCron/sijucheckfiles/';
if (file_exists($basepath . $frmFoldr)) {
$dir_iterator = new RecursiveDirectoryIterator($basepath . $frmFoldr);
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
} else{
print_r("Read Directory '$basepath$frmFoldr' not Found. Please check the read directory filename.\n");
exit;
}
$fromDir = array();
$sendToArray = array();
/* this foreach would create folders inside the path ($toFoldr) specified */
foreach ($iterator as $file) {
$readDir = strstr($file->getPathname(), $frmFoldr);
$tofileminpath = str_replace($frmFoldr, "", $readDir);
$fromExPath = $basepath . $readDir;
$toExPath = $basepath . $toFoldr . $tofileminpath;
$sendToArray[] = $tofileminpath;
if (strpos($file, '/.')) {
if (!file_exists($toExPath)) {
$oldmask = umask(0);
mkdir("$toExPath", 0777, true);
umask($oldmask);
}
}
}
/* this foreach would copy files from PROD/UAT and paste inside the path($toExPath) specified */
foreach ($sendToArray as $fPath) {
$fromExPath = $prodBasePath . $fPath;
$toExPath = $basepath . $toFoldr . $fPath;
if (strpos($fromExPath, '.php') || strpos($fromExPath, '.js') || strpos($fromExPath, '.css')) {
if (file_exists($fromExPath)) {
copy($fromExPath, $toExPath);
print_r('Files copied to ' . $toExPath . ".\n");
}else{
print_r($fromExPath." ::: Read path not found.\n");
}
}
}
}
Thanks..

php rename() Access is denied. (code: 5)

So I am trying to use rename function in php.
On the first try, if the destination folder is empty or does not contain any directories with the same name as the source folder the rename function works perfectly. However, if there is same directory name it fails. What I want is just to overwrite it and I thought rename() would suffice.
Here is my code:
/**
* Move temp folders to their permanent places
*
* $module_folder = example (violator, pnp, etc)
* $folders = name of folders within module_folder
**/
public function move_temp_to_permanent($module_folder, $folders){
$bool = false;
$module_folder_path = realpath(APPPATH . '../public/resources/temps/' . $module_folder);
$module_folder_destination_path = $_SERVER['DOCUMENT_ROOT'] . '/ssmis/public/resources/photos/' . $module_folder . '/';
foreach($folders as $folder){
$bool = rename($module_folder_path . '/' . $folder, $module_folder_destination_path . $folder);
}
return $bool;
}
The code above gives me an error saying:
Message:
rename(C:\xampp\htdocs\ssmis\public\resources\temps\violator/SJ-VIOL-2015-0002,C:/xampp/htdocs/ssmis/public/resources/photos/violator/SJ-VIOL-2015-0002):
Access is denied. (code: 5)
I am using CodeIgniter as framework.
Thank you very much!
If it is on Windows, this can be read in contributions:
rename() definitely does not follow the *nix rename convention on WinXP with PHP 5. If the $newname exists, it will return FALSE and $oldname and $newname will remain in their original state. You can do something like this instead:
function rename_win($oldfile,$newfile) {
if (!rename($oldfile,$newfile)) {
if (copy ($oldfile,$newfile)) {
unlink($oldfile);
return TRUE;
}
return FALSE;
}
return TRUE;
}
Link.
You are adding extra / in path make this like
/**
* Move temp folders to their permanent places
*
* $module_folder = example (violator, pnp, etc)
* $folders = name of folders within module_folder
**/
public function move_temp_to_permanent($module_folder, $folders){
$bool = false;
$module_folder_path = realpath(APPPATH . '../public/resources/temps/' . $module_folder);
$module_folder_destination_path = $_SERVER['DOCUMENT_ROOT'] . '/ssmis/public/resources/photos/' . $module_folder . '/';
foreach($folders as $folder){
$bool = rename($module_folder_path . $folder, $module_folder_destination_path . $folder);
}
return $bool;
}
you can also echo the path you prepared so you can check its right or not

Assetic is generating multiple files with same content

I have a class that uses Assetic to generate some css files to disk. I'll jump right into the code.
In my layout header, I'm doing something like this:
$assetify = new Assetify();
$assetify->setDebug(true);
$assetify->setAssetDirectory(BASE_DIR . '/public/assets');
$assetify->setOutputDirectory(BASE_DIR . '/public/assets/generated');
$assetify
->addStylesheet('/assets/css/bootstrap-2.3.2.css')
->addStylesheet('/assets/css/select2-3.4.3.css')
->addStylesheet('/assets/css/main.css');
echo $assetify->dump();
My "Assetify" class runs this through Assetic. I'll paste what I hope are only the relevant portions from the dump() function:
// The Asset Factory allows us to not have to do all the hard work ourselves.
$factory = new AssetFactory($this->assetDirectory, $this->debug);
$factory->setDefaultOutput('/generated/*.css');
// The Filter Manager allows us to organize filters for the asset handling.
// For other filters, see: https://github.com/kriswallsmith/assetic
$fm = new FilterManager();
$fm->set('yui_css', new Yui\CssCompressorFilter('/usr/local/bin/yuicompressor-2.4.7.jar'));
$fm->set('yui_js', new Yui\JsCompressorFilter('/usr/local/bin/yuicompressor-2.4.7.jar'));
$factory->setFilterManager($fm);
// The Asset Manager allows us to keep our assets organized.
$am = new AssetManager();
$factory->setAssetManager($am);
// The cache-busting worker prefixes every css with what amounts to a version number.
$factory->addWorker(new CacheBustingWorker());
$assetCollection = array();
foreach ($assetGroups as $assetGroup) {
foreach ($assetGroup as $media => $items) {
$fileCollection = array();
foreach ($items as $item) {
// Add this asset to the asset collection.
$fileCollection[] = new FileAsset($item);
}
$assetCollection[] = new AssetCollection($fileCollection);
}
}
$assetCollection = new AssetCollection($assetCollection);
$am->set('base_css', $assetCollection);
// Generate the required assets. Prefixing a filter name with a question mark
// will cause that filter to be omitted in debug mode.
$asset = $factory->createAsset(
array('#base_css'),
array('?yui_css')
);
// Configure an internal file system cache so we don't regenerate this file on every load.
$cache = new AssetCache(
$asset,
new FilesystemCache($this->outputDirectory)
);
// And generate static versions of the files on disk.
$writer = new AssetWriter($this->assetDirectory);
$writer->writeAsset($cache);
This generates two different files, 87229eb-f47a352.css and a37c1589762f39aee5bd24e9405dbdf9. The contents of the files are exactly the same. The 87229eb-f47a352.css file seems to get generated every single time, and the other file is not regenerated unless the contents of the files change (this is what I would like). If I comment out the $writer->writeAsset($cache), no files are written to disk.
What obvious configuration am I missing? I appreciate the help, thank you.
I was able to roughly replicate your code and got the same results.
I was trying to get the same results as what I think you require but ended up writing my own code to cache and serve static files.
It's not complete by any means but it is working. It has the following features:
You can choose to cache files for different pages if you specify $filename
You can choose to create versions of your released files or delete previous versions
A cached file will be generated to your target folder only if changes have made to a source file
You just need to put the code in to a class or function and return the url to serve.
Hope it helps :)
<?php
use Assetic\Factory\AssetFactory;
use Assetic\AssetManager;
use Assetic\FilterManager;
use Assetic\Asset\AssetCollection;
use Assetic\Asset\FileAsset;
use Assetic\Filter\JSMinFilter;
// JavaScript Collection
$js_collection[] = new FileAsset(SCRIPT_PATH . 'jquery.js');
$js_collection[] = new FileAsset(SCRIPT_PATH . 'production.js');
if (file_exists(SCRIPT_PATH . $page_info['name'] . '.js')) {
$js_collection[] = new FileAsset(SCRIPT_PATH . $page_info['name'] . '.js');
}
// CSS Collection
$css_collection[] = new FileAsset(STYLE_PATH . 'theme.css');
if (file_exists(STYLE_PATH . $page_info['name'] . '.css')) {
$css_collection[] = new FileAsset(STYLE_PATH . $page_info['name'] . '.css');
}
// The Filter Manager allows us to organize filters for the asset handling.
$fm = new FilterManager();
$fm->set('js', new JSMinFilter());
$js = new AssetCollection (
$js_collection
);
$js->setTargetPath(SCRIPT_PATH . 'static');
$css = new AssetCollection (
$css_collection
);
$css->setTargetPath(STYLE_PATH . 'static');
$am = new AssetManager();
$am->set('js', $js);
$am->set('css', $css);
//** TO DO: put the below in a class and return the static file names **//
// options
$seperator = '-';
$filename = $page_info['name'];
$versions = false;
// get a list of all collection names
$collections = $am->getNames();
// get each collection
foreach ($collections as $collection_name) {
// get the collection object
$collection = $am->get($collection_name);
// ensure file types are identical
$last_ext = false;
foreach ($collection as $leaf) {
$ext = strtolower(pathinfo($leaf->getSourcePath(), PATHINFO_EXTENSION));
if (!$last_ext || $ext == $last_ext) {
$last_ext = $ext;
} else {
throw new \RuntimeException('File type mismatch.');
}
}
// get the highest last-modified value of all assets in the current collection
$modified_time = $collection->getLastModified();
// get the target path
$path = $collection->getTargetPath();
// the target path must be set
if (!$path) {
throw new \RuntimeException('Target path not specified.');
}
// build the filename to check
$file = ($filename) ? $filename . $seperator . $modified_time . '.' . $ext : $modified_time . '.' . $ext;
$cached_file = $path . '/' . $file;
// the file doesn't exist so we need to minify, dump and save as new cached file
if (!file_exists($cached_file)) {
// create the output dir if it doesnt exist
if (!is_dir($path) && false === #mkdir($path, 0777, true)) {
throw new \RuntimeException('Unable to create directory ' . $path);
}
// apply the filters
if ($fm->has($collection_name)) {
$collection->ensureFilter($fm->get($collection_name));
}
// If not versioned, delete previous version of this file
if (!$versions) {
if ($filename) {
foreach (glob($path . '/' . $filename . $seperator . '*.' . $ext) as $searchfile) {
#unlink($searchfile);
}
} else {
foreach (glob($path . '/*.' . $ext) as $searchfile) {
#unlink($searchfile);
}
}
}
// put the contents in the file
if (false === #file_put_contents($cached_file, $collection->dump())) {
throw new \RuntimeException('Unable to write file ' . $cached_file);
}
}
// return the cached file
echo 'output: ' . $cached_file . '<br>';
}
exit;
?>

Categories