I have a application which is developed in PHP using the Symfony 2 framework. I have changed a HTML file, but the change is not reflecting when I refresh the page.
I restarted the server. No luck.
I tried to remove the Twig folder from the /protected/cache/ page itself. This is not loading.
How can I reload the Twig cache?
Notes:
I am using tomcat server to deploy.
I don’t have the Symfony 2 command line configured on the server.
I am new to PHP.
The most simple way, type the command :
rm -rf app/cache/*
The point is: all files in app/cache/ can be removed freely, they are regenerated when needed.
If you really want to clear only twig cache :
rm -rf app/cache/<environment>/twig
Replace <environment> by dev, prod, or test according to your requirements.
When creating a new Twig_Environment instance, you can pass an array of options as the constructor second argument. One of them is auto_reload. When developing with Twig, it's useful to recompile the template whenever the source code changes. If you don't provide a value for the auto_reload option, it will be determined automatically based on the debug value.
Set auto_reload to be true:
$twig = new Twig_Environment($loader, array('auto_reload' => true));
Twig's documentation for developers:
http://twig.sensiolabs.org/doc/api.html#environment-options
I had a similar problem, but deleting the cache-folder did not have any impact on my template and I don't know why. What seems to solve my problem now is the following code in my config_dev.yml:
twig:
cache: false
Maybe this is also a solution for you, so that you don't need to use the command all the time.
References:
TwigBundle Configuration
Twig Environment Options
If you are using opcache/other similar caching, deleting twig's cache folder won't refresh templates as twig cache consist of only .php files.
You need to delete twig's cache folder + execute php file which contains:
opcache_reset();
You have to do some changes in app.php file located in web folder.
Change:
$kernel = new AppKernel('prod', false);
to:
$kernel = new AppKernel('prod', true);
and clear the cache if you want.
You can use the Symfony console to clear cache
./bin/console cache:clear
you can add a function like this :
public function renderView($view, array $parameters = array())
{
$loader = new \Twig_Loader_Filesystem($this->container->getParameter("template_path"));
$twig = new \Twig_Environment($loader, array('auto_reload' => true,
'cache' => false
));
/////////////////////add a translate filter///////////////////////
$getTextdomain = new \Twig_SimpleFilter('trans',function ($string){
return $this->container->get('translator')->trans($string);
});
$twig->addFilter($getTextdomain);
//////////////////////////////////////////////////////////////////
///////////////////////////Add an extension twig//////////////////
$twig->addExtension(new Extension());
//////////////////////////////////////////////////////////////////
return $twig->render($view, $parameters);
}
If you are using OPcache make sure to comment out opcache.validate_timestamps=0 in dev environment.
Related
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
$cache = new FilesystemAdapter();
$value = $cache->get('my_cache_key', function (ItemInterface $item) {
$item->expiresAfter(3600);
// ... do some HTTP request or heavy computations
$computedValue = 'foobar';
return $computedValue;
});
I use Symfony 5.4 and the cache contracts on an application and some cache expirations are quite long. My problem is that some values need to be changed and to do it properly, I would need to be able to purge the cache with a command line on my production server to be sure to have correct data.
I can make a custom command ex: php bin/console app:cache:custom-clear that invalidates some tags but I'm surprised I don't have a native command to do this cache purge operation globally.
It may be that it's simple and I didn't understand anything but I don't see much in the doc on this point.
If anyone has a lead, I'm interested.
I am trying to debug some code on my Drupal 9 application.
For example, at the file web/index.php, I try to add die('Was here')
<?php
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
$autoloader = require_once 'autoload.php';
//My code is here
die('Was here');
$kernel = new DrupalKernel('prod', $autoloader);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
At first, I got the result on my browser. Next I delete this die function, and when i refresh my browser, I get the same as before as if I didn't change the code
After some minutes, the expected result is shown in my browser. So weird
So I am wondering why is Drupal isn't taking that code change into account.
Btw, I run the command drush cr but it didn't change anything
That sounds like PHP OpCode Cache.
You can see if its enabled or not in the Drupal status report under "PHP OPcode caching".
There's a page on on disabling Drupal Caching [here][1] which includes a section on opcache.
I believe to disable opcache you can add an entry to your php.ini file.
opcache.enable=0
Be sure to restart relevant services like php-fpm. And verify it was effective in the Drupal status report.
[1]: https://www.drupal.org/node/2598914
I want to use Supervisor to manager my processes. I have got it installed on my Amazon linux Machine and the basic setup runs fine as per the config file.
Now, I want to change the processes dynamically. Since it needs the config file to be changed every time and a restart, using PHP library to do the same seems to be a good option.
Specifically I am going through SupervisorPHP config to change the configuration dynamically and SupervisorPHP to manager Supervisor through PHP.
Following the README for SupervisorPHP config, I got it installed via composer
composer require supervisorphp/configuration
I copied the sample code
<?php
use Supervisor\Configuration\Configuration;
use Supervisor\Configuration\Section\Supervisord;
use Supervisor\Configuration\Section\Program;
use Indigophp\Ini\Rendere;
$config = new Configuration;
$renderer = new Renderer;
$section = new Supervisord(['identifier' => 'supervisor']);
$config->addSection($section);
$section = new Program('test', ['command' => 'cat']);
$config->addSection($section);
echo $renderer->render($config->toArray());
When I run this code, I get the following error:
PHP Fatal error: Class 'Supervisor\Configuration\Configuration' not found in test.php on line 7
I also tried to clone the repo and include the files individually, however it shows error for other dependencies. It would be great if I could use this.
There are 2 mistakes in the above code.
The first mistake is that you do not use the autoloader provided by composer so that php can find the necessary classes. To do so just add require __DIR__ . '/vendor/autoload.php'; (If the vendor folder is in a different path relative to the sample script then adjust accordingly).
The second mistake is in the use statement for Indigophp. Apart from the obvious typo in the word Renderer, if you check the source of Indigo you will see that it must be use Indigo\Ini\Renderer;
So the correct code to test your installation is:
<?php
require __DIR__ . '/vendor/autoload.php';
use Supervisor\Configuration\Configuration;
use Supervisor\Configuration\Section\Supervisord;
use Supervisor\Configuration\Section\Program;
use Indigo\Ini\Renderer;
$config = new Configuration;
$renderer = new Renderer;
$section = new Supervisord(['identifier' => 'supervisor']);
$config->addSection($section);
$section = new Program('test', ['command' => 'cat']);
$config->addSection($section);
echo $renderer->render($config->toArray());
Running the above code, you should get the following output:
[supervisord]
identifier = supervisor
[program:test]
command = cat
I want to use the composer/composer PHP classes to update individual plugin packages.
I do not want to use command-line solutions like exec("php composer.phar update");
I am unable to get it to work. I have tried several different options, much alike the following code.
It just returns a blank screen.
use Composer\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
$input = new ArrayInput(array('command' => 'require vendor/packkage dev-master'));
$output = new BufferedOutput();
$application = new Application();
$application->run($input, $output);
dd($output->fetch());
Things i would like to achieve:
Download/Update individual packages
Get result output to verify success
Dump autoload
Remove/require packages
A bit of context details:
I am creating a plugin updater for my PHP application (in admin panel).
Every plugin is a composer package and resides on my own Satis repository.
The plugins get installed into a custom dir using my composer plugin.
I can read composer.lock locally and packages.json on the satis server to figure out
what packages require updates.
update
I've managed to at least get it to work. The no-output issue was due to $application->setAutoExit that needed to be false before running. Next issue that i had was that the required package would download itself into the same directory as the class where i called it from. Solved that by using putenv and chdir. Result:
root/comp.php
putenv('COMPOSER_HOME=' . __DIR__ . '/vendor/bin/composer');
chdir(__DIR__);
root/workbench/sumvend/sumpack/src/PackageManager.php
include(base_path() . '/comp.php');
$input = new ArrayInput(array('command' => 'require', 'packages' => ['vend/pak dev-master']));
$output = new BufferedOutput();
$application = new Application();
$application->setAutoExit(false);
$application->run($input, $output); //, $output);
dd($output->fetch());
This works, but it's far from ideal.
The full solution to this would be pretty long winded, but I will try to get you on the right track.
php composer.phar require composer/composer dev-master
You can load the source of composer into your project vendors. You might have already done this.
The code you are looking for is at: Composer\Command\RequireCommand.
$install = Installer::create($io, $composer);
$install
->setVerbose($input->getOption('verbose'))
->setPreferSource($input->getOption('prefer-source'))
->setPreferDist($input->getOption('prefer-dist'))
->setDevMode($updateDevMode)
->setUpdate(true)
->setUpdateWhitelist(array_keys($requirements))
->setWhitelistDependencies($input->getOption('update-with-dependencies'));
;
$status = $install->run();
Most of the command relates to the reading and writing to of the composer.json file.
However the installer itself is independent of where the configuration actually came from. You could in theory store the configuration in a database.
This is the static create method for the installer:
public static function create(IOInterface $io, Composer $composer)
{
return new static(
$io,
$composer->getConfig(),
$composer->getPackage(),
$composer->getDownloadManager(),
$composer->getRepositoryManager(),
$composer->getLocker(),
$composer->getInstallationManager(),
$composer->getEventDispatcher(),
$composer->getAutoloadGenerator()
);
}
You will need to pay special attention to the Package, And implement your own.
Although your current attempt to run it on the command line will work, I do not recommend it because Composer is primarily a development and deployment utility, not an application utility.
In order to smoothly use it to assist with loading plugins on a production environment, you will need to tightly integrate its internals with your own application, not just use it on the side.
This is something I am interested in as well, and I think this has inspired me to look into it myself. So I'll let you know what I come up with, but this is the best I can advise you for now on what I consider to be the correct approach.
I do have a PHP script, which is not an extension for Typo3. Now I would like to delete the whole Cache of Typo3 out of this script. How is that possible?
install the TYPO3 Extension cleartypo3cache
create a tool and a keyboard shortcut in PhpStorm 4 to trigger cleartypo3cache
SSH access with passwordless pubkey authentication when pushing to a remote host.
Install Extension "cleartypo3cache" and create the BE user "_cli_cleartypo3cache" and add the following TSconfig:
options.clearCache.all=1
options.clearCache.pages=1
Now test if cache is cleared:
$ cd /path/tp/typo3-site/
$ php typo3/cli_dispatch.phpsh cleartypo3cache all
If your webserver is on localhost, you are lucky because you don't need this shell script. If your webserver is on a remote host, you need an additional wrapper script. This is because PhpStorm does not provide an environment variable for the remote host directory. You have to set this directory statically for each project in the wrapper script:
#!/bin/sh
TYPO3_SITE_PATH="/path/to/typo3-site"
USER="alice"
HOST="example.com"
/usr/bin/ssh $USER#$HOST '/usr/bin/php $TYPO3_SITE_PATH/typo3/cli_dispatch.phpsh cleartypo3cache all'
Save this file in your project file directory into .idea/clear-typo3-cache.sh and make it executable:
$ chmod 755 .idea/clear-typo3-cache.sh
PhpStorm External Tools
You need to create an "external tool" in PhpStorm to be able to clear cache.
Go to PhpStorm-->Settings-->External Tools-->Add...
Give your tool a name and a group, e.g. "Deployment" -> "Clear TYPO3 Cache"
Deactivate checkbox "Open Console" and "Menu->Search Results"
Remote host scenario
Add the following line to "Programm:"
$ProjectFileDir$/.idea/clear-typo3-cache.sh
Localhost scenario
Add this line to "Program:"
$PhpExecutable$
Add this line to "Parameters:"
$ProjectFileDir$/typo3/cli_dispatch.phpsh cleartypo3cache all
You need to have a PHP interpreter configured in PhpStorm-->Settings-->PHP to use $PhpExecutable$. Alternatively you can use /usr/bin/php
(source: t3node.com)
PhpStorm Keymap
I suggest to use the same key binding as you use for saving or remote host uploading:
Go to PhpStorm-->Settings-->Keymap
For remote host scenario, navigate to: Main menu-->Tools-->Deployment-->Upload to Default Server. Notice the existing shortcut. If you don't have one for that, create a new one (I use ALT+SHIFT+U)
For the localhost scenario, just use Ctrl+S (Main menu-->File-->Save All).
Now navigate to the External Tool you have created (e.g. External Tools-->Deployment->Clear TYPO3 Cache)
Right click "Add Keyboard Shortcut"
Create the particular shortcut in "First Stroke"
Now PhpStorm will warn you that the shortcut is already in use for a different command. That's fine, it's exactly what we want to have.
That's it. Your TYPO3 caches are always cleared when you hit save or upload on your keyboard.
adapted from t3node
I found the solution myself and its actually pretty easy. I took a look into the class.t3lib_tcemain.php in the t3lib folder. There you've got the necessary commands to clear the cache. It also checks, if you have the cachingframework enabled. If so, you need to truncate a few other tables as well (Starts with cachingframework_cache_)
It is basically:
<?php
require_once('./typo3conf/localconf.php');
$conn = mysql_connect($typo_db_host, $typo_db_username, $typo_db_password);
mysql_select_db($typo_db);
// Clear Cache here
mysql_query("TRUNCATE cache_treelist;");
mysql_query("TRUNCATE cache_pagesection;");
mysql_query("TRUNCATE cache_hash;");
mysql_query("TRUNCATE cache_pages;");
if($handle = opendir('./typo3conf')) {
while (false !== ($file = readdir($handle))) {
if(strpos($file, 'temp_CACHED_')!==false) {
unlink('./typo3conf/'.$file);
}
}
closedir($handle);
}
?>
TYPO3 >= 7
From TYPO3 7 on you can install Helmut Hummels Extension typo3_console.
Then you can clear the cache like (for composer installations):
./vendor/bin/typo3cms cache:flush
https://extensions.typo3.org/extension/typo3_console/
https://github.com/TYPO3-Console/TYPO3-Console
TYPO3 6.x
first initialize the Service in your Class
/**
* #var Tx_Extbase_Service_CacheService
*/
protected $cacheService;
/**
* #param Tx_Extbase_Service_CacheService $cacheService
* #return void
*/
public function injectCacheService(Tx_Extbase_Service_CacheService $cacheService) {
$this->cacheService = $cacheService;
}
in your function just call
$this->cacheService->clearPageCache($pids);
while $pids is an integer (for single page) or array of integers (multiple pages)
see: http://typo3.org/api/typo3cms/class_t_y_p_o3_1_1_c_m_s_1_1_extbase_1_1_service_1_1_cache_service.html
In TYPO3 since 4.5 (I think) its a static method so you have just to call
Tx_Extbase_Utility_Cache::clearPageCache($pids);
in your controller.
Found it here: http://www.phpkode.com/source/p/typo-cms/typo3_src+dummy-4.6.5/typo3/sysext/extbase/Classes/MVC/Controller/ActionController.php
In FLOW3 there is a possibility to do such stuff, as far as I know with TYPO3 v.4.x You have no such default CLI option, so You should use or You own script, or use such extensions as cleartypo3cache or Cli Cleaner.
Also I made a bash script to clean cache tables of Your dB : https://gist.github.com/fedir/5162747
in typo3 6.x extbase its simple.
Edit : clearPageCache is not static then you need to create object of CacheService
TYPO3\CMS\Extbase\Service\CacheService::clearPageCache(pageUid);