Jump to content

Classes being cached by FileCompiler


FireWire
 Share

Recommended Posts

I'm building a site that renders certain features using reusable classes. Unfortunately FileCompiler caches the files and my changes don't show up unless I clear the FileCompiler cache.

On top of that, when I try to use the wire() function inside a class it throws a "Fatal error: Uncaught Error: Call to undefined function wire()". I've tried playing with ProcessWire namespacing, but have come up short.

My files are structured as such:

app.php - Is included in my templates and loads classes as they're called with spl_autoload_register.
- classes/ExampleClass.class.php - Loaded by app.php

Thanks!
 

Link to comment
Share on other sites

1 hour ago, abdus said:

Make sure file starts with <?php namespace ProcessWire;

I've added this to both app.php which loads the Class file, now it throws a new error saying that the class isn't found but it says that it's looking for the class in the template itself, instead of app.php it was looking for it before (the structure I noted above in my original post).

Really trying to figure this out but it's throwing new errors and I'm doing some very basic things...

Link to comment
Share on other sites

Solved.

@Zeka's suggestion to disable templateCompile solved all caching issues with class files.

 

I was also using: 

spl_autoload_register()

To dynamically load classes when they're called, unfortunately this doesn't seem to play well with PW. I used a regular include loop to add classes to app.php and it all works with namespacing.

Big thanks to @abdus for the help!

  • Like 1
Link to comment
Share on other sites

@skylundy, turns out ProcessWire has a built-in class loader. It uses spl_autoload_register() internally.

Given a folder structure:

/site/templates/
    components/
        Composer.php
        Writer.php
    app.php

where Composer.php and Writer.php are classes under \ProcessWire namespace

Spoiler

<?php namespace ProcessWire;
// /site/templates/components/Composer.php

class Composer
{
    public function compose($text)
    {
        return $text;
    }
}

<?php namespace ProcessWire;
// /site/templates/components/Writer.php

class Writer
{
    public function write($text)
    {
        echo $text;
    }
}

 

You can autoload them using

// /site/templates/app.php

$loader = new WireClassLoader($wire);
// or use $classLoader since it's already an API variable.


// autoload classes inside /site/templates/components/
$componentsPath = wire()->config->paths->templates . 'components/';
$loader->addNamespace('ProcessWire', $componentsPath);


// then you can reference classes just fine.
$b = new Composer();
$w = new Writer();
$w->write($b->compose('hello'));

http://processwire.com/api/ref/class-loader/

  • Like 5
Link to comment
Share on other sites

Just now, abdus said:

@skylundy, turns out ProcessWire has a built-in class loader.

Given a folder structure:


/site/templates/
    components/
        Composer.php
        Writer.php
    app.php

where Composer.php and Writer.php are classes under \ProcessWire namespace

  Hide contents


<?php namespace ProcessWire;
// /site/templates/components/Composer.php

class Composer
{
    public function compose($text)
    {
        return $text;
    }
}


<?php namespace ProcessWire;
// /site/templates/components/Writer.php

class Writer
{
    public function write($text)
    {
        echo $text;
    }
}

 

You can autoload them using


// /site/templates/app.php

$loader = new WireClassLoader($wire);
// or use $classLoader since it's already an API variable.


// autoload classes inside /site/templates/components/
$componentsPath = wire()->config->paths->templates . 'components/';
$loader->addNamespace('ProcessWire', $componentsPath);


// then you can reference classes just fine.
$b = new Composer();
$w = new Writer();
$w->write($b->compose('hello'));

 

 

Great find! Thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...